|
46 | 46 |
|
47 | 47 | <p>
|
48 | 48 |
|
49 |
| - The check with the regular expression match is, however, easy to bypass. For example |
50 |
| - by embedding <code>http://www.example.com/</code> in the query |
51 |
| - string component: <code>http://evil-example.net/?x=http://www.example.com/</code>. |
52 |
| - Address these shortcomings by using anchors in the regular expression instead: |
| 49 | + However, this regular expression check can be easily bypassed, |
| 50 | + and a malicious actor could embed |
| 51 | + <code>http://www.example.com/</code> in the query |
| 52 | + string component of a malicious site. For example, |
| 53 | + <code>http://evil-example.net/?x=http://www.example.com/</code>. |
| 54 | + Instead, you should use anchors in the regular expression check: |
53 | 55 |
|
54 | 56 | </p>
|
55 | 57 |
|
56 | 58 | <sample src="MissingRegexAnchorGood.swift"/>
|
57 | 59 |
|
58 | 60 | <p>
|
59 | 61 |
|
60 |
| - A related mistake is to write a regular expression with |
61 |
| - multiple alternatives, but to only include an anchor for one of the |
62 |
| - alternatives. As an example, the regular expression |
63 |
| - <code>/^www\.example\.com|beta\.example\.com/</code> will match the host |
64 |
| - <code>evil.beta.example.com</code> because the regular expression is parsed |
65 |
| - as <code>/(^www\.example\.com)|(beta\.example\.com)/</code> |
| 62 | + If you need to write a regular expression to match |
| 63 | + multiple hosts, you should include an anchor for all of the |
| 64 | + alternatives. For example, the regular expression |
| 65 | + <code>/^www\.example\.com|beta\.example\.com/</code> will only match the host |
| 66 | + <code>evil.beta.example.com</code>, because the regular expression is parsed |
| 67 | + as <code>/(^www\.example\.com)|(beta\.example\.com)/</code>. |
66 | 68 |
|
67 | 69 | </p>
|
68 | 70 | </example>
|
|
0 commit comments