Allow \ in setvar#3519
Conversation
|
|
Hi @JonathanBerrew, it would be nice to explain the reason with an example, why it is necessary and in which case is it useful to allow |
|
Sadly I don't know why Marc made those modifications, I wasn't working on the project at the time. If this seem unnecessary, you can close the PR |
I see. I don't want to close this PR without any reason. If Marc made this modification, that means it's possible useful, but I would like to understand it. Beside of that, we need to documentation the behavior, and give examples to users, so this is why it would be good. |
|
No tests, and no use cases. 🤷 I'll close. |
|
This could be useful to have a setvar with a windows path for example or any regex. This keep the previous behaviour that was working for the escaping of the single quote |
Thanks for this explanation. Anyway, it could be very good to add a real-world example. Eg. if you use this feature, just a small part any of your rule where you use that. |
|
In our rules, we have: Here we have |
Uhm, without the mentioned |
|
Here is another example:
We also have:
Another example:
|
There was a problem hiding this comment.
Pull request overview
This PR relaxes quoted-string parsing in ModSecurity’s generic parser to allow literal backslashes inside quoted parameters (instead of treating them as an invalid escape unless followed by ' or \). This is intended to support inputs such as Windows-style paths and other strings containing backslashes.
Changes:
- Update
msre_parse_generic()so a backslash in a quoted value is only treated as an escape for'and\; otherwise it’s preserved as a literal character. - Keep backward compatibility for the previously-supported quoted pairs (
\'and\\).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if (*p == '\\') { | ||
| if ( (*(p + 1) == '\0') || ((*(p + 1) != '\'')&&(*(p + 1) != '\\')) ) { | ||
| if ((*(p + 1) == '\0')) { | ||
| *error_msg = apr_psprintf(mp, "Invalid quoted pair at position %d: %s", | ||
| (int)(p - text), text); | ||
| free(value); | ||
| return -1; | ||
| } | ||
| p++; | ||
| if ((*(p + 1) == '\'') || (*(p + 1) == '\\')) p++; // compatibility with previous behaviour | ||
| *(d++) = *(p++); |
There was a problem hiding this comment.
This change is in msre_parse_generic() (used for both target and action parsing), so it relaxes backslash handling for all quoted parameters—not just setvar. If the intent is setvar-only, consider moving this logic into the setvar parser; otherwise please update the PR title/description to reflect the broader behavioral change.
| if ((*(p + 1) == '\0')) { | ||
| *error_msg = apr_psprintf(mp, "Invalid quoted pair at position %d: %s", | ||
| (int)(p - text), text); | ||
| free(value); | ||
| return -1; | ||
| } | ||
| p++; | ||
| if ((*(p + 1) == '\'') || (*(p + 1) == '\\')) p++; // compatibility with previous behaviour | ||
| *(d++) = *(p++); |
There was a problem hiding this comment.
There are regression tests covering setvar actions, but none appear to cover the newly-allowed case of a single backslash inside a quoted parameter (e.g. a Windows-style path like C:\temp written with single backslashes in the config). Please add a regression test that exercises a quoted action parameter containing an unescaped backslash to lock in this behavior and prevent accidental re-tightening of the parser.




This is a Marc Stern modification, I don't have much more insight on the code he made. To be reviewed with caution and check if this is still relevant