Skip to content

Commit 14db0df

Browse files
committed
Swift: Fairly minimal fix to get the query and test working for Swift.
1 parent c040d48 commit 14db0df

File tree

3 files changed

+29
-16
lines changed

3 files changed

+29
-16
lines changed

swift/ql/lib/codeql/swift/security/regex/HostnameRegex.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ private import codeql.regex.HostnameRegexp as Shared
1212
/**
1313
* An implementation of the signature that allows the Hostname analysis to run.
1414
*/
15-
private module Impl implements Shared::HostnameRegexpSig<TreeImpl> {
15+
module Impl implements Shared::HostnameRegexpSig<TreeImpl> {
1616
class DataFlowNode = DataFlow::Node;
1717

1818
class RegExpPatternSource = Regex::RegexPatternSource;

swift/ql/src/queries/Security/CWE-020/MissingRegexAnchor.ql

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,25 @@
55
* @problem.severity warning
66
* @security-severity 7.8
77
* @precision medium
8-
* @id js/regex/missing-regexp-anchor
8+
* @id swift/missing-regexp-anchor
99
* @tags correctness
1010
* security
1111
* external/cwe/cwe-020
1212
*/
1313

14-
private import javascript
15-
private import semmle.javascript.security.regexp.HostnameRegexp as HostnameRegexp
14+
private import swift
15+
private import codeql.swift.dataflow.DataFlow
16+
private import codeql.swift.regex.Regex
17+
private import codeql.swift.regex.RegexTreeView::RegexTreeView as TreeImpl
18+
private import codeql.swift.security.regex.HostnameRegex as HostnameRegex
1619
private import codeql.regex.MissingRegExpAnchor as MissingRegExpAnchor
17-
private import semmle.javascript.security.regexp.RegExpTreeView::RegExpTreeView as TreeImpl
1820

1921
private module Impl implements
20-
MissingRegExpAnchor::MissingRegExpAnchorSig<TreeImpl, HostnameRegexp::Impl>
22+
MissingRegExpAnchor::MissingRegExpAnchorSig<TreeImpl, HostnameRegex::Impl>
2123
{
22-
predicate isUsedAsReplace(RegExpPatternSource pattern) {
23-
// is used for capture or replace
24+
predicate isUsedAsReplace(RegexPatternSource pattern) {
25+
none()
26+
/* java // is used for capture or replace
2427
exists(DataFlow::MethodCallNode mcn, string name | name = mcn.getMethodName() |
2528
name = "exec" and
2629
mcn = pattern.getARegExpObject().getAMethodCall() and
@@ -37,20 +40,30 @@ private module Impl implements
3740
or
3841
name = "match" and exists(mcn.getAPropertyRead())
3942
)
40-
)
43+
)*/
44+
/* rb exists(DataFlow::CallNode mcn, DataFlow::Node arg, string name |
45+
name = mcn.getMethodName() and
46+
arg = mcn.getArgument(0)
47+
|
48+
(
49+
pattern.getAParse().(DataFlow::LocalSourceNode).flowsTo(arg) or
50+
pattern.getAParse() = arg
51+
) and
52+
name = ["sub", "sub!", "gsub", "gsub!"]
53+
)*/
4154
}
4255

4356
string getEndAnchorText() { result = "$" }
4457
}
4558

46-
import MissingRegExpAnchor::Make<TreeImpl, HostnameRegexp::Impl, Impl>
59+
import MissingRegExpAnchor::Make<TreeImpl, HostnameRegex::Impl, Impl>
4760

48-
from DataFlow::Node nd, string msg
61+
from DataFlow::Node node, string msg
4962
where
50-
isUnanchoredHostnameRegExp(nd, msg)
63+
isUnanchoredHostnameRegExp(node, msg)
5164
or
52-
isSemiAnchoredHostnameRegExp(nd, msg)
65+
isSemiAnchoredHostnameRegExp(node, msg)
5366
or
54-
hasMisleadingAnchorPrecedence(nd, msg)
67+
hasMisleadingAnchorPrecedence(node, msg)
5568
// isLineAnchoredHostnameRegExp is not used here, as it is not relevant to JS.
56-
select nd, msg
69+
select node, msg
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
Security/CWE-020/MissingRegExpAnchor.ql
1+
queries/Security/CWE-020/MissingRegexAnchor.ql

0 commit comments

Comments
 (0)