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
Swift: New query for Missing Regular Expression Anchor #14639
base: main
Are you sure you want to change the base?
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
|
QHelp previews: swift/ql/src/queries/Security/CWE-020/MissingRegexAnchor.qhelpMissing regular expression anchorSanitizing untrusted input with regular expressions is a common technique, but malicious actors may be able to embed one of the allowed patterns in an unexpected location. To prevent this, you should use anchors in your regular expressions, such as Even if the matching is not done in a security-critical context, it may still cause undesirable behavior when the regular expression accidentally matches. RecommendationUse anchors to ensure that regular expressions match at the expected locations. ExampleThe following example code attempts to check that a URL redirection will reach the The check with the regular expression match is, however, easy to bypass. For example by embedding A related mistake is to write a regular expression with multiple alternatives, but to only include an anchor for one of the alternatives. As an example, the regular expression References
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code LGTM!
|
Thanks for the quick review. I've added the docs review tag. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @geoffw0 👋 I've just left some minor suggestions that I think may help the reader—do please let me know what you think and whether you have any questions. My apologies if the suggestions mess up the formatting and you apply them/need to adjust the indentation.
Many thanks!
| The check with the regular expression match is, however, easy to bypass. For example | ||
| by embedding <code>http://www.example.com/</code> in the query | ||
| string component: <code>http://evil-example.net/?x=http://www.example.com/</code>. | ||
| Address these shortcomings by using anchors in the regular expression instead: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| The check with the regular expression match is, however, easy to bypass. For example | |
| by embedding <code>http://www.example.com/</code> in the query | |
| string component: <code>http://evil-example.net/?x=http://www.example.com/</code>. | |
| Address these shortcomings by using anchors in the regular expression instead: | |
| However, this regular expression check can be easily bypassed, | |
| and a malicious actor could embed | |
| <code>http://www.example.com/</code> in the query | |
| string component of a malicious site. For example, | |
| <code>http://evil-example.net/?x=http://www.example.com/</code>. | |
| Instead, you should use anchors in the regular expression check: |
| A related mistake is to write a regular expression with | ||
| multiple alternatives, but to only include an anchor for one of the | ||
| alternatives. As an example, the regular expression | ||
| <code>/^www\.example\.com|beta\.example\.com/</code> will match the host | ||
| <code>evil.beta.example.com</code> because the regular expression is parsed | ||
| as <code>/(^www\.example\.com)|(beta\.example\.com)/</code> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| A related mistake is to write a regular expression with | |
| multiple alternatives, but to only include an anchor for one of the | |
| alternatives. As an example, the regular expression | |
| <code>/^www\.example\.com|beta\.example\.com/</code> will match the host | |
| <code>evil.beta.example.com</code> because the regular expression is parsed | |
| as <code>/(^www\.example\.com)|(beta\.example\.com)/</code> | |
| If you need to write a regular expression to match | |
| multiple hosts, you should include an anchor for all of the | |
| alternatives. For example, the regular expression | |
| <code>/^www\.example\.com|beta\.example\.com/</code> will only match the host | |
| <code>evil.beta.example.com</code>, because the regular expression is parsed | |
| as <code>/(^www\.example\.com)|(beta\.example\.com)/</code>. |
Co-authored-by: Ben Ahmady <32935794+subatoi@users.noreply.github.com>
Co-authored-by: Ben Ahmady <32935794+subatoi@users.noreply.github.com>
|
Hi @subatoi, thank you for your review and suggestions. I've accepted three, the other two I think I'm going to adapt a little... |
New Swift query for missing regular expression anchors. This is a port of the existing query from JS (it also exists for Ruby and Go), with most of the query logic residing in the shared and Swift regular expressions libraries. I suggest that reviewers focus on results (e.g. in the tests, which I have also adapted from JS), or compare to other language versions of the query.
The PR also contains qhelp and examples, adapted from JS as well.
TODO: