mirror of
https://github.com/anthropics/claude-code.git
synced 2026-04-16 16:07:50 +00:00
Improve gh.sh wrapper: stricter validation and better error messages
- Use allowlist for issue view (numeric issue numbers only) - Enforce zero positional args for issue list / label list - Pin GH_HOST and GH_REPO explicitly to avoid ambient state - Add descriptive error messages with usage examples
This commit is contained in:
@@ -11,6 +11,15 @@ set -euo pipefail
|
|||||||
# ./scripts/gh.sh search issues "search query" --limit 10
|
# ./scripts/gh.sh search issues "search query" --limit 10
|
||||||
# ./scripts/gh.sh label list --limit 100
|
# ./scripts/gh.sh label list --limit 100
|
||||||
|
|
||||||
|
export GH_HOST=github.com
|
||||||
|
|
||||||
|
REPO="${GH_REPO:-${GITHUB_REPOSITORY:-}}"
|
||||||
|
if [[ -z "$REPO" || "$REPO" == */*/* || "$REPO" != */* ]]; then
|
||||||
|
echo "Error: GH_REPO or GITHUB_REPOSITORY must be set to owner/repo format (e.g., GITHUB_REPOSITORY=anthropics/claude-code)" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
export GH_REPO="$REPO"
|
||||||
|
|
||||||
ALLOWED_FLAGS=(--comments --state --limit --label)
|
ALLOWED_FLAGS=(--comments --state --limit --label)
|
||||||
FLAGS_WITH_VALUES=(--state --limit --label)
|
FLAGS_WITH_VALUES=(--state --limit --label)
|
||||||
|
|
||||||
@@ -21,6 +30,7 @@ case "$CMD" in
|
|||||||
"issue view"|"issue list"|"search issues"|"label list")
|
"issue view"|"issue list"|"search issues"|"label list")
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
|
echo "Error: only 'issue view', 'issue list', 'search issues', 'label list' are allowed (e.g., ./scripts/gh.sh issue view 123)" >&2
|
||||||
exit 1
|
exit 1
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
@@ -45,6 +55,7 @@ for arg in "$@"; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
if [[ "$matched" == false ]]; then
|
if [[ "$matched" == false ]]; then
|
||||||
|
echo "Error: only --comments, --state, --limit, --label flags are allowed (e.g., ./scripts/gh.sh issue list --state open --limit 20)" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
FLAGS+=("$arg")
|
FLAGS+=("$arg")
|
||||||
@@ -62,24 +73,24 @@ for arg in "$@"; do
|
|||||||
fi
|
fi
|
||||||
done
|
done
|
||||||
|
|
||||||
REPO="${GH_REPO:-${GITHUB_REPOSITORY:-}}"
|
|
||||||
|
|
||||||
if [[ "$CMD" == "search issues" ]]; then
|
if [[ "$CMD" == "search issues" ]]; then
|
||||||
if [[ -z "$REPO" ]]; then
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
QUERY="${POSITIONAL[0]:-}"
|
QUERY="${POSITIONAL[0]:-}"
|
||||||
QUERY_LOWER=$(echo "$QUERY" | tr '[:upper:]' '[:lower:]')
|
QUERY_LOWER=$(echo "$QUERY" | tr '[:upper:]' '[:lower:]')
|
||||||
if [[ "$QUERY_LOWER" == *"repo:"* || "$QUERY_LOWER" == *"org:"* || "$QUERY_LOWER" == *"user:"* ]]; then
|
if [[ "$QUERY_LOWER" == *"repo:"* || "$QUERY_LOWER" == *"org:"* || "$QUERY_LOWER" == *"user:"* ]]; then
|
||||||
|
echo "Error: search query must not contain repo:, org:, or user: qualifiers (e.g., ./scripts/gh.sh search issues \"bug report\" --limit 10)" >&2
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
gh "$SUB1" "$SUB2" "$QUERY" --repo "$REPO" "${FLAGS[@]}"
|
gh "$SUB1" "$SUB2" "$QUERY" --repo "$REPO" "${FLAGS[@]}"
|
||||||
|
elif [[ "$CMD" == "issue view" ]]; then
|
||||||
|
if [[ ${#POSITIONAL[@]} -ne 1 ]] || ! [[ "${POSITIONAL[0]}" =~ ^[0-9]+$ ]]; then
|
||||||
|
echo "Error: issue view requires exactly one numeric issue number (e.g., ./scripts/gh.sh issue view 123)" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
gh "$SUB1" "$SUB2" "${POSITIONAL[0]}" "${FLAGS[@]}"
|
||||||
else
|
else
|
||||||
# Reject URLs in positional args to prevent cross-repo access
|
if [[ ${#POSITIONAL[@]} -ne 0 ]]; then
|
||||||
for pos in "${POSITIONAL[@]}"; do
|
echo "Error: issue list and label list do not accept positional arguments (e.g., ./scripts/gh.sh issue list --state open, ./scripts/gh.sh label list --limit 100)" >&2
|
||||||
if [[ "$pos" == http://* || "$pos" == https://* ]]; then
|
exit 1
|
||||||
exit 1
|
fi
|
||||||
fi
|
gh "$SUB1" "$SUB2" "${FLAGS[@]}"
|
||||||
done
|
|
||||||
gh "$SUB1" "$SUB2" "${POSITIONAL[@]}" "${FLAGS[@]}"
|
|
||||||
fi
|
fi
|
||||||
|
|||||||
Reference in New Issue
Block a user