Skip to content

Commit 46a14b2

Browse files
committed
move parseInt logic into getValue method predicate on IntegerLiteral
1 parent 1c89bbe commit 46a14b2

2 files changed

Lines changed: 28 additions & 23 deletions

File tree

ql/src/codeql_ruby/ast/Literal.qll

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,33 @@ class IntegerLiteral extends NumericLiteral, TIntegerLiteral {
4747

4848
final override string getValueText() { result = g.getValue() }
4949

50+
final int getValue() {
51+
exists(string s, string values, string str |
52+
s = this.getValueText() and
53+
(
54+
s.matches("0b%") and values = "01" and str = s.suffix(2)
55+
or
56+
s.matches("0x%") and values = "0123456789abcdef" and str = s.suffix(2)
57+
or
58+
s.charAt(0) = "0" and
59+
not s.charAt(1) = ["b", "x"] and
60+
values = "01234567" and
61+
str = s.suffix(1)
62+
or
63+
s.charAt(0) != "0" and values = "0123456789" and str = s
64+
)
65+
|
66+
result =
67+
sum(int index, string c, int v, int exp |
68+
c = str.replaceAll("_", "").charAt(index) and
69+
v = values.indexOf(c.toLowerCase()) and
70+
exp = str.replaceAll("_", "").length() - index - 1
71+
|
72+
v * values.length().pow(exp)
73+
)
74+
)
75+
}
76+
5077
final override string toString() { result = this.getValueText() }
5178

5279
final override string getAPrimaryQlClass() { result = "IntegerLiteral" }

ql/src/queries/security/cwe-732/WeakFilePermissions.ql

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,34 +41,12 @@ string access(int p) {
4141
p % 8 in [4, 5] and result = "readable"
4242
}
4343

44-
bindingset[s]
45-
int parseInt(string s) {
46-
exists(string values, string str |
47-
s.matches("0b%") and values = "01" and str = s.suffix(2)
48-
or
49-
s.matches("0x%") and values = "0123456789abcdef" and str = s.suffix(2)
50-
or
51-
s.charAt(0) = "0" and not s.charAt(1) = ["b", "x"] and values = "01234567" and str = s.suffix(1)
52-
or
53-
s.charAt(0) != "0" and values = "0123456789" and str = s
54-
|
55-
result =
56-
sum(int index, string c, int v, int exp |
57-
c = str.replaceAll("_", "").charAt(index) and
58-
v = values.indexOf(c.toLowerCase()) and
59-
exp = str.replaceAll("_", "").length() - index - 1
60-
|
61-
v * values.length().pow(exp)
62-
)
63-
)
64-
}
65-
6644
/** An expression specifing a file permission that allows group/others read or write access */
6745
class PermissivePermissionsExpr extends Expr {
6846
// TODO: non-literal expressions?
6947
PermissivePermissionsExpr() {
7048
exists(int perm, string acc |
71-
perm = parseInt(this.(IntegerLiteral).getValueText()) and
49+
perm = this.(IntegerLiteral).getValue() and
7250
(acc = access(world_permission(perm)) or acc = access(group_permission(perm)))
7351
)
7452
or

0 commit comments

Comments
 (0)