Skip to content

ci: harden auto-fix path validation, prompt isolation, and PR token scope - #1088

Merged
dkhawk merged 6 commits into
mainfrom
ci/harden-auto-fix-followups
Nov 17, 2026
Merged

dkhawk merged 6 commits into
mainfrom
ci/harden-auto-fix-followups

Conversation

@dkhawk

@dkhawk dkhawk commented Nov 13, 2026

Copy link
Copy Markdown
Collaborator

Follow-up security hardening on top of #1087 (ci/harden-auto-fix) to close remaining CI execution, path-injection, and prompt-injection vectors:

Why these additional guardrails are needed

  1. Downstream on: pull_request CI execution with repository secrets (auto-fix.yml & create_pr.py)

    • While auto-fix.yml runs ./gradlew compileDebugKotlin with persist-credentials: false, pushing the branch auto-fix/issue-<N> directly to origin using secrets.SYNCED_GITHUB_TOKEN_REPO and opening a non-draft PR immediately triggers same-repo on: pull_request workflows (test.yml, instrumentation-test.yml, demo-smoke-test.yml) where head.repo.full_name == github.repository.
    • Those workflows execute unit tests (src/test/) and instrumentation tests (src/androidTest/)—both of which were previously reachable under */src/—with access to secrets.ACTIONS_API_KEY, secrets.SYNCED_GITHUB_TOKEN_REPO, and secrets.GITHUB_TOKEN.
    • Fix:
      • Switch auto-fix.yml from SYNCED_GITHUB_TOKEN_REPO to secrets.GITHUB_TOKEN and open the PR with --draft so downstream pull_request workflows do not automatically execute unreviewed AI-generated code until a maintainer reviews the diff.
      • Narrow EDITABLE_SOURCE_DIRS in create_pr.py from */src/ to */src/main/ (and exclude src/main/generated/), keeping src/test/, src/androidTest/, and src/screenshotTest/ off-limits.
  2. Filename newline / control-character injection (create_pr.py & auto-fix.yml)

    • validate_source_path checked relative.startswith(EDITABLE_SOURCE_DIRS) and relative.endswith(".kt"), which still allowed literal newlines (\n) inside path segments (valid on Linux ext4). When auto-fix.yml runs mapfile -t CHANGED_FILES < <(jq -r '.files[]' pr_metadata.json), an embedded newline splits one .files[] entry into multiple arguments to git add.
    • Fix: Enforce SAFE_PATH_PATTERN (^[A-Za-z0-9_][A-Za-z0-9_.-]*(/[A-Za-z0-9_][A-Za-z0-9_.-]*)*\.kt$) and reject symlinks via os.path.islink.
  3. Prompt isolation & PR metadata sanitization (create_pr.py)

    • Separate system_instruction from <issue_content>-sanitized user messages across all 4 Gemini API calls (matching triage_issue.py and answer_issue.py).
    • Validate commit_message and pr_title as single-line strings without leading - flags.
    • Validate outbound URLs in pr_body against ALLOWED_DOMAINS, wrap @mentions in code spans so googlemaps-bot cannot be used to mass-ping users/teams, and rewrite cross-issue closing keywords (Fixes #999 -> Fixes issue 999 when 999 != issue_number).
  4. Operational & hygiene fixes

    • Scope list_project_files() to editable .kt files under EDITABLE_SOURCE_DIRS, cap modified/created files per run (MAX_FILES_PER_RUN = 10), add timeout=60 to urllib.request.urlopen, and abort if changed_files is empty.
    • Add a concurrency: group to auto-fix.yml and support idempotent re-runs via git push --force-with-lease after verifying [[ "$BRANCH_NAME" =~ ^auto-fix/issue-[0-9]+$ ]].
    • Remove the accidentally committed .github/scripts/__pycache__/create_pr.cpython-315.pyc binary and add __pycache__/ and *.py[cod] to .gitignore and check_agent_leftovers.py.

kikoso and others added 2 commits November 12, 2026 20:40
create_pr.py now only writes .kt files under the library and demo source
directories, derives the branch name from the issue number and records the
files it changed. The workflow checks out without persisted credentials,
commits only those files and authenticates just for the push.
…cope

- Restrict EDITABLE_SOURCE_DIRS to src/main/ (excluding test source sets and generated baseline profiles) and enforce a strict character allowlist regex on relative paths so embedded newlines cannot split mapfile in auto-fix.yml.
- Scope list_project_files() to editable .kt files, cap modified/created files per run (MAX_FILES_PER_RUN = 10), add HTTP request timeouts, and abort if no files were changed.
- Separate system_instruction from <issue_content>-delimited user input across all Gemini API calls and sanitize PR metadata (single-line commit/title, approved link domains, code-wrapped @mentions, and same-issue closing keywords).
- Use GITHUB_TOKEN instead of SYNCED_GITHUB_TOKEN_REPO and create auto-fix PRs as drafts so downstream pull_request workflows do not execute unreviewed AI-generated code with repository secrets.
- Remove committed create_pr.cpython-315.pyc bytecode file and block __pycache__/ and *.py[cod] in .gitignore and check_agent_leftovers.py.
@dkhawk
dkhawk requested a review from kikoso November 13, 2026 17:17
@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

Coverage (unit tests)

Compared against 126ea04 on main (#1085).

Module Line % Change Branch % Change
maps-compose 15.27% no change 3.75% no change
maps-compose-utils 26.29% no change 12.70% no change
maps-compose-widgets 3.87% no change 0.00% no change
TOTAL 17.35% no change 5.46% no change

Line and branch coverage from unit test reports. History is recorded on the coverage-history branch after each merge to main.

@dkhawk
dkhawk marked this pull request as ready for review November 13, 2026 17:29
@dkhawk
dkhawk requested a review from a team as a code owner November 13, 2026 17:29
@googlemaps-bot

googlemaps-bot commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor

Coverage (instrumentation tests)

Overall Project 45.8% 🍏

There is no coverage information present for the Files changed

Base automatically changed from ci/harden-auto-fix to main November 16, 2026 17:20
…llowups-merge

# Conflicts:
#	.github/scripts/create_pr.py
#	.github/workflows/auto-fix.yml
Comment thread .github/scripts/create_pr.py Outdated
Comment on lines +99 to +102
return (
text.replace("</issue_content>", "&lt;/issue_content&gt;")
.replace("<issue_content>", "&lt;issue_content&gt;")
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only catches the exact lowercase tags, so </Issue_Content> or </issue_content > would still close the block from the model's point of view. Could we neutralise them with a case-insensitive regex?

Suggested change
return (
text.replace("</issue_content>", "&lt;/issue_content&gt;")
.replace("<issue_content>", "&lt;issue_content&gt;")
)
return re.sub(
r"<\s*/?\s*issue_content\b[^>]*>",
lambda m: m.group(0).replace("<", "&lt;").replace(">", "&gt;"),
text,
flags=re.IGNORECASE,
)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! Updated sanitize_content() in 21b891a to use a case-insensitive regex (r"<\s*/?\s*issue_content\b[^>]*>?") so mixed-case, spaced, attributed, or unclosed delimiter tags are all neutralized.

Comment thread .github/workflows/auto-fix.yml Outdated
--base main

git fetch origin "$BRANCH_NAME" 2>/dev/null || true
git push --force-with-lease origin "$BRANCH_NAME"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the fetch above updates origin/$BRANCH_NAME, the lease will always match and this behaves like a plain --force. If a maintainer pushed commits on top of a previous auto-fix PR, re-labelling the issue would wipe them out. Should we bail out when the remote branch already exists?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed — bailing out is much safer so we never wipe out commits a maintainer pushed onto an existing auto-fix branch. Added an early Check for Existing Auto-Fix Branch guard right after checkout in 21b891a (so we also avoid burning Gemini/Gradle quota on re-labels) plus a git ls-remote check right before a plain git push origin "$BRANCH_NAME".

Comment thread .github/workflows/auto-fix.yml Outdated
# Add a comment to the issue
gh issue comment "$ISSUE_NUMBER" --body "🚀 The **Auto-Fix Agent** has successfully generated a patch and created a Pull Request to address this issue! Please review the proposed changes."
gh issue comment "$ISSUE_NUMBER" --body "🚀 The **Auto-Fix Agent** has generated a patch and opened a draft Pull Request to address this issue. A maintainer will review the proposed changes before running full CI."

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity, how do we run the CI afterwards? Since the PR is opened with GITHUB_TOKEN, nothing triggers on opened, and marking it as ready for review only starts instrumentation-test.yml (test.yml and lint-report.yml don't listen to ready_for_review). Would it make sense to mention in the comment that a maintainer needs to push or close and reopen it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point! Captured PR_URL from gh pr create in 21b891a and updated the issue comment to link directly to the draft PR and explain that a maintainer should close and reopen the PR (or push a commit to $BRANCH_NAME) and mark it ready for review to trigger the full CI suite.

@kikoso kikoso left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I merged main to fix the conflicts after #1087. Minor comments @dkhawk , but looks good!

kikoso and others added 3 commits November 17, 2026 11:58
- Use a case-insensitive regex in sanitize_content() to neutralize mixed-case, spaced, attributed, or unclosed <issue_content> delimiter tags.
- Bail out if auto-fix/issue-<N> already exists on origin (both right after checkout and before git push) instead of force-pushing over potential maintainer commits.
- Include the draft PR URL and instructions to close/reopen (or push a commit) and mark ready for review to trigger the full CI suite.
@dkhawk
dkhawk merged commit 4fa8784 into main Nov 17, 2026
14 checks passed
@dkhawk
dkhawk deleted the ci/harden-auto-fix-followups branch November 17, 2026 21:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants