Skip to content

Commit ed250d5

Browse files
authored
Merge pull request github#5339 from smowton/smowton/feature/commons-regex-utils
Java: Add models for Commons-Lang's RegExUtils class
2 parents b1d0b9a + 189b221 commit ed250d5

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
lgtm,codescanning
2+
* Added models for Apache Commons Lang's `RegExUtils` class. This means that any query that tracks tainted data may return additional results in cases where a `RegExUtils` transformation is part of the path from source to sink.

java/ql/src/semmle/code/java/frameworks/apache/Lang.qll

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,23 @@ private class ApacheStrSubstitutorModel extends SummaryModelCsv {
376376
]
377377
}
378378
}
379+
380+
/**
381+
* Taint-propagating models for `RegexUtils`.
382+
*/
383+
private class ApacheRegExUtilsModel extends SummaryModelCsv {
384+
override predicate row(string row) {
385+
row =
386+
[
387+
"org.apache.commons.lang3;RegExUtils;false;removeAll;;;Argument[0];ReturnValue;taint",
388+
"org.apache.commons.lang3;RegExUtils;false;removeFirst;;;Argument[0];ReturnValue;taint",
389+
"org.apache.commons.lang3;RegExUtils;false;removePattern;;;Argument[0];ReturnValue;taint",
390+
"org.apache.commons.lang3;RegExUtils;false;replaceAll;;;Argument[0];ReturnValue;taint",
391+
"org.apache.commons.lang3;RegExUtils;false;replaceFirst;;;Argument[0];ReturnValue;taint",
392+
"org.apache.commons.lang3;RegExUtils;false;replacePattern;;;Argument[0];ReturnValue;taint",
393+
"org.apache.commons.lang3;RegExUtils;false;replaceAll;;;Argument[2];ReturnValue;taint",
394+
"org.apache.commons.lang3;RegExUtils;false;replaceFirst;;;Argument[2];ReturnValue;taint",
395+
"org.apache.commons.lang3;RegExUtils;false;replacePattern;;;Argument[2];ReturnValue;taint"
396+
]
397+
}
398+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import org.apache.commons.lang3.RegExUtils;
2+
import java.util.regex.Pattern;
3+
4+
public class RegExUtilsTest {
5+
String taint() { return "tainted"; }
6+
7+
void sink(Object o) {}
8+
9+
void test() throws Exception {
10+
Pattern cleanPattern = Pattern.compile("clean");
11+
Pattern taintedPattern = Pattern.compile(taint());
12+
13+
sink(RegExUtils.removeAll(taint(), cleanPattern)); // $hasTaintFlow
14+
sink(RegExUtils.removeAll(taint(), "clean")); // $hasTaintFlow
15+
sink(RegExUtils.removeFirst(taint(), cleanPattern)); // $hasTaintFlow
16+
sink(RegExUtils.removeFirst(taint(), "clean")); // $hasTaintFlow
17+
sink(RegExUtils.removePattern(taint(), "clean")); // $hasTaintFlow
18+
sink(RegExUtils.replaceAll(taint(), cleanPattern, "replacement")); // $hasTaintFlow
19+
sink(RegExUtils.replaceAll(taint(), "clean", "replacement")); // $hasTaintFlow
20+
sink(RegExUtils.replaceFirst(taint(), cleanPattern, "replacement")); // $hasTaintFlow
21+
sink(RegExUtils.replaceFirst(taint(), "clean", "replacement")); // $hasTaintFlow
22+
sink(RegExUtils.replacePattern(taint(), "clean", "replacement")); // $hasTaintFlow
23+
sink(RegExUtils.replaceAll("original", cleanPattern, taint())); // $hasTaintFlow
24+
sink(RegExUtils.replaceAll("original", "clean", taint())); // $hasTaintFlow
25+
sink(RegExUtils.replaceFirst("original", cleanPattern, taint())); // $hasTaintFlow
26+
sink(RegExUtils.replaceFirst("original", "clean", taint())); // $hasTaintFlow
27+
sink(RegExUtils.replacePattern("original", "clean", taint())); // $hasTaintFlow
28+
// Subsequent calls don't propagate taint, as regex search patterns don't propagate to the return value.
29+
sink(RegExUtils.removeAll("original", taintedPattern));
30+
sink(RegExUtils.removeAll("original", taint()));
31+
sink(RegExUtils.removeFirst("original", taintedPattern));
32+
sink(RegExUtils.removeFirst("original", taint()));
33+
sink(RegExUtils.removePattern("original", taint()));
34+
sink(RegExUtils.replaceAll("original", taintedPattern, "replacement"));
35+
sink(RegExUtils.replaceAll("original", taint(), "replacement"));
36+
sink(RegExUtils.replaceFirst("original", taintedPattern, "replacement"));
37+
sink(RegExUtils.replaceFirst("original", taint(), "replacement"));
38+
sink(RegExUtils.replacePattern("original", taint(), "replacement"));
39+
sink(RegExUtils.replaceAll("original", taintedPattern, "replacement"));
40+
sink(RegExUtils.replaceAll("original", taint(), "replacement"));
41+
sink(RegExUtils.replaceFirst("original", taintedPattern, "replacement"));
42+
sink(RegExUtils.replaceFirst("original", taint(), "replacement"));
43+
sink(RegExUtils.replacePattern("original", taint(), "replacement"));
44+
}
45+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.lang3;
18+
19+
import java.util.regex.Pattern;
20+
21+
public class RegExUtils {
22+
public static String removeAll(final String text, final Pattern regex) {
23+
return null;
24+
}
25+
26+
public static String removeAll(final String text, final String regex) {
27+
return null;
28+
}
29+
30+
public static String removeFirst(final String text, final Pattern regex) {
31+
return null;
32+
}
33+
34+
public static String removeFirst(final String text, final String regex) {
35+
return null;
36+
}
37+
38+
public static String removePattern(final String text, final String regex) {
39+
return null;
40+
}
41+
42+
public static String replaceAll(final String text, final Pattern regex, final String replacement) {
43+
return null;
44+
}
45+
46+
public static String replaceAll(final String text, final String regex, final String replacement) {
47+
return null;
48+
}
49+
50+
public static String replaceFirst(final String text, final Pattern regex, final String replacement) {
51+
return null;
52+
}
53+
54+
public static String replaceFirst(final String text, final String regex, final String replacement) {
55+
return null;
56+
}
57+
58+
public static String replacePattern(final String text, final String regex, final String replacement) {
59+
return null;
60+
}
61+
62+
}

0 commit comments

Comments
 (0)