Skip to content

Commit 9f01834

Browse files
authored
Increase IAST propagation to StringBuilder subSequence (#8026)
1 parent b1bfb13 commit 9f01834

File tree

5 files changed

+63
-34
lines changed

5 files changed

+63
-34
lines changed

dd-java-agent/instrumentation/java-lang/src/main/java/datadog/trace/instrumentation/java/lang/StringBuilderCallSite.java

+17
Original file line numberDiff line numberDiff line change
@@ -139,4 +139,21 @@ public static String afterSubstring(
139139
}
140140
return result;
141141
}
142+
143+
@CallSite.After("java.lang.CharSequence java.lang.StringBuilder.subSequence(int, int)")
144+
public static CharSequence afterSubSequence(
145+
@CallSite.This final CharSequence self,
146+
@CallSite.Argument final int beginIndex,
147+
@CallSite.Argument final int endIndex,
148+
@CallSite.Return final CharSequence result) {
149+
final StringModule module = InstrumentationBridge.STRING;
150+
if (module != null) {
151+
try {
152+
module.onStringSubSequence(self, beginIndex, endIndex, result);
153+
} catch (final Throwable e) {
154+
module.onUnexpectedException("afterSubSequence threw", e);
155+
}
156+
}
157+
return result;
158+
}
142159
}

dd-java-agent/instrumentation/java-lang/src/test/groovy/datadog/trace/instrumentation/java/lang/StringBuilderCallSiteTest.groovy

