Skip to content

fix(expressions): only eval template-authored expressions - #7221

Merged
dwisiswant0 merged 2 commits into
devfrom
dwisiswant0/fix/expressions/only-eval-template-authored-expressions
Mar 16, 2026
Merged

dwisiswant0 merged 2 commits into
devfrom
dwisiswant0/fix/expressions/only-eval-template-authored-expressions

Conversation

@dwisiswant0

@dwisiswant0 dwisiswant0 commented Mar 14, 2026 •

Copy link
Copy Markdown
Member

Proposed changes

expressions.Evaluate() currently replaces
placeholders first and then scans the substituted
output for expressions which allows
response-derived values, including extractor
output, to be reinterpreted as DSL/helper syntax
on a second pass, which then becomes more serious
when env-backed variables are enabled.

So making this by collecting expressions from the
original template text before placeholder
substitution and then evaluating only those
original expressions after normal replacement to
keep trusted template-authored helpers working,
including placeholders nested inside helper
arguments while treating substituted response data
as literal text.

Fixes #7210

Proof

$ go test -v -count 1 -run ^TestEvaluateDoesNotReinterpretResolvedValues$ ./pkg/protocols/common/expressions
=== RUN   TestEvaluateDoesNotReinterpretResolvedValues
=== RUN   TestEvaluateDoesNotReinterpretResolvedValues/helper_syntax_in_resolved_values_stays_literal
=== RUN   TestEvaluateDoesNotReinterpretResolvedValues/resolved_values_cannot_access_other_variables
=== RUN   TestEvaluateDoesNotReinterpretResolvedValues/template-authored_placeholders_inside_helper_expressions_still_resolve
--- PASS: TestEvaluateDoesNotReinterpretResolvedValues (0.00s)
    --- PASS: TestEvaluateDoesNotReinterpretResolvedValues/helper_syntax_in_resolved_values_stays_literal (0.00s)
    --- PASS: TestEvaluateDoesNotReinterpretResolvedValues/resolved_values_cannot_access_other_variables (0.00s)
    --- PASS: TestEvaluateDoesNotReinterpretResolvedValues/template-authored_placeholders_inside_helper_expressions_still_resolve (0.00s)
PASS
ok  	github.com/projectdiscovery/nuclei/v3/pkg/protocols/common/expressions	0.084s

Checklist

  • Pull request is created against the dev branch
  • All checks passed (lint, unit/integration/regression tests etc.) with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

Summary by CodeRabbit

  • Bug Fixes

    • Expression detection now runs before data replacement, preventing helper syntax and resolved values from being re-interpreted while preserving correct placeholder resolution.
  • Tests

    • Added unit tests verifying resolved values remain literal and isolated from other variables.
    • Added an integration test validating literal reuse of response data in HTTP scenarios.

`expressions.Evaluate()` currently replaces
placeholders first and then scans the substituted
output for expressions which allows
response-derived values, including extractor
output, to be reinterpreted as DSL/helper syntax
on a second pass, which then becomes more serious
when env-backed variables are enabled.

So making this by collecting expressions from the
original template text before placeholder
substitution and then evaluating only those
original expressions after normal replacement to
keep trusted template-authored helpers working,
including placeholders nested inside helper
arguments while treating substituted response data
as literal text.

Fixes #7210

Signed-off-by: Dwi Siswanto <[email protected]>
@dwisiswant0
dwisiswant0 requested a review from Ice3man543 March 14, 2026 22:03
@ghost
ghost requested a review from dogancanbakir March 14, 2026 22:03
@neo-by-projectdiscovery-dev

neo-by-projectdiscovery-dev Bot commented Mar 14, 2026 •

Copy link
Copy Markdown

Neo - PR Security Review

No security issues found

Highlights

  • Commit a334ad8 builds on the expression injection fix from 75836b0
  • Core security mechanism unchanged: expressions collected from original template BEFORE placeholder substitution
  • Response-derived data continues to be treated as literal text, preventing DSL reinterpretation
  • Test coverage validates the injection prevention works as intended
