Skip to content

fix(waf-detect): recover from unclosed quote elements - #905

Closed
WilliamK112 wants to merge 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/waf-code-boundary-904
Closed

WilliamK112 wants to merge 1 commit into
squirrelscan:mainfrom
WilliamK112:codex/waf-code-boundary-904

Conversation

@WilliamK112

Copy link
Copy Markdown
Contributor

Summary

  • stop malformed code, kbd, and samp quote regions at closing block boundaries
  • keep properly closed quoted block-page wording excluded
  • cover all three quote elements across representative block boundaries

Testing

  • npm exec --yes --cache /tmp/squirrelscan-904-npm-cache [email protected] -- bun test packages/waf-detect/tests
  • npm exec --yes --cache /tmp/squirrelscan-904-npm-cache --package=@typescript/[email protected] -- tsgo -p packages/waf-detect/tsconfig.json --noEmit

Closes #904

@nc9

nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for picking this up, and for the clear boundary list. One change is needed before we can merge.

A block opened inside the quote now ends the quote. The reset fires on any closing boundary tag. A </div> that closes a <div> opened inside the <code> therefore ends the quote too. Several syntax highlighters wrap each line of a code block in a div. Expressive Code (Astro Starlight) is one: <pre><code><div class="ec-line">...</div></code></pre>. A docs page that quotes a block-page line in one of those code blocks becomes visible to the wording signatures again, and that is the case #693 guarded against. Example:

<pre><code><div class="ec-line"><div class="code">curl -I x</div></div><div class="ec-line"><div class="code">Request unsuccessful. Incapsula incident ID: 1234000280151781213-1234</div></div></code></pre>

On main, signatureViews(...).text does not contain the incident line. With this PR it does.

The HTML parser only closes an open <code> when the block that contains it ends, not when a block nested inside it ends. Suggested shape:

  • While a quote is open, count boundary blocks opened inside it.
  • A closing boundary tag first decrements that count.
  • Only a closing boundary tag with the count at zero resets quoteDepth.
  • Reset the count when the quote closes normally.

Please add a test with the nested-div code block above. It should still not be attributed to Imperva.

nc9 added a commit that referenced this pull request Oct 17, 2026
…'s end #904 (#916)

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: n
@nc9

nc9 commented Oct 17, 2026

Copy link
Copy Markdown
Contributor

Thanks for taking this on, @WilliamK112, and for the clear tests. While reviewing we found that resetting on any closing block tag lets a block opened inside the code (the per-line divs Expressive Code and Starlight emit) end the quote early, so we landed a fix that counts blocks opened inside the quote (#916) and are closing this one as superseded. The reviewer's notes on the edge case are in the thread above. Your other PRs are being merged separately.

@nc9 nc9 closed this Oct 17, 2026
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

2 participants