+14-30
Original file line numberDiff line numberDiff line change
@@ -174,76 +174,60 @@ class StringBuilderCallSiteTest extends AgentTestRunner {
174174
ex.stackTrace.find { it.className == StringBuilderCallSite.name } == null
175175
}
176176
177-
def 'test string builder substring call site'() {
177+
def 'test string #type substring call site'() {
178178
setup:
179179
final iastModule = Mock(StringModule)
180180
InstrumentationBridge.registerIastModule(iastModule)
181181
182182
when:
183-
final result = TestStringBuilderSuite.substring(param, beginIndex)
183+
final result = suite.substring(param, beginIndex)
184184
185185
then:
186186
result == expected
187187
1 * iastModule.onStringSubSequence(param, beginIndex, param.length(), expected)
188188
0 * _
189189
190190
where:
191-
param | beginIndex | expected
192-
sb('012345') | 1 | '12345'
191+
type | suite | param | beginIndex | expected
192+
"builder" | new TestStringBuilderSuite() | sb('012345') | 1 | '12345'
193+
"buffer" | new TestStringBufferSuite() | sbf('012345') | 1 | '12345'
193194
}
194195
195-
def 'test string buffer substring call site'() {
196+
def 'test string #type substring with endIndex call site'() {
196197
setup:
197198
final iastModule = Mock(StringModule)
198199
InstrumentationBridge.registerIastModule(iastModule)
199200
200201
when:
201-
final result = TestStringBufferSuite.substring(param, beginIndex)
202-
203-
then:
204-
result == expected
205-
1 * iastModule.onStringSubSequence(param, beginIndex, param.length(), expected)
206-
0 * _
207-
208-
where:
209-
param | beginIndex | expected
210-
sbf('012345') | 1 | '12345'
211-
}
212-
213-
def 'test string builder substring with endIndex call site'() {
214-
setup:
215-
final iastModule = Mock(StringModule)
216-
InstrumentationBridge.registerIastModule(iastModule)
217-
218-
when:
219-
final result = TestStringBuilderSuite.substring(param, beginIndex, endIndex)
202+
final result = suite.substring(param, beginIndex, endIndex)
220203
221204
then:
222205
result == expected
223206
1 * iastModule.onStringSubSequence(param, beginIndex, endIndex, expected)
224207
0 * _
225208
226209
where:
227-
param | beginIndex | endIndex | expected
228-
sb('012345') | 1 | 5 | '1234'
210+
type | suite | param | beginIndex | endIndex | expected
211+
"builder" | new TestStringBuilderSuite() | sb('012345') | 1 | 5 | '1234'
212+
"buffer" | new TestStringBufferSuite() | sbf('012345') | 1 | 5 | '1234'
229213
}
230214
231-
def 'test string buffer substring with endIndex call site'() {
215+
def 'test string #type subSequence with endIndex call site'() {
232216
setup:
233217
final iastModule = Mock(StringModule)
234218
InstrumentationBridge.registerIastModule(iastModule)
235219
236220
when:
237-
final result = TestStringBufferSuite.substring(param, beginIndex, endIndex)
221+
final result = suite.subSequence(param, beginIndex, endIndex)
238222
239223
then:
240224
result == expected
241225
1 * iastModule.onStringSubSequence(param, beginIndex, endIndex, expected)
242226
0 * _
243227
244228
where:
245-
param | beginIndex | endIndex | expected
246-
sbf('012345') | 1 | 5 | '1234'
229+
type | suite | param | beginIndex | endIndex | expected
230+
"builder" | new TestStringBuilderSuite() | sb('012345') | 1 | 5 | '1234'
247231
}
248232
249233
private static class BrokenToString {

dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestAbstractStringBuilderSuite.java

+6
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,11 @@ public interface TestAbstractStringBuilderSuite<E> {
1212

1313
void append(final E target, final Object param);
1414

15+
String substring(final E self, final int beginIndex, final int endIndex);
16+
17+
String substring(final E self, final int beginIndex);
18+
19+
CharSequence subSequence(final E self, final int beginIndex, final int endIndex);
20+
1521
String toString(final E target);
1622
}

dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBufferSuite.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,28 @@ public String toString(final StringBuffer buffer) {
5252
return result;
5353
}
5454

55-
public static String substring(StringBuffer self, int beginIndex, int endIndex) {
55+
@Override
56+
public String substring(final StringBuffer self, final int beginIndex, final int endIndex) {
5657
LOGGER.debug("Before string buffer substring {} from {} to {}", self, beginIndex, endIndex);
5758
final String result = self.substring(beginIndex, endIndex);
5859
LOGGER.debug("After string buffer substring {}", result);
5960
return result;
6061
}
6162

62-
public static String substring(StringBuffer self, int beginIndex) {
63+
@Override
64+
public String substring(final StringBuffer self, final int beginIndex) {
6365
LOGGER.debug("Before string buffer substring {} from {}", self, beginIndex);
6466
final String result = self.substring(beginIndex);
6567
LOGGER.debug("After string buffer substring {}", result);
6668
return result;
6769
}
70+
71+
@Override
72+
public CharSequence subSequence(
73+
final StringBuffer self, final int beginIndex, final int endIndex) {
74+
LOGGER.debug("Before string builder subSequence {} from {} to {}", self, beginIndex, endIndex);
75+
final CharSequence result = self.subSequence(beginIndex, endIndex);
76+
LOGGER.debug("After string builder subSequence {}", result);
77+
return result;
78+
}
6879
}

dd-java-agent/instrumentation/java-lang/src/test/java/foo/bar/TestStringBuilderSuite.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -77,17 +77,28 @@ public String plus(final Object... items) {
7777
return result;
7878
}
7979

80-
public static String substring(StringBuilder self, int beginIndex, int endIndex) {
80+
@Override
81+
public String substring(final StringBuilder self, final int beginIndex, final int endIndex) {
8182
LOGGER.debug("Before string builder substring {} from {} to {}", self, beginIndex, endIndex);
8283
final String result = self.substring(beginIndex, endIndex);
8384
LOGGER.debug("After string builder substring {}", result);
8485
return result;
8586
}
8687

87-
public static String substring(StringBuilder self, int beginIndex) {
88+
@Override
89+
public String substring(final StringBuilder self, final int beginIndex) {
8890
LOGGER.debug("Before string builder substring {} from {}", self, beginIndex);
8991
final String result = self.substring(beginIndex);
9092
LOGGER.debug("After string builder substring {}", result);
9193
return result;
9294
}
95+
96+
@Override
97+
public CharSequence subSequence(
98+
final StringBuilder self, final int beginIndex, final int endIndex) {
99+
LOGGER.debug("Before string builder subSequence {} from {} to {}", self, beginIndex, endIndex);
100+
final CharSequence result = self.subSequence(beginIndex, endIndex);
101+
LOGGER.debug("After string builder subSequence {}", result);
102+
return result;
103+
}
93104
}

0 commit comments

Comments
 (0)