Hardening Notes
  • Sandbox filesystem constraints prevented full diff analysis for this incremental review. Assessment based on PR metadata, previous review state, and test output provided in PR description.

Comment @pdneo help for available commands. · Open in Neo

@coderabbitai

coderabbitai Bot commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 6b94d7b0-ddeb-4710-b9cd-ceec54e8b1e9

📥 Commits

Reviewing files that changed from the base of the PR and between 75836b0 and a334ad8.

⛔ Files ignored due to path filters (1)
  • integration_tests/protocols/http/response-data-literal-reuse.yaml is excluded by !**/*.yaml
📒 Files selected for processing (1)
  • cmd/integration-test/http.go

Walkthrough

Moved expression discovery in Evaluate to run before replacement so expressions are collected from pre-replacement data, preventing recursive evaluation of DSL syntax in resolved values. Added unit and integration tests validating literal reuse and isolation of resolved values.

Changes

Cohort / File(s) Summary
Expression Detection Timing
pkg/protocols/common/expressions/expressions.go
Invoke FindExpressions earlier in Evaluate so expressions are discovered from pre-replacement data rather than after replacements; removed the later duplicate discovery.
Expression Resolution Tests
pkg/protocols/common/expressions/expressions_test.go
Added TestEvaluateDoesNotReinterpretResolvedValues with cases ensuring resolved response-derived values are not reinterpreted as DSL/helpers and that placeholders behave as expected.
Integration Test: HTTP
cmd/integration-test/http.go
Added an integration test case and runner (httpResponseDataLiteralReuse) that spins up a local server returning template-like content and asserts template execution does not re-evaluate response-derived DSL.
Module manifest
go.mod
Module manifest updated (lines changed recorded in diff).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~22 minutes

Poem

🐰 I found expressions, neat and early,
No more re-parsing things so curly.
Response text stays as given, true,
Helpers only run on what we knew.
Hop, test, and ship — a calm review.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely summarizes the main change: preventing re-evaluation of template-authored expressions and treating response-derived values as literals.
Linked Issues check ✅ Passed The PR directly addresses all objectives from issue #7210: prevents DSL/expression re-interpretation of response-derived values, ensures helpers only evaluate template-authored input, and adds comprehensive tests.
Out of Scope Changes check ✅ Passed All changes align with the PR objective: core logic changes in expressions.go, targeted test coverage, and an integration test validating the fix behavior with no extraneous modifications.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch dwisiswant0/fix/expressions/only-eval-template-authored-expressions
📝 Coding Plan
  • Generate coding plan for human review comments

Comment @coderabbitai help to get the list of available commands and usage tips.

Tip

You can disable the changed files summary in the walkthrough.

Disable the reviews.changed_files_summary setting to disable the changed files summary in the walkthrough.

@Ice3man543 Ice3man543 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm!

@dwisiswant0
dwisiswant0 merged commit d221732 into dev Mar 16, 2026
19 checks passed
@dwisiswant0
dwisiswant0 deleted the dwisiswant0/fix/expressions/only-eval-template-authored-expressions branch March 16, 2026 11:25
dwisiswant0 added a commit that referenced this pull request Nov 5, 2026
Evaluate substituted every placeholder into the whole text first, then
wrote each expression result over the first matching marker it found.
When a substituted value repeated an expression's text, such as a
response body of {{to_upper(seed)}}, the result went into that value
and the template's own expression was left unevaluated.

Replace had the same flaw in another form: its § pass ran over the
output of its {{}} pass, so a substituted value of §name§ resolved to
the value of name, and a value could pull in any other variable.

Both now make one left-to-right pass over the template text. Evaluate
locates each expression before anything is substituted and writes the
result at that position, and Replace never scans a value it inserted.
Replace also stops converting every value up front, so rendering is
faster and allocates less.

Fixes: #8053
Fixes: #8055
Refs: #7221

Signed-off-by: Dwi Siswanto <[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.

Disable recursive variable/DSL resolution from target response data

2 participants