Skip to content

Commit c6083b5

Browse files
committed
add security response id
1 parent de2c137 commit c6083b5

File tree

3 files changed

+123
-9
lines changed

3 files changed

+123
-9
lines changed

dd-java-agent/agent-bootstrap/src/main/java/datadog/trace/bootstrap/blocking/BlockingActionHelper.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,18 +119,13 @@ public static TemplateType determineTemplateType(
119119
}
120120

121121
public static byte[] getTemplate(TemplateType type) {
122-
return getTemplate(type, null);
123-
}
124-
125-
public static byte[] getTemplate(TemplateType type, String securityResponseId) {
126-
byte[] template;
127122
if (type == TemplateType.JSON) {
128-
template = TEMPLATE_JSON;
123+
return TEMPLATE_JSON;
129124
} else if (type == TemplateType.HTML) {
130-
template = TEMPLATE_HTML;
131-
} else {
132-
return null;
125+
return TEMPLATE_HTML;
133126
}
127+
return null;
128+
}
134129

135130
// Use empty string when securityResponseId is not present
136131
String replacementValue =

dd-java-agent/agent-bootstrap/src/test/groovy/datadog/trace/bootstrap/blocking/BlockingActionHelperSpecification.groovy

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,4 +213,112 @@ class BlockingActionHelperSpecification extends DDSpecification {
213213
HTML | 'template.html' | '<body>Custom template with security_response_id: [security_response_id]</body>' | 'Custom template with security_response_id: [id]'
214214
JSON | 'template.json' | '{"error":"blocked","id":"[security_response_id]"}' | '"error":"blocked","id":"[id]"'
215215
}
216+
217+
218+
void 'getTemplate with security_response_id replaces placeholder in HTML template'() {
219+
given:
220+
def securityResponseId = '12345678-1234-1234-1234-123456789abc'
221+
222+
when:
223+
def template = BlockingActionHelper.getTemplate(HTML, securityResponseId)
224+
def templateStr = new String(template, StandardCharsets.UTF_8)
225+
226+
then:
227+
templateStr.contains("Security Response ID: ${securityResponseId}")
228+
!templateStr.contains('[security_response_id]')
229+
}
230+
231+
void 'getTemplate with security_response_id replaces placeholder in JSON template'() {
232+
given:
233+
def securityResponseId = '12345678-1234-1234-1234-123456789abc'
234+
235+
when:
236+
def template = BlockingActionHelper.getTemplate(JSON, securityResponseId)
237+
def templateStr = new String(template, StandardCharsets.UTF_8)
238+
239+
then:
240+
templateStr.contains("\"security_response_id\":\"${securityResponseId}\"")
241+
!templateStr.contains('[security_response_id]')
242+
}
243+
244+
void 'getTemplate without security_response_id uses empty string in HTML template'() {
245+
when:
246+
def template = BlockingActionHelper.getTemplate(HTML, null)
247+
def templateStr = new String(template, StandardCharsets.UTF_8)
248+
249+
then:
250+
!templateStr.contains('[security_response_id]')
251+
templateStr.contains('Security Response ID:')
252+
// The placeholder is replaced with empty string
253+
}
254+
255+
void 'getTemplate without security_response_id uses empty string in JSON template'() {
256+
when:
257+
def template = BlockingActionHelper.getTemplate(JSON, null)
258+
def templateStr = new String(template, StandardCharsets.UTF_8)
259+
260+
then:
261+
!templateStr.contains('[security_response_id]')
262+
templateStr.contains('"security_response_id"')
263+
templateStr.contains('""') // Empty string value
264+
}
265+
266+
void 'getTemplate with empty security_response_id uses empty string'() {
267+
when:
268+
def htmlTemplate = BlockingActionHelper.getTemplate(HTML, '')
269+
def jsonTemplate = BlockingActionHelper.getTemplate(JSON, '')
270+
271+
then:
272+
!new String(htmlTemplate, StandardCharsets.UTF_8).contains('[security_response_id]')
273+
!new String(jsonTemplate, StandardCharsets.UTF_8).contains('[security_response_id]')
274+
// Both templates have placeholders replaced with empty string
275+
}
276+
277+
void 'getTemplate with security_response_id works with custom HTML template'() {
278+
setup:
279+
File tempDir = File.createTempDir('testTempDir-', '')
280+
Config config = Mock(Config)
281+
File tempFile = new File(tempDir, 'template.html')
282+
tempFile << '<body>Custom template with security_response_id: [security_response_id]</body>'
283+
def securityResponseId = 'test-block-id-123'
284+
285+
when:
286+
BlockingActionHelper.reset(config)
287+
def template = BlockingActionHelper.getTemplate(HTML, securityResponseId)
288+
def templateStr = new String(template, StandardCharsets.UTF_8)
289+
290+
then:
291+
1 * config.getAppSecHttpBlockedTemplateHtml() >> tempFile.toString()
292+
1 * config.getAppSecHttpBlockedTemplateJson() >> null
293+
templateStr.contains("Custom template with security_response_id: ${securityResponseId}")
294+
!templateStr.contains('[security_response_id]')
295+
296+
cleanup:
297+
BlockingActionHelper.reset(Config.get())
298+
tempDir.deleteDir()
299+
}
300+
301+
void 'getTemplate with security_response_id works with custom JSON template'() {
302+
setup:
303+
File tempDir = File.createTempDir('testTempDir-', '')
304+
Config config = Mock(Config)
305+
File tempFile = new File(tempDir, 'template.json')
306+
tempFile << '{"error":"blocked","id":"[security_response_id]"}'
307+
def securityResponseId = 'test-block-id-456'
308+
309+
when:
310+
BlockingActionHelper.reset(config)
311+
def template = BlockingActionHelper.getTemplate(JSON, securityResponseId)
312+
def templateStr = new String(template, StandardCharsets.UTF_8)
313+
314+
then:
315+
1 * config.getAppSecHttpBlockedTemplateHtml() >> null
316+
1 * config.getAppSecHttpBlockedTemplateJson() >> tempFile.toString()
317+
templateStr.contains("\"error\":\"blocked\",\"id\":\"${securityResponseId}\"")
318+
!templateStr.contains('[security_response_id]')
319+
320+
cleanup:
321+
BlockingActionHelper.reset(Config.get())
322+
tempDir.deleteDir()
323+
}
216324
}

dd-java-agent/instrumentation/grizzly/grizzly-2.0/src/main/java/datadog/trace/instrumentation/grizzly/GrizzlyBlockingHelper.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,17 @@ public static boolean block(
4848
context);
4949
}
5050

51+
public static boolean block(
52+
Request request,
53+
Response response,
54+
int statusCode,
55+
BlockingContentType bct,
56+
Map<String, String> extraHeaders,
57+
String securityResponseId,
58+
Context context) {
59+
return block(request, response, statusCode, bct, extraHeaders, null, context);
60+
}
61+
5162
public static boolean block(
5263
Request request,
5364
Response response,

0 commit comments

Comments
 (0)