Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase IAST propagation to StringBuffer setLength #8128

Merged
merged 7 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -852,6 +852,8 @@ public void onStringBuilderSetLength(@Nonnull CharSequence self, int length) {
Range[] newRanges = Ranges.forSubstring(0, length, rangesSelf);
if (newRanges != null && newRanges.length > 0) {
selfTainted.setRanges(newRanges);
} else {
selfTainted.clear();
Mariovido marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1448,9 +1448,11 @@ class StringModuleTest extends IastModuleImplTestBase {
0 * _

where:
self | length | mockCalls
sb("123") | 2 | 0
sb() | 0 | 1
self | length | mockCalls
sb("123") | 2 | 0
sb() | 0 | 1
sbf("123") | 2 | 0
sbf() | 0 | 1
}

void 'onStringBuilderSetLength (#input, #length)'() {
Expand All @@ -1472,10 +1474,38 @@ class StringModuleTest extends IastModuleImplTestBase {
taintFormat(result, taintedObject.getRanges()) == expected

where:
input | length | expected
sb("==>0123<==") | 3 | "==>012<=="
sb("0123==>456<==78") | 5 | "0123==>4<=="
sb("01==>234<==5==>678<==90") | 8 | "01==>234<==5==>67<=="
input | length | expected
sb("==>0123<==") | 3 | "==>012<=="
sb("0123==>456<==78") | 5 | "0123==>4<=="
sb("01==>234<==5==>678<==90") | 8 | "01==>234<==5==>67<=="
sbf("==>0123<==") | 3 | "==>012<=="
sbf("0123==>456<==78") | 5 | "0123==>4<=="
sbf("01==>234<==5==>678<==90") | 8 | "01==>234<==5==>67<=="
}

void 'onStringBuilderSetLength untainting after setLength (#input, #length)'() {
final taintedObjects = ctx.getTaintedObjects()
def self = addFromTaintFormat(taintedObjects, input)
if (self instanceof StringBuilder) {
((StringBuilder) self).setLength(length)
} else if (self instanceof StringBuffer) {
((StringBuffer) self).setLength(length)
}

when:
module.onStringBuilderSetLength(self, length)
def taintedObject = taintedObjects.get(self)

then:
1 * tracer.activeSpan() >> span
taintedObject == null

where:
input | length
sb("==>0123<==") | 0
sb("0123==>456<==78") | 3
sbf("==>0123<==") | 0
sbf("0123==>456<==78") | 3
}

private static Date date(final String pattern, final String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ public static CharSequence afterSubSequence(
}

@CallSite.After("void java.lang.StringBuilder.setLength(int)")
@CallSite.After("void java.lang.StringBuffer.setLength(int)")
public static void afterSetLength(
@CallSite.This final CharSequence self, @CallSite.Argument final int length) {
final StringModule module = InstrumentationBridge.STRING;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ class StringBuilderCallSiteTest extends AgentTestRunner {
where:
type | suite | param | length | expected
"builder" | new TestStringBuilderSuite() | sb('012345') | 5 | '01234'
"buffer" | new TestStringBufferSuite() | sbf('012345') | 5 | '01234'
}

private static class BrokenToString {
Expand Down
Loading