Skip to content

fix(#919): track heredoc boundaries in the URL-host-in-comment guard - #978

Merged
fdaviddpt merged 2 commits into
mainfrom
fix-919
Oct 8, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix-919

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

_check_url_in_comment flagged any #-prefixed line matching URL_HOST with zero awareness of shell heredoc boundaries, unlike the same file's own _in_code() helper, which already tracks fenced-code state for a sibling markdown check. A #-led line inside a typed <<DELIM ... DELIM heredoc body that happens to name a URL-shaped host would be misread as a real shell comment.

Adds _HEREDOC_OPEN, a heredoc-opener regex modeled on the file's existing TYPED_HEREDOC/_mask_arithmetic pair, and tracks the open heredoc delimiter inside _check_url_in_comment, skipping lines between the opener and the matching closer.

Closes #919

Self-review findings

Two reviewers (Explore and oss:auditor), spawned concurrently against the primary fix, independently found and reproduced the same underlying mechanism: the heredoc-opener regex also matches a bitwise left-shift against a named operand and a C++-style stream-insertion operator, neither of which is a heredoc, and since _check_url_in_comment runs over every shipped text file (not just shell scripts), a bare x << NAME line in any shipped file silently disabled the URL-host guard for the rest of that file.

  • Fixed: heredoc-delimiter tracking is now scoped to hooks/hooks.d/scripts, the same directories _check_typed_heredoc is already scoped to, since a heredoc cannot exist anywhere else.
  • Fixed: the closing-delimiter match used line.strip() == delim, stripping both leading and trailing whitespace unconditionally; real bash only tab-strips for <<-DELIM, so a plain <<DELIM heredoc now requires an exact, unstripped match.
  • Fixed: the docstring now documents both the directory-scoping and the whitespace-exactness behavior.
  • Below the bar, not fixed: multiple heredocs opened on one line (cmd <<A <<B) are only partially tracked -- _HEREDOC_OPEN.search() finds only the first opener, so the second heredoc's body is never registered as heredoc content. This is a rare shell idiom not used anywhere in this repo's own shipped scripts today, and fixing it properly would require tracking a queue of pending delimiters rather than a single one, adding meaningfully more complexity to a guard whose only job is a heuristic comment-vs-content distinction, not a full shell parser -- the file's own stated design philosophy throughout (e.g. _check_catch_all_in_loop's docstring: "intentionally the same coarse, line-oriented heuristic the real portal scanner itself is, not a bash parser, rather than a tighter check").

Adjacent finding (not fixed here, filing left to maintainer)

11 other checker functions in the same file share the same lstrip().startswith("#")-as-sole-comment-guard pattern with no heredoc-boundary tracking -- the same latent class #919 itself named, just not caught anywhere else in the file. Out of scope for #919, which names only _check_url_in_comment; all are REVIEW-severity per their own docstrings, not FAIL.

Tests

Two new tests pin the primary fix (heredoc-body skip, plus a positive control for the same line outside any heredoc in the same file). Four new tests pin the follow-up fix (bitshift/stream-insertion false-negative guard, loose-closer false negative, and two positive controls). Full file: 83 passed.

🤖 Generated with Claude Code

[AI-generated]

fdaviddpt and others added 2 commits October 8, 2026 01:33
`_check_url_in_comment` flagged any `#`-prefixed line whose text matched
URL_HOST, with no awareness of whether that line sat inside a typed
`<<DELIM ... DELIM` heredoc body. A heredoc-content line that happens to
start with `#` and names a URL-shaped host is not a real shell comment, the
same distinction `_in_code()` already draws for fenced markdown code blocks.

Adds `_HEREDOC_OPEN`, a heredoc-opener regex modeled on TYPED_HEREDOC/
_mask_arithmetic, and tracks the open delimiter inside `_check_url_in_comment`
so lines between the opener and the matching closing delimiter are skipped.

Not live today (no shipped heredoc currently holds such a line), but
confirmed latent at HEAD per #919's own re-check.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
…match

Self-review findings (both the Explore reviewer and oss:auditor independently
reproduced the same mechanism):

1. The heredoc-opener regex (`<< NAME`) also matches a bitwise left-shift
   against a named operand and a C++-style stream-insertion operator, neither
   of which is a heredoc. `_check_url_in_comment` runs over every shipped text
   file (not just shell scripts), so a bare `x << NAME` line in e.g. a shipped
   `.py` file silently disabled the URL-host guard for the rest of that file.
   Heredoc-delimiter tracking is now scoped to hooks/hooks.d/scripts, the same
   directories `_check_typed_heredoc` is already scoped to, since a heredoc
   cannot exist anywhere else.

2. The closing-delimiter match used `line.strip() == delim`, which strips both
   leading and trailing whitespace of any kind. Real bash only tolerates
   leading-tab stripping for `<<-DELIM`; a plain `<<DELIM` heredoc requires an
   exact bare-delimiter line. The looser check closed a plain heredoc one line
   early on an indented look-alike and then read genuine heredoc body as
   ordinary file content. Now tracks whether the opener used `<<-` and only
   tab-strips in that case.

Four new tests pin both fixes, with the bitshift one and the loose-closer one
confirmed red against the prior state (2 failed, 1 passed) before this commit.

