-
Notifications
You must be signed in to change notification settings - Fork 28
Refactor CVE fix skill into shell scripts #2383
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dfarrell07
wants to merge
3
commits into
submariner-io:devel
Choose a base branch
from
dfarrell07:cve-fix-skill-redesign
base: devel
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| #!/bin/bash | ||
| # Kill orphaned CVE fix processes and containers. | ||
| # Safe to run anytime — only targets fix-all.sh, grype, and dapper fix containers. | ||
| pkill -f fix-all.sh 2>/dev/null || true | ||
| docker ps --format '{{.ID}} {{.Image}}' 2>/dev/null | grep -E 'grype:latest|fix-.*-cves-' | awk '{print $1}' | xargs -r docker kill 2>/dev/null || true |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #!/bin/bash | ||
| # Detect repository configuration for CVE fixing and optionally set up fix branch. | ||
| # Usage: detect.sh [repo] [branch] [--setup-branch] | ||
| # Prints state file path as last line of stdout. | ||
| set -euo pipefail | ||
|
|
||
| SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | ||
| # shellcheck source-path=SCRIPTDIR | ||
| # shellcheck source=lib.sh | ||
| source "$SCRIPT_DIR/lib.sh" | ||
|
|
||
| # --- Argument parsing (order-independent) --- | ||
| REPO="" | ||
| BRANCH="" | ||
| SETUP_BRANCH=false | ||
|
|
||
| for arg in "$@"; do | ||
| case "$arg" in | ||
| --setup-branch) SETUP_BRANCH=true ;; | ||
| *) | ||
| # Expand tilde | ||
| arg="${arg/#\~/$HOME}" | ||
| # Try sibling directory for bare names (e.g., "subctl" -> "../subctl") | ||
| if [[ "$arg" != */* ]] && [[ -d "../$arg" ]]; then | ||
| arg="../$arg" | ||
| fi | ||
| if [[ "$arg" == /* ]] || [[ "$arg" == ./* ]] || [[ "$arg" == ../* ]] || [[ -d "$arg" ]]; then | ||
| [[ -n "$REPO" ]] && { echo "ERROR: Multiple repositories specified" >&2; exit 1; } | ||
| REPO="$arg" | ||
| else | ||
| [[ -n "$BRANCH" ]] && { echo "ERROR: Multiple branches specified" >&2; exit 1; } | ||
| BRANCH="$arg" | ||
| fi | ||
| ;; | ||
| esac | ||
| done | ||
|
|
||
| REPO="${REPO:-.}" | ||
|
|
||
| # --- Validate repo --- | ||
| [[ -d "$REPO" ]] || { echo "ERROR: Repository not found: $REPO" >&2; exit 1; } | ||
| git -C "$REPO" rev-parse --git-dir &>/dev/null || { echo "ERROR: Not a git repository: $REPO" >&2; exit 1; } | ||
|
|
||
| # --- Resolve branch --- | ||
| if [[ -z "$BRANCH" ]]; then | ||
| BRANCH=$(git -C "$REPO" branch --show-current 2>/dev/null) | ||
| [[ -n "$BRANCH" ]] || { echo "ERROR: Not on a branch (detached HEAD). Specify branch explicitly." >&2; exit 1; } | ||
| echo "Using current branch: $BRANCH" | ||
| fi | ||
|
|
||
| # Normalize short version (0.23 -> release-0.23) | ||
| if [[ "$BRANCH" =~ ^[0-9]+\.[0-9]+$ ]]; then | ||
| BRANCH="release-${BRANCH}" | ||
| echo "Normalized to branch: $BRANCH" | ||
| fi | ||
|
|
||
| # --- Change to repo (absolute path) --- | ||
| REPO="$(cd "$REPO" && pwd)" | ||
| cd "$REPO" | ||
|
|
||
| REPO_NAME=$(basename "$REPO") | ||
| echo "" | ||
| echo "=== CVE Fix: $REPO_NAME/$BRANCH ===" | ||
|
|
||
| # --- Detect config --- | ||
| HAS_TOOLS_GOMOD=false | ||
| test -f tools/go.mod && HAS_TOOLS_GOMOD=true | ||
|
|
||
| GENERATED_FILE="" | ||
| DIFF_IGNORE_ARGS="" | ||
| if grep -rql "controller-gen.kubebuilder.io/version" --include="*.go" . 2>/dev/null; then | ||
| DIFF_IGNORE_ARGS="-Icontroller-gen.kubebuilder.io/version" | ||
| GENERATED_FILE=$(grep -rl "controller-gen.kubebuilder.io/version" --include="*.go" . 2>/dev/null | head -1) | ||
| elif find . -name "*.pb.go" -type f 2>/dev/null | head -1 | grep -q .; then | ||
| DIFF_IGNORE_ARGS="-I^//" | ||
| GENERATED_FILE=$(find . -name "*.pb.go" -type f 2>/dev/null | head -1) | ||
| fi | ||
|
|
||
| NEEDS_BUILD_FOR_SCAN=false | ||
| grep -q '^build:' Makefile 2>/dev/null && NEEDS_BUILD_FOR_SCAN=true | ||
|
|
||
| CONTAINER_CMD=$(detect_container_cmd) | ||
|
|
||
| HAS_LOCAL_GRYPE=false | ||
| command -v grype &>/dev/null && HAS_LOCAL_GRYPE=true | ||
|
|
||
| # Shipyard build image | ||
| if [[ "$BRANCH" == "devel" ]]; then | ||
| SHIPYARD_TAG="devel" | ||
| elif [[ "$BRANCH" =~ ^release- ]]; then | ||
| SHIPYARD_TAG="$BRANCH" | ||
| else | ||
| echo "WARNING: Unknown branch pattern, assuming devel build image" | ||
| SHIPYARD_TAG="devel" | ||
| fi | ||
|
|
||
| SHIPYARD_IMAGE="quay.io/submariner/shipyard-dapper-base:${SHIPYARD_TAG}" | ||
| SHIPYARD_GO_VERSION="unknown" | ||
| if [[ -n "$CONTAINER_CMD" ]]; then | ||
| if $CONTAINER_CMD pull "$SHIPYARD_IMAGE" >/dev/null 2>&1; then | ||
| SHIPYARD_GO_VERSION=$($CONTAINER_CMD run --rm "$SHIPYARD_IMAGE" go version 2>/dev/null || echo "unknown") | ||
| fi | ||
| fi | ||
|
|
||
| # --- Branch setup (optional) --- | ||
| ORIGINAL_REF="" | ||
| FIX_BRANCH="" | ||
| FETCH_FAILED=false | ||
|
|
||
| if [[ "$SETUP_BRANCH" == "true" ]]; then | ||
| if ! git diff --quiet 2>/dev/null || ! git diff --cached --quiet 2>/dev/null; then | ||
| echo "ERROR: Working tree has uncommitted changes. Commit or stash first." >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| ORIGINAL_REF=$(git rev-parse --abbrev-ref HEAD 2>/dev/null) | ||
| [[ "$ORIGINAL_REF" == "HEAD" ]] && ORIGINAL_REF=$(git rev-parse HEAD) | ||
|
|
||
| if ! GIT_SSH_COMMAND="ssh -o BatchMode=yes" git fetch 2>/dev/null; then | ||
| echo "WARNING: git fetch failed. Continuing with cached remote state." | ||
| FETCH_FAILED=true | ||
| fi | ||
|
|
||
| DATE=$(date +%Y-%m-%d) | ||
| VERSION="${BRANCH//release-/}" | ||
| FIX_BRANCH="fix-${VERSION}-cves-${DATE}" | ||
|
|
||
| SUFFIX="" | ||
| while git show-ref --verify --quiet "refs/heads/${FIX_BRANCH}${SUFFIX}"; do | ||
| if [[ -z "$SUFFIX" ]]; then SUFFIX="-v2"; else NUM=${SUFFIX#-v}; SUFFIX="-v$((NUM+1))"; fi | ||
| done | ||
| FIX_BRANCH="${FIX_BRANCH}${SUFFIX}" | ||
|
|
||
| if ! git checkout -b "$FIX_BRANCH" "origin/$BRANCH" 2>/dev/null; then | ||
| echo "ERROR: Could not create fix branch from origin/$BRANCH" >&2 | ||
| exit 1 | ||
| fi | ||
| echo "Fix branch: $FIX_BRANCH" | ||
| fi | ||
|
|
||
| # --- Write state file --- | ||
| STATE_FILE=$(state_file_path "$REPO" "$BRANCH") | ||
| cat > "$STATE_FILE" <<EOF | ||
| STATE_FILE="$STATE_FILE" | ||
| REPO="$REPO" | ||
| BRANCH="$BRANCH" | ||
| FIX_BRANCH="$FIX_BRANCH" | ||
| ORIGINAL_REF="$ORIGINAL_REF" | ||
| HAS_TOOLS_GOMOD=$HAS_TOOLS_GOMOD | ||
| GENERATED_FILE="$GENERATED_FILE" | ||
| DIFF_IGNORE_ARGS="$DIFF_IGNORE_ARGS" | ||
| NEEDS_BUILD_FOR_SCAN=$NEEDS_BUILD_FOR_SCAN | ||
| CONTAINER_CMD="$CONTAINER_CMD" | ||
| HAS_LOCAL_GRYPE=$HAS_LOCAL_GRYPE | ||
| SHIPYARD_IMAGE="$SHIPYARD_IMAGE" | ||
| SHIPYARD_GO_VERSION="$SHIPYARD_GO_VERSION" | ||
| FETCH_FAILED=$FETCH_FAILED | ||
| CVE_SCRIPTS="$SCRIPT_DIR" | ||
| EOF | ||
|
|
||
| # Last line is the state file path (captured by callers) | ||
| echo "$STATE_FILE" | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shell-escape values before writing the sourced state file.
This file is later
sourced byscan.sh,fix-all.sh,review.sh, and others. Writing raw values inside double quotes means a repo path or branch containing", backticks, or$(...)can break parsing or execute commands when the state file is loaded.🛡️ Proposed hardening
📝 Committable suggestion
🤖 Prompt for AI Agents
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't fix. All values are controlled (paths from pwd, git branch names, hardcoded strings like docker/true/false). No user-supplied input reaches the state file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dfarrell07, fair point — looking at the concrete values:pwd-derived paths, git ref names (which cannot contain"or$()),true/falseliterals, hardcoded image strings, andgo versionoutput. None of these realistically carry shell-metacharacter payloads, so the heredoc approach is safe in practice here. Acknowledged, moving on.😔