Skip to content

fix: return errors instead of panicking on negative repeat counts and substr starts - #368

Merged
Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
0xABCD01:fix/repeat-substr-negative-args
Nov 2, 2026
Merged

Mzack9999 merged 1 commit into
projectdiscovery:mainfrom
0xABCD01:fix/repeat-substr-negative-args

Conversation

@0xABCD01

@0xABCD01 0xABCD01 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

What

repeat and substr panic on negative numeric arguments:

  • repeat passed its count straight to strings.Repeat, which panics on a negative count.
  • substr checked end < 0, end < start, and start > len(str), but never start < 0, so any negative start panicked on the slice, in both the two-argument and three-argument forms.

Both reproduce through Engine.EvalExpr, the path nuclei uses to evaluate template expressions, and nothing on that path recovers panics, so one expression whose count or offset is computed from response data (an extracted length, len(...) - n) takes the whole scan down.

Fix

Return an error instead, mirroring the existing guards: "negative count" in repeat, "negative start position" in substr (next to the existing "negative end position"). Same approach as the recent aes_cbc bad-key/IV fix.

Regression tests in TestRepeatAndSubstrRejectNegativeArguments, following the TestAesCbcRejectsBadKeyAndIV idiom (require.NotPanics + require.Error).

Note: the four TestDslExpressions compression subtests (gzip/deflate/zlib/zip) fail identically on the base commit in my environment (Go 1.27 flate output differs from the pinned bytes); they are unrelated to this change.

Fixes #367

@Mzack9999
Mzack9999 merged commit d61d88b into projectdiscovery:main Nov 2, 2026
6 checks passed
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.

DSL helpers repeat and substr panic on negative arguments

2 participants