Skip to content

fix(waf-detect): an unclosed <code> stops hiding wording at its block's end #904 - #916

Merged
nc9 merged 1 commit into
mainfrom
fix/904-waf-unclosed-code
Oct 17, 2026
Merged

nc9 merged 1 commit into
mainfrom
fix/904-waf-unclosed-code

Conversation

@nc9

@nc9 nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Closes #904

Thank you @WilliamK112 for #905, which tackled the same problem and helped confirm the fix was needed. This PR takes a slightly different route so that code blocks built from nested blocks, such as Expressive Code and Starlight, keep their lines treated as quoted.

What changed

signatureViews in packages/waf-detect/src/views.ts drops the text inside <code>, <kbd> and <samp> from the text view. Before this change, an unclosed <code> hid every word after it in the sample. The browser instead closes it at the end of its block. Now:

  • a block-level end tag (</p>, </div>, </pre>, </li>, </td>, </body> and the like) ends any quote still open inside that block;
  • a block opened inside the quote is counted. Its own end tag closes only that block, not the quote around it. Without this, a code block that wraps each line in a <div>, as Expressive Code (Starlight) does, would expose its quoted lines to the text view.

Tests

packages/waf-detect/tests/content-signatures.test.ts, 155/155:

  • a stray <code> before an Imperva incident line: attributed to Imperva
  • an unclosed <kbd> in a list item before a Cloudflare ray id: attributed to Cloudflare
  • a properly closed <pre><code> quoting the incident line: not attributed
  • <pre><code><div>log</div>…incident line…</code></pre>: not attributed
  • an Expressive Code block (<figure><pre><code><div class="ec-line">…</div></code></pre></figure>) quoting an incident line and a ray id: not attributed. It fails if the nested-block counter is removed.

Typecheck (waf-detect) and oxlint on the changed files pass. CHANGELOG entry under Unreleased. Skill: no change needed.

@claude

claude Bot commented Oct 17, 2026

Copy link
Copy Markdown

Reviewed the diff in views.ts and the new tests. I didn't run the tests. The approach is sound: the block-end-closes-quote rule matches browser behaviour, and the nested-block counter handles Expressive Code correctly. Findings:

  • (low) views.ts ~L130: blocksInQuote counts block start tags that the HTML parser closes implicitly (<p>, <li>, <td>, <tr>, <dd>, <dt>). For example, <li><kbd>x<p>y</li><li>Cloudflare Ray ID… opens <p> (count 1), then </li> only decrements the count. The quote stays open and hides the rest of the page, which is the original bug in a narrower form. This is a rare edge case and a failure to detect is the safe direction. If you want to cover it, an end tag could unwind the counter to the matching depth instead of decrementing by one.
  • (low) views.ts ~L130: a self-closing <div/> inside a quote is counted as an opened block. Since /> is ignored for HTML elements, that matches the browser, so it's fine. I'm only noting it as intended.
  • (nit) Tests: there is no case for an unclosed <code> that contains an implicitly closed <p> or <li>. Adding one would document the limitation above.
  • (nit) The QUOTE_CLOSING_BLOCKS list omits tbody, thead, tfoot, caption, fieldset, details, summary and center. The list is fine as a heuristic, but it could be completed.

The linear-scan guarantee is preserved, since the change is O(1) per tag. I found no security concerns. The CHANGELOG entry is clear.

@nc9
nc9 merged commit 89038cd into main Oct 17, 2026
15 checks passed
@nc9
nc9 deleted the fix/904-waf-unclosed-code branch October 17, 2026 09:13
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.

waf-detect: an unclosed <code> hides the rest of the page from block-page wording

1 participant