Skip to content

Commit 851317e

Browse files
committed
Add models for StrBuilder's fluent methods
1 parent 7f16c52 commit 851317e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,15 @@ private class ApacheStrBuilderModel extends SummaryModelCsv {
427427
}
428428
}
429429

430+
/**
431+
* An Apache Commons-Lang StrBuilder method that returns `this`.
432+
*/
433+
private class ApacheStrBuilderFluentMethod extends FluentMethod {
434+
ApacheStrBuilderFluentMethod() {
435+
this.getReturnType().(RefType).hasQualifiedName("org.apache.commons.lang3.text", "StrBuilder")
436+
}
437+
}
438+
430439
/**
431440
* Taint-propagating models for `WordUtils`.
432441
*/

java/ql/test/library-tests/frameworks/apache-commons-lang3/StrBuilderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,20 @@ void test() throws Exception {
128128
StrBuilder sb72 = new StrBuilder(); sb72.append(taint()); sink(sb72.toCharArray(0, 0)); // $hasTaintFlow
129129
StrBuilder sb73 = new StrBuilder(); sb73.append(taint()); sink(sb73.toStringBuffer()); // $hasTaintFlow
130130
StrBuilder sb74 = new StrBuilder(); sb74.append(taint()); sink(sb74.toStringBuilder()); // $hasTaintFlow
131+
132+
// Tests for fluent methods (those returning `this`):
133+
134+
StrBuilder fluentTest = new StrBuilder();
135+
sink(fluentTest.append("Harmless").append(taint()).append("Also harmless").toString()); // $hasTaintFlow
136+
137+
StrBuilder fluentBackflowTest = new StrBuilder();
138+
fluentBackflowTest.append("Harmless").append(taint()).append("Also harmless");
139+
sink(fluentBackflowTest.toString()); // $hasTaintFlow
140+
141+
// Test the case where the fluent method contributing taint is at the end of a statement:
142+
StrBuilder fluentBackflowTest2 = new StrBuilder();
143+
fluentBackflowTest2.append("Harmless").append(taint());
144+
sink(fluentBackflowTest2.toString()); // $hasTaintFlow
131145
}
132146

133147
}

0 commit comments

Comments
 (0)