Skip to content

Commit 58f5189

Browse files
authored
Merge pull request #4173 from erik-krogh/targetBlankFP
Approved by esbena
2 parents 7f18c33 + d946a61 commit 58f5189

File tree

5 files changed

+22
-3
lines changed

5 files changed

+22
-3
lines changed

change-notes/1.26/analysis-javascript.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
| **Query** | **Expected impact** | **Change** |
2828
|--------------------------------|------------------------------|---------------------------------------------------------------------------|
29+
| Potentially unsafe external link (`js/unsafe-external-link`) | Fewer results | This query no longer flags URLs constructed using a template system where only the hash or query part of the URL is dynamic. |
2930
| Incomplete URL substring sanitization (`js/incomplete-url-substring-sanitization`) | More results | This query now recognizes additional URLs when the substring check is an inclusion check. |
3031
| Ambiguous HTML id attribute (`js/duplicate-html-id`) | Results no longer shown | Precision tag reduced to "low". The query is no longer run by default. |
3132
| Unused loop iteration variable (`js/unused-loop-variable`) | Fewer results | This query no longer flags variables in a destructuring array assignment that are not the last variable in the destructed array. |

javascript/ql/src/DOM/TargetBlank.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ predicate hasDynamicHrefHostAttributeValue(DOM::ElementDefinition elem) {
2929
or
3030
exists(string url | url = attr.getStringValue() |
3131
// fixed string with templating
32-
url.regexpMatch(Templating::getDelimiterMatchingRegexp()) and
32+
url.regexpMatch(Templating::getDelimiterMatchingRegexpWithPrefix("[^?#]*")) and
3333
// ... that does not start with a fixed host or a relative path (common formats)
3434
not url.regexpMatch("(?i)((https?:)?//)?[-a-z0-9.]*/.*")
3535
)

javascript/ql/src/semmle/javascript/frameworks/Templating.qll

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@ module Templating {
3636
* of the known template delimiters identified by `getADelimiter()`,
3737
* storing it in its first (and only) capture group.
3838
*/
39-
string getDelimiterMatchingRegexp() {
40-
result = "(?s).*(" + concat("\\Q" + getADelimiter() + "\\E", "|") + ").*"
39+
string getDelimiterMatchingRegexp() { result = getDelimiterMatchingRegexpWithPrefix(".*") }
40+
41+
/**
42+
* Gets a regular expression that matches a string containing one
43+
* of the known template delimiters identified by `getADelimiter()`,
44+
* storing it in its first (and only) capture group.
45+
* Where the string prior to the template delimiter matches the regexp `prefix`.
46+
*/
47+
bindingset[prefix]
48+
string getDelimiterMatchingRegexpWithPrefix(string prefix) {
49+
result = "(?s)" + prefix + "(" + concat("\\Q" + getADelimiter() + "\\E", "|") + ").*"
4150
}
4251
}

javascript/ql/test/query-tests/DOM/TargetBlank/TargetBlank.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
| tst.html:23:1:23:61 | <a>...</> | External links without noopener/noreferrer are a potential security risk. |
22
| tst.html:24:1:24:48 | <a>...</> | External links without noopener/noreferrer are a potential security risk. |
33
| tst.html:25:1:25:36 | <a>...</> | External links without noopener/noreferrer are a potential security risk. |
4+
| tst.html:30:1:30:61 | <a>...</> | External links without noopener/noreferrer are a potential security risk. |
45
| tst.js:18:1:18:43 | <a href ... ple</a> | External links without noopener/noreferrer are a potential security risk. |
56
| tst.js:19:1:19:58 | <a href ... ple</a> | External links without noopener/noreferrer are a potential security risk. |
67
| tst.js:20:1:20:51 | <a data ... ple</a> | External links without noopener/noreferrer are a potential security risk. |

javascript/ql/test/query-tests/DOM/TargetBlank/tst.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,13 @@ <h1>NOT OK, because of dynamic URL</h1>
2626
Example
2727
</a>
2828

29+
<h1>NOT OK: mailto is not fine.</h1>
30+
<a target="_blank" href="mailto:{{var:mail}}">mail somone</a>
31+
32+
<h1>OK: template elements after # or ? are fine.</h1>
33+
<a href="file.extension?#[% row.href %]" target="_blank">Example</a>
34+
<a href="file.extension?[% row.href %]" target="_blank">Example</a>
35+
<a href="file.extension#[% row.href %]" target="_blank">Example</a>
36+
2937
</body>
3038
</html>

0 commit comments

Comments
 (0)