feat(fuzz): Add XSS Context Analyzer (Closes #5838)#7038
feat(fuzz): Add XSS Context Analyzer (Closes #5838)#7038sonumishrAA wants to merge 3 commits intoprojectdiscovery:devfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
WalkthroughAdds an XSS context analyzer that sends fuzzed HTTP requests, detects payload reflections in responses, and classifies the reflection context (HTML text, attribute name/value, script block, comment, or unknown) using HTML tokenization. Includes unit tests for the context detection logic. Changes
Sequence DiagramsequenceDiagram
participant Analyzer as XSS Analyzer
participant HTTPClient as HTTP Client
participant Server as Target Server
participant Parser as HTML Tokenizer
Analyzer->>HTTPClient: Build fuzzed request (inject payload)
HTTPClient->>Server: Send HTTP request
Server-->>HTTPClient: Return HTTP response
HTTPClient-->>Analyzer: Deliver response body
alt payload found in body
Analyzer->>Parser: Tokenize HTML body
Parser-->>Analyzer: Return tokens
Analyzer->>Analyzer: determineContext() -> classify reflection
Analyzer-->>Analyzer: Return (true, context)
else payload not found
Analyzer-->>Analyzer: Return (false, "")
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
pkg/fuzz/analyzers/xss/analyzer.go (1)
35-36: Guardoptions.HttpClientbefore callingDo.A nil
HttpClientwould panic at Line [44]. Add a defensive guard in the upfront validation block.🛡️ Suggested hardening
- if options == nil || options.FuzzGenerated.Component == nil { + if options == nil || options.HttpClient == nil || options.FuzzGenerated.Component == nil { return false, "", nil }Also applies to: 44-45
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/fuzz/analyzers/xss/analyzer.go` around lines 35 - 36, The initial nil-check should also guard options.HttpClient to avoid a panic when calling options.HttpClient.Do; update the validation in the beginning of the function (the block checking options and options.FuzzGenerated.Component) to also check options.HttpClient != nil and return the same false, empty string, nil triple if it's nil so subsequent calls to options.HttpClient.Do are safe.pkg/fuzz/analyzers/xss/analyzer_test.go (1)
19-40: Use thepayloadvariable in fixtures instead of hardcoded literals.Line [10] defines
payload, but Lines [19], [24], [29], [34], and [39] inline"pwned_payload". This can drift if the payload changes.♻️ Proposed refactor
import ( + "fmt" "testing" "github.com/stretchr/testify/require" ) @@ { name: "HTML Text Context", - htmlBody: "<html><body>Hello pwned_payload world</body></html>", + htmlBody: fmt.Sprintf("<html><body>Hello %s world</body></html>", payload), expected: "HTML Text", }, { name: "Script Block Context", - htmlBody: "<html><script>var a = 'pwned_payload';</script></html>", + htmlBody: fmt.Sprintf("<html><script>var a = '%s';</script></html>", payload), expected: "Script Block", }, { name: "Attribute Value Context", - htmlBody: "<input type='text' value='pwned_payload'>", + htmlBody: fmt.Sprintf("<input type='text' value='%s'>", payload), expected: "Attribute Value (input[value])", }, { name: "Attribute Name Context", - htmlBody: "<svg pwned_payload='1'>", + htmlBody: fmt.Sprintf("<svg %s='1'>", payload), expected: "Attribute Name (svg)", }, { name: "HTML Comment Context", - htmlBody: "<!-- pwned_payload -->", // ← fix: payload add kiya + htmlBody: fmt.Sprintf("<!-- %s -->", payload), expected: "HTML Comment", },🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@pkg/fuzz/analyzers/xss/analyzer_test.go` around lines 19 - 40, Tests in analyzer_test.go are embedding the literal "pwned_payload" instead of using the declared payload variable, which will drift if payload changes; update the table-driven fixtures (entries named "HTML Text", "Script Block", "Attribute Value (input[value])", "Attribute Name (svg)", and "HTML Comment") to build their htmlBody using the payload variable (e.g., via string concatenation or fmt.Sprintf) rather than the hardcoded literal so all occurrences reference the single payload constant; ensure the variable name payload is used exactly and that surrounding quotes/markup remain correct for each htmlBody.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@pkg/fuzz/analyzers/xss/analyzer.go`:
- Around line 39-42: The component's current fuzz value isn't being set before
rebuilding, causing stale state: before calling Rebuild() on
options.FuzzGenerated.Component (and similarly on gr.Component), call the
component's SetValue with the current key and payload (e.g.,
options.FuzzGenerated.Component.SetValue(gr.Key, payload) or
gr.Component.SetValue(gr.Key, payload)) and then call Rebuild() on the same
component instance (do not clone) so the rebuilt request uses the correct,
per-request state.
---
Nitpick comments:
In `@pkg/fuzz/analyzers/xss/analyzer_test.go`:
- Around line 19-40: Tests in analyzer_test.go are embedding the literal
"pwned_payload" instead of using the declared payload variable, which will drift
if payload changes; update the table-driven fixtures (entries named "HTML Text",
"Script Block", "Attribute Value (input[value])", "Attribute Name (svg)", and
"HTML Comment") to build their htmlBody using the payload variable (e.g., via
string concatenation or fmt.Sprintf) rather than the hardcoded literal so all
occurrences reference the single payload constant; ensure the variable name
payload is used exactly and that surrounding quotes/markup remain correct for
each htmlBody.
In `@pkg/fuzz/analyzers/xss/analyzer.go`:
- Around line 35-36: The initial nil-check should also guard options.HttpClient
to avoid a panic when calling options.HttpClient.Do; update the validation in
the beginning of the function (the block checking options and
options.FuzzGenerated.Component) to also check options.HttpClient != nil and
return the same false, empty string, nil triple if it's nil so subsequent calls
to options.HttpClient.Do are safe.
/claim #5838
Proposed Changes
I have added an intelligent XSS Context Analyzer in
pkg/fuzz/analyzers/xss.It uses
golang.org/x/net/htmlfor proper HTML tokenization, allowing it to accurately detect reflection in:Proof
Verified with 6 local unit tests. All tests passed.
Output:
--- PASS: TestDetermineContext (0.00s)
--- PASS: TestDetermineContext/HTML_Text_Context (0.00s)
--- PASS: TestDetermineContext/Script_Block_Context (0.00s)
--- PASS: TestDetermineContext/Attribute_Value_Context (0.00s)
--- PASS: TestDetermineContext/Attribute_Name_Context (0.00s)
--- PASS: TestDetermineContext/HTML_Comment_Context (0.00s)
Checklist
Summary by CodeRabbit
New Features
Tests