Skip to content

fix(cloud): \Z-anchor validators, register anchor contract (#12128) - #12161

Merged
iamwhatever merged 1 commit into
mainfrom
fix/cloud-regex-anchor-class-12128
Sep 21, 2026
Merged

iamwhatever merged 1 commit into
mainfrom
fix/cloud-regex-anchor-class-12128

Conversation

@NicholasRBowers

@NicholasRBowers NicholasRBowers commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Problem / Motivation

Every regex validator under src/kiro_crew/cloud/ was anchored with $. In
Python, $ matches just before one trailing newline, so each of these
validators used with .match accepts its own valid input with "\n"
appended. A cloud tag of my-crew\n, a CIDR of 10.0.0.0/16\n, or a git ref
of main\n all pass validation that was written to refuse them. The sharpest
case: _REPO_RE and _REF_RE guard values that reach a
git clone --branch '<ref>' '<repo>' line in EC2 instance UserData, where the
comment above them promises charset validation keeps crafted values inside
their quotes.

Why it matters

These are request-path validators on security-sensitive sinks (subprocess
argv, IAM role names, CloudFormation parameters). The bound is small — exactly
one trailing newline, no arbitrary suffix — but the repo has an established
\Z convention for this class and a contract test built to stop it drifting
back, and cloud/ was not registered with that guard. This PR's scope is
src/kiro_crew/cloud/ only, per #12128: other packages still carry
$-anchored patterns and are separate work — the contract test's
_REQUEST_PATH_PACKAGES registry is the extension point for converting and
guarding them package by package.

What changed (motivation → approach → change)

Cloud's input validators now genuinely refuse a value with a trailing newline,
and the contract test that guards this defect class now covers cloud/ so the
bug cannot drift back — the fix is the mechanical anchor conversion plus that
registration.

test/test_regex_anchor_contract.py already guards the class two ways —
behaviorally (every registered validator must reject valid-input+"\n") and
structurally (a package walk flags any module-level pattern carrying an
unescaped $) — but its package registry only listed the Papyrus backend,
which is why cloud/ escaped it.

All 19 remaining $ anchors under src/kiro_crew/cloud/ become \Z: the
issue enumerated 18, plus _HOSTNAME_RE (login_target.py) and
_SECRET_NAME_RE (fargate/identity.py), whose multi-line re.compile calls
a one-line grep misses; the 20th, _USERNAME_RE in ssm.py, landed on main
via #12106 with the same \Z spelling, so this diff no longer touches that
file. The literal $ inside _CONTROL_OR_SHELL_RE's
character class is spelled \$ (a semantic no-op inside [...]) so the
structural walk's documented heuristic does not flag it, with comments on both
sides recording why that escape is load-bearing.

kiro_crew.cloud is registered in _REQUEST_PATH_PACKAGES, and the contract
test's package walk is now recursive (pkgutil.walk_packages), so
cloud/fargate/ and any sub-package added under a registered root later are
covered without editing the test. Each canonical validator gains a
_VALIDATORS entry — including ec2._PROFILE_SPEC.pattern, which is aliased
from deploy/profiles.py and would otherwise stay outside both halves of the
guard. The test's docstring distinguishes patterns matched raw (directly
tightened) from those routed through validation.validate_field, whose
boundary strips before matching, making \Z defense-in-depth there. All cloud
modules were verified import-safe for the walk (no client construction,
credentials, network, or filesystem effects at import).

Backwards compatibility

Breaking: values carrying exactly one trailing newline are now refused by these
cloud validators (19 in this diff, plus ssm.py's _USERNAME_RE via #12106) —
that refusal is the fix. Writer sweep on
origin/main @ 653f44b7a: no caller can legitimately supply one.
CloudConfig.load reads JSON (json.loads strips nothing into values;
a stored tag was validated before save), wizard/CLI inputs are stripped or
argv-shaped, normalize_start_url lowercases and strips before matching, and
no .read() call feeds any of these patterns. The CloudFormation
AllowedPattern mirrors are full-string matches and unaffected.

Tests

  • test_regex_anchor_contract.py: 21 new behavioral entries, each locking in
    that the validator accepts its canonical input and rejects it with "\n"
    appended; the structural walk now covers every module under cloud/ and
    cloud/fargate/, so a future $-anchored cloud validator fails without
    editing the test; the walk-guard pins ec2 and fargate.identity as
    reached. Reverting the production hunks makes 21 tests fail (verified).
  • test_cloud_ec2.py / test_cloud_config.py: the two assertions pinning
    _TAG_RE's exact pattern string updated to the \Z spelling; their
    purpose (the {1,51} IAM cap lockstep) is unchanged.

Manual verification

N/A — unit coverage sufficient: the change is regex-anchor spelling with the
accept/reject behavior locked by the contract test; full backend suite, isort,
flake8, mypy, and the black/encoding gates all pass locally.

Related Issues

Fixes #12128

Pattern harvest

Rule candidate: agents-md
Pattern: "$-anchored validator on a request path — anchor with \Z and register
the package in test_regex_anchor_contract.py; the contract test's registry, not
per-validator tests, is what stops the class drifting back"

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff

@NicholasRBowers
NicholasRBowers requested review from a team and chenmingwei23 September 20, 2026 02:45
@github-actions github-actions Bot added the readiness: checking Automated validation is still running label Sep 20, 2026
@NicholasRBowers

Copy link
Copy Markdown
Contributor Author

Intent: Make every request-path validator under src/kiro_crew/cloud/ reject a value carrying a trailing newline, and register the cloud packages with the existing anchor-contract test so the defect class cannot silently return.
Not a goal: Auditing downstream call sites for where a trailing \n would have changed behavior, or converting $ anchors anywhere outside src/kiro_crew/cloud/.

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Design Review (Fable 5) — ✅ PASS

Design-level review of 5b17789671bd83b5c5408854b80da6a893509b50 — updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

Design-Verdict: PASS

Verify the writer-sweep claim that no legitimate caller feeds a newline-carrying value to these validators — it is the sole breaking-behavior premise.

[DESIGN-REVIEWED] 5b17789

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

GPT 5.6 Review — ✅ no blocking findings

GPT 5.6 completed its review of 5b17789671bd83b5c5408854b80da6a893509b50 and found no blocking issues.

This comment is updated in place on each push.

Review details

No findings.
[GPT-REVIEWED] 5b17789

False positive or not applicable? A repository writer can comment:
/ai-review override gpt 5b17789671bd83b5c5408854b80da6a893509b50: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Opus 4.8 Review — ✅ no blocking findings

Reviewed 5b17789671bd83b5c5408854b80da6a893509b50 — this comment is updated in place on each push.

Review details

I've verified the full picture. This PR replaces $ with \Z across cloud validators — strictly stricter (or no-op) anchoring, since Python's $ matches before a trailing newline. My checks:

  • No re.MULTILINE anywhere in cloud, so $→\Z never changes multi-line semantics.
  • login_target._HOSTNAME_RE/_REGION_RE use .fullmatch (equivalent under trailing newline); _CONTROL_OR_SHELL_RE's $→\$ inside [...] is byte-identical, and _UNESCAPED_DOLLAR's (?<!\\) lookbehind correctly skips the escaped \$.
  • Every validator newly registered in _VALIDATORS (_PROFILE_SPEC via AWS_PROFILE_NAME_PATTERN, ssm._USERNAME_RE) is already \Z-anchored, so the new trailing-newline-rejected cases pass.
  • The only residual $ in a cloud Python regex is the escaped one; all other $-endings are in CloudFormation YAML templates, which the structural walk (filtered to re.Pattern instances) does not inspect.

No regression: none of these validators (tags, regions, CIDRs, ARNs, digests) legitimately carries a trailing newline, and the FieldSpec.pattern entries are sanitize_string()-stripped before matching. The candidate list's conclusion holds under independent re-derivation, and Step 2 surfaced nothing groundable to the 80+ bar.

No findings.

[OPUS-REVIEWED] 5b17789

Verdict parsed from the review's SHA-scoped output markers for commit 5b17789671bd83b5c5408854b80da6a893509b50.

False positive or not applicable? A repository writer can comment:
/ai-review override fable 5b17789671bd83b5c5408854b80da6a893509b50: <one-sentence reason>

@github-actions

github-actions Bot commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

First Principles Review (Fable 5) — ✅ PASS

Premise-level review of 5b17789671bd83b5c5408854b80da6a893509b50 — why this exists and whether the shipped surface is the smallest honest version. Updated in place on each push. A BLOCK verdict blocks PR readiness; PASS/CONCERNS are advisory.

First-Principles-Verdict: PASS

Fix guards cloud/ only by declared scope — confirm #12128 tracks the 144 other files still carrying $-anchored compiled patterns (306 occurrences counted).

What this change ships

Inventory (9 items) — 9 justified

Intent: FIX — cloud input validators genuinely refuse trailing-newline values reaching subprocess/IAM/CloudFormation sinks, and the anchor-contract test now covers cloud/ (#12128).

  1. Cloud deploy inputs (tag, region, CIDR, repo, ref, profile, subnet id) with a trailing newline now refused — justified
  2. Fargate identity/secret/ARN and tag-value validators likewise refuse a trailing newline — justified
  3. Login-target region and hostname validators likewise refuse a trailing newline — justified
  4. The anchor-contract test now guards cloud/ via _REQUEST_PATH_PACKAGES registration — justified
  5. The contract test's package walk is now recursive, covering cloud/fargate/ and future sub-packages — justified
  6. 21 behavioral accept/reject entries lock each cloud validator's boundary — justified
  7. _CONTROL_OR_SHELL_RE's literal $ respelled \$ with comments explaining the load-bearing escape — justified
  8. Contract-test docstring distinguishes raw-matched patterns from sanitize-routed defense-in-depth ones — justified
  9. Two _TAG_RE pattern-string assertions updated to the \Z spelling — justified

[FIRST-PRINCIPLES-REVIEWED] 5b17789

@github-actions github-actions Bot added readiness: action required A blocking check or review needs attention and removed readiness: checking Automated validation is still running labels Sep 20, 2026
@NicholasRBowers
NicholasRBowers force-pushed the fix/cloud-regex-anchor-class-12128 branch from 8f4325f to 9b2e6c4 Compare September 20, 2026 02:56
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 20, 2026
@NicholasRBowers

Copy link
Copy Markdown
Contributor Author
  • span=4aa454e34de0 fixed — the description's scope claim was corrected, not the code.

The finding is right: the body claimed the repo had "already fixed this class everywhere else", and a grep for $-anchored compiled patterns outside cloud/ finds them in 131 files, so the claim was false.
The PR description now states this fix covers src/kiro_crew/cloud/ only (the #12128 scope) and that other packages still carry $-anchored patterns as separate work, with _REQUEST_PATH_PACKAGES named as the extension point for guarding them package by package.
Converting the siblings here would widen the diff far beyond the issue and its review; the contract-test registry is the mechanism that makes each follow-up conversion durable.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge and removed readiness: passed Eligible automated validation passed for the current revision readiness: checking Automated validation is still running labels Sep 20, 2026
@NicholasRBowers
NicholasRBowers force-pushed the fix/cloud-regex-anchor-class-12128 branch from 9b2e6c4 to 123bc8a Compare September 21, 2026 03:56
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: passed Eligible automated validation passed for the current revision merge conflict Branch has merge conflicts with its base — author must resolve before merge readiness: checking Automated validation is still running labels Sep 21, 2026
@NicholasRBowers
NicholasRBowers force-pushed the fix/cloud-regex-anchor-class-12128 branch from 123bc8a to 289ff8e Compare September 21, 2026 06:33
@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: action required A blocking check or review needs attention and removed readiness: passed Eligible automated validation passed for the current revision readiness: checking Automated validation is still running labels Sep 21, 2026
Python's $ anchor matches before one trailing newline, so every
$-anchored cloud validator used with .match accepted its own valid
input plus "\n" -- including _REPO_RE/_REF_RE, whose values reach a
git clone argv in EC2 UserData behind a charset guard.

Convert all 20 $ anchors under src/kiro_crew/cloud/ to \Z (18 from
the issue enumeration plus _HOSTNAME_RE and _SECRET_NAME_RE, whose
multi-line re.compile calls the issue's one-line grep missed), and
escape the literal $ inside _CONTROL_OR_SHELL_RE's character class
so the contract walk's documented heuristic does not flag it.

Register kiro_crew.cloud and kiro_crew.cloud.fargate in the anchor
contract test (pkgutil.iter_modules walks direct children only, so
the sub-package registers alongside its parent) and add each
canonical validator to _VALIDATORS, locking the class against
drift-back. Update the two tests pinning _TAG_RE's exact pattern
string to the new spelling.

Closes #12128
@NicholasRBowers
NicholasRBowers force-pushed the fix/cloud-regex-anchor-class-12128 branch from 289ff8e to 5b17789 Compare September 21, 2026 07:09
@NicholasRBowers

Copy link
Copy Markdown
Contributor Author

Rebased: main's 4e8af7b6b (#12381) broke mypy on aws_control/backend/backup.py in the merge-ref window this head's CI pinned; main fixed it in 1dd5a03eb 30 minutes later. Rebase-only push 289ff8ec2 -> 5b1778967, diff unchanged; CI re-running.

@github-actions github-actions Bot added readiness: checking Automated validation is still running readiness: passed Eligible automated validation passed for the current revision and removed readiness: action required A blocking check or review needs attention readiness: checking Automated validation is still running labels Sep 21, 2026

@iamwhatever iamwhatever left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Reviewed the diff: all 19 \Z conversions present, no $-anchored pattern left under cloud/, contract test covers fargate via walk_packages; tightening only.

@iamwhatever
iamwhatever merged commit 7517bc1 into main Sep 21, 2026
93 checks passed
@iamwhatever
iamwhatever deleted the fix/cloud-regex-anchor-class-12128 branch September 21, 2026 17:23
@github-actions github-actions Bot removed the readiness: passed Eligible automated validation passed for the current revision label Sep 21, 2026
amadsalmon pushed a commit to amadsalmon/KiroCrew that referenced this pull request Sep 22, 2026
…v#12128) (kirodotdev#12161)

Python's $ anchor matches before one trailing newline, so every
$-anchored cloud validator used with .match accepted its own valid
input plus "\n" -- including _REPO_RE/_REF_RE, whose values reach a
git clone argv in EC2 UserData behind a charset guard.

Convert all 20 $ anchors under src/kiro_crew/cloud/ to \Z (18 from
the issue enumeration plus _HOSTNAME_RE and _SECRET_NAME_RE, whose
multi-line re.compile calls the issue's one-line grep missed), and
escape the literal $ inside _CONTROL_OR_SHELL_RE's character class
so the contract walk's documented heuristic does not flag it.

Register kiro_crew.cloud and kiro_crew.cloud.fargate in the anchor
contract test (pkgutil.iter_modules walks direct children only, so
the sub-package registers alongside its parent) and add each
canonical validator to _VALIDATORS, locking the class against
drift-back. Update the two tests pinning _TAG_RE's exact pattern
string to the new spelling.

Closes kirodotdev#12128

Co-authored-by: Nick Bowers <nrb@amazon.com>
wknight1 pushed a commit to wknight1/KiroCrew that referenced this pull request Oct 5, 2026
…v#12128) (kirodotdev#12161)

Python's $ anchor matches before one trailing newline, so every
$-anchored cloud validator used with .match accepted its own valid
input plus "\n" -- including _REPO_RE/_REF_RE, whose values reach a
git clone argv in EC2 UserData behind a charset guard.

Convert all 20 $ anchors under src/kiro_crew/cloud/ to \Z (18 from
the issue enumeration plus _HOSTNAME_RE and _SECRET_NAME_RE, whose
multi-line re.compile calls the issue's one-line grep missed), and
escape the literal $ inside _CONTROL_OR_SHELL_RE's character class
so the contract walk's documented heuristic does not flag it.

Register kiro_crew.cloud and kiro_crew.cloud.fargate in the anchor
contract test (pkgutil.iter_modules walks direct children only, so
the sub-package registers alongside its parent) and add each
canonical validator to _VALIDATORS, locking the class against
drift-back. Update the two tests pinning _TAG_RE's exact pattern
string to the new spelling.

Closes kirodotdev#12128

Co-authored-by: Nick Bowers <nrb@amazon.com>
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.

cloud: 17 dollar-anchored regexes each admit one trailing newline, including the tag validator launch_state reuses

2 participants