Co-Authored-By: Claude Sonnet 5 <[email protected]>
@fdaviddpt
fdaviddpt merged commit 7fb764e into main Oct 8, 2026
19 checks passed
@fdaviddpt
fdaviddpt deleted the fix-919 branch October 8, 2026 00:34
fdaviddpt added a commit that referenced this pull request Oct 8, 2026
…e URL-host guard (#984)

* fix(#981): skip comment lines and quoted text when tracking heredocs in the URL-host guard

`_check_url_in_comment`'s heredoc-open tracking (#919/#978) ran `_HEREDOC_OPEN`
against every line, including `#`-comment lines -- bash never opens a
heredoc from inside a comment, so a comment line that merely mentions
`<<WORD` could leave the tracker "open" for the rest of the file and
silently disable the URL-host-in-comment guard past that point. The
sibling `_check_typed_heredoc` already skipped comment lines for exactly
this reason; this guard's own tracking did not.

Also address the addendum reported on the same issue: a `<<WORD`-shaped
substring sitting inside an ordinary quoted string (not a real heredoc
opener) no longer opens the tracker either, via a naive quote-toggle scan
(`_in_quotes_before`) that stays conservative in the direction that
matters here -- under-detecting a real opener is safe for this tracker,
since a false positive is what silently swallows the rest of the file. An
opener that is never closed is now reported as an offense instead of
silently skipping everything after it.

Add tests pinning both failure modes (and their positive controls) to
tests/test_release_branch_check_898.py, alongside the existing heredoc-
tracking tests for this same function.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* fix(#981): make the quote-awareness scan match complete spans, not a naive toggle

Self-review finding (Explore reviewer): the first version of `_in_quotes_before`
toggled on any single/double-quote character with no pairing check, so a lone,
unmatched apostrophe from an ordinary English contraction ("it's", "don't")
earlier on a line could flip the toggle on and never off -- misreading a REAL
heredoc opener later on the same line as "inside quotes" and silently skipping
it. That is exactly the failure direction #981 set out to eliminate, just
reached through a different trigger.

Rewritten to match only COMPLETE quoted spans (both quote characters present)
via a regex scan, so a lone apostrophe forms no span and cannot swallow a real
opener. Added a regression test
(test_an_unmatched_apostrophe_before_a_real_opener_does_not_suppress_it) pinning
the fixed shape.

Logged a related, lower-severity, pre-existing limitation surfaced by the same
self-review round (the auditor spawn) to trap.d/981.heredoc-quote-scan-is-
escape-blind.md rather than fixing it here: the complete-span match has no
backslash-escape awareness, so a backslash-escaped quote inside a real quoted
string can still close a span one character early. Confirmed this is NOT a
regression from this commit's own rewrite -- the original naive-toggle version
produced the identical wrong answer on the same reproduction. Ranked
non-blocking (misreports) against the release ranking table; not exercised by
any file currently shipped in this repo.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
fdaviddpt added a commit that referenced this pull request Oct 8, 2026
…e shared heredoc tracker (#988)

* fix(#980): route 16 comment-detecting checkers in check_release_tree.py through a shared heredoc tracker

Adjacent finding from #919's lane self-review (PR #978): #978 fixed one
checker (_check_url_in_comment) that decided "is this a comment" with the
bare `.lstrip().startswith("#")` test, with no heredoc-boundary tracking, so
a heredoc body line could be misread as a comment. 16 other functions in
the same file shared the exact same unguarded pattern (18 raw call sites,
the two extra folding into `_check_dot_string` and `_sh_lines`).

Added `_iter_non_heredoc_lines`, generalizing the tracker
`_check_url_in_comment` already carries, and routed all 16 checkers through
it -- 8 of them share the existing `_sh_lines` helper, fixed once for all 8.
`_check_typed_heredoc` itself was a self-referential case: without tracking
the heredoc bodies it finds, a body line containing a second
"<<WORD"-shaped substring was misread as an independent second opener.

Added tests/test_heredoc_comment_guard_980.py: one must-not-fire case per
class of fix (shared _sh_lines helper, the self-referential heredoc
detector, check_tree's own unscoped eval/download REVIEW loop, and the
_SCRIPT_DIRS-scoped-not-.sh-restricted checkers), each paired with a
positive control proving the same pattern still fires outside a heredoc.

Co-Authored-By: Claude Sonnet 5 <[email protected]>

* fix(#980): close self-review findings -- unclosed-heredoc gap and two deferred functions

Self-review (Explore reviewer) on the preceding #980 commit found two real
gaps:

1. `_iter_non_heredoc_lines` has no "the heredoc opened here was never
   closed" diagnostic of its own, unlike `_check_url_in_comment`'s own
   hand-rolled copy of this same tracking loop. An unclosed/malformed
   `<<DELIM` silently blinds all 16 routed checkers to the rest of the
   file. Verified this is mitigated, not masked: `check_tree` always runs
   the unchanged `_check_url_in_comment` on every file too, with the exact
   same `track_heredocs` scope, so a genuinely unclosed heredoc in a
   reachable file still trips that function's own FAIL in the same run.
   Documented this coupling in the helper's docstring and added a
   regression test pinning the guarantee, rather than threading an extra
   parameter through 24 call sites to duplicate the diagnostic everywhere.

2. The two functions sharing the same unguarded comment pattern that were
   left out of the #980 fix (`_check_catch_all_in_loop`,
   `_check_single_line_delegate_positional`) had neither a fix nor
   in-code documentation of why -- #980 as scoped asked for one or the
   other for every checker sharing the pattern. Added a docstring note to
   each explaining why it needs a different approach (its own multi-line
   state machine; filtering an already-extracted function-body string
   rather than the raw file) and that it is deferred to a follow-up, not
   silently dropped.

Updated changelog.d/980.fixed.md to describe both outcomes accurately
(16 of 18 routed, not "every one"; the unclosed-heredoc mitigation).

Co-Authored-By: Claude Sonnet 5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5 <[email protected]>
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.

_check_url_in_comment has no heredoc-boundary tracking -- a URL-shaped host inside a typed heredoc body would misread as a comment

1 participant