Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #11: Support double quotes in strings #12

Merged
merged 5 commits into from
Aug 26, 2019
Merged

Conversation

arendjr
Copy link

@arendjr arendjr commented Aug 6, 2019

This PR makes it possible to use double quotes inside strings, provided they're escaped with a preceding backslash.

Please note this PR could be considered breaking, as literal backslashes will now be interpreted as escape characters.

@cshaa
Copy link
Owner

cshaa commented Aug 7, 2019

Looks good to me! Just give me some more time to read the code properly before merging.

src/filtrex.js Outdated
);
return "STRING";`
], // "foo"
['"(?:\\\\"|[^"])*"', 'return "STRING";'], // "foo"
Copy link
Owner

@cshaa cshaa Aug 8, 2019

Choose a reason for hiding this comment

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

Your regex also matches "a\" as a legit string. The not-char pattern should be something like [^"\\] instead of [^"]. This can be used as an exploit, because the next string would be actually executed as JavaScript:

"a\" == "; window.p0wned = true; //"

Also, can you guarantee that removing the JSON.stringify won't result in any malicious code? We know that everything that comes out of stringify is safe. But how can we be sure every match of your regex is safe? (After fixing the previous bug, of course, now it obviously isn't safe.)

@cshaa
Copy link
Owner

cshaa commented Aug 8, 2019

I've found a potential security bug in your code, please be sure to fix it before I merge the PR.
Furthermore, you seem to have pushed from a branch where more people have write access, and @albehrens, probably unknowingly, also pushed there. While his commit is interesting, I'd like to discuss it in a separate PR.

@albehrens
Copy link

@m93a arend and I work in a team. That is why we both have access to the repo. I reverted my commit and open a new PR: #16

@arendjr
Copy link
Author

arendjr commented Aug 12, 2019

Thanks for the detailed explanation! I added tests for these specific cases, but actually the regex was already not vulnerable to this. It seems to me this was because the first alternative in the parentheses (\\\\") would swallow the characters (\") and thereby the double quotes would not be allowed to close the string. I have added these test cases to the PR.

Even so, it is true the regex would pretty much allow all JS escape codes to sneak in. I did not see much issue with this, though I admit I can also not guarantee it to be safe. So I have tightened the regex, so that only \\ and \" are allowed as valid escape sequences. Sequences such as \n are still invalid now. I have added test cases for this too.

Adding JSON.stringify() is unfortunately not an option. This would insert additional backslashes that would break the resulting output. Still, by only allowing explicitly allowing the \\ and \" escape codes, I hope to have made it sufficiently secure.

@cshaa cshaa changed the base branch from master to v2.0.0 August 26, 2019 16:55
@cshaa cshaa merged commit 08f60d3 into cshaa:v2.0.0 Aug 26, 2019
@arendjr
Copy link
Author

arendjr commented Aug 26, 2019

Thank you!

@cshaa
Copy link
Owner

cshaa commented Aug 28, 2019

@arendjr Don't you mind being added to the list of contributors in README?

@arendjr
Copy link
Author

arendjr commented Aug 28, 2019

Not at all 👍

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.

3 participants