Skip to content

perf: Optimise urlDecodeUni transformation - #3795

Open
JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:url-decode-uni
Open

JakubOnderka wants to merge 1 commit into
owasp-modsecurity:v3/masterfrom
JakubOnderka:url-decode-uni

Conversation

@JakubOnderka

@JakubOnderka JakubOnderka commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

what

  • Optimise urlDecodeUni transformation by first checking if string contains + or % that needs to be transformed

why

Benchmark Case Original Optimized Speedup Total Time (Original / Optimized)
No % or + (short) 13.77 ns/call 10.35 ns/call 1.33x 0.2 ms / 0.2 ms
No % or + (long) 6211.44 ns/call 646.41 ns/call 9.61x 62.1 ms / 6.5 ms
Mixed %u, %xx, and + 112.77 ns/call 45.31 ns/call 2.49x 1.1 ms / 0.5 ms

Summary by CodeRabbit

  • Bug Fixes
    • URL decoding preserves text before the first + or encoded character.
    • Text without either marker remains unchanged. When a marker is present, decoding begins at its earliest occurrence, retaining any preceding text in the result.

@coderabbitai

coderabbitai Bot commented Dec 1, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The URL decoding transformation now locates the first + or % before decoding. It returns false when neither is present, preserves preceding bytes, and removes unused counting and explicit NUL-termination operations.

Changes: URL decoding transformation

Layer / File(s) Summary
Locate and decode special characters
src/actions/transformations/url_decode_uni.cc
The transformation starts decoding at the first + or %, or returns false when neither is present. It removes an unused counter and an explicit NUL write before resizing.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: zimmerle, eduar-hte

Merge Risk: Minimal · up to 1d1ba

This change speeds up URL decoding by skipping straight to the first + or %, and returns early when neither is present. The output and return value stay the same as before. No concrete defect was found, so the merge risk is minimal.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "perf: Optimise urlDecodeUni transformation" names the primary change. The raw summary shows the urlDecodeUni transformation now locates the first '+' or '%' and returns early when neither e…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "perf: Optimise urlDecodeUni transformation" names the primary change. The raw summary shows the urlDecodeUni transformation now locates the first '+' or '%' and returns early when neither exists, which is a performance optimisation of that transformation. The title is short, specific, and follows a conventional commit style. It does not list the detailed fast path, but that detail is not needed for a title.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 7433f002-b4a8-4392-a7c4-2089405e2bb2
Commits

Reviewing files that changed from the base of the PR and between d1803aa and 1d1ba97.

Files selected for processing: 1

  • src/actions/transformations/url_decode_uni.cc

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 5 remain after this review.

@airween

airween commented Dec 1, 2026

Copy link
Copy Markdown
Member

Hi @JakubOnderka, thanks for the PR!

I went through the change: the logic is equivalent to the old loop. Every byte before the first % or + is a plain byte that the old code copied onto itself (d == input + i) without setting changed, so starting both i and d at first_special gives the same output and the same return value, and the early return for input with no %/+ returns false like before. Dropping *d = '\0' is also fine, because value.resize() takes care of the terminator.

A few things before we can merge:

  1. Tests. The new code paths are only partly covered by the existing urlDecodeUni unit cases: there is no case where plain bytes come before a % (the pct branch with a non-zero offset), or where a + comes after the first % with a prefix. Could you add a few cases, e.g. Test%20Case → Test Case, ab%41+c → abA c, a+b%41 → a bA, abc% → abc% (ret 0) and abc%zz → abc%zz (ret 0)?

  2. Commit message. Please add a body to the commit (first line, blank line, then the technical details), e.g. what the fast path does, that the output and the return value don't change, and the benchmark numbers with how you measured them.

  3. #include <cstring>. std::memchr currently compiles only because src/utils/string.h includes <cstring>. Please include it directly, as the other transformations that use <cstring> do.

  4. Stale comment. The IMP1 Assumes NUL-terminated comment above inplace() was already misleading (the loop uses input_len), and now the function doesn't write a NUL at all. Please remove or update it.

  5. Optional: when the + is near the start of a long value, find_first_special() scans the whole value for % first and the decode loop then walks it again. The benchmark doesn't have a case like that (e.g. a long a+b... value with no %). It's probably negligible because memchr is fast, but a number for that case would be nice to see.

Also please check Sonar's issue and update your branch!

Thanks!

Skip leading non-special characters, returning early when neither '+' nor '%' is present
@sonarqubecloud

sonarqubecloud Bot commented Dec 2, 2026

Copy link
Copy Markdown

@JakubOnderka

Copy link
Copy Markdown
Contributor Author

Force pushed new version that is using string::find method. Tests cases added in owasp-modsecurity/secrules-language-tests#13

This branch has not been deployed

No deployments
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.

2 participants