Skip to content

Commit 1c44c6f

Browse files
authored
Forbidden attempt to set different raw URI for given request context (#10709)
WIP WIP WIP add SEND_TELEMETRY to debug log Merge branch 'master' into issue-appsec-61504 Co-authored-by: alejandro.gonzalez <alejandro.gonzalez@datadoghq.com>
1 parent 6ece63b commit 1c44c6f

File tree

4 files changed

+35
-19
lines changed

4 files changed

+35
-19
lines changed

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/AppSecRequestContext.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -413,11 +413,9 @@ String getSavedRawURI() {
413413
}
414414

415415
void setRawURI(String savedRawURI) {
416-
if (this.savedRawURI != null && this.savedRawURI.compareToIgnoreCase(savedRawURI) != 0) {
417-
throw new IllegalStateException(
418-
"Forbidden attempt to set different raw URI for given request context");
416+
if (this.savedRawURI == null) {
417+
this.savedRawURI = savedRawURI;
419418
}
420-
this.savedRawURI = savedRawURI;
421419
}
422420

423421
public String getRoute() {

dd-java-agent/appsec/src/main/java/com/datadog/appsec/gateway/GatewayBridge.java

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -989,21 +989,28 @@ private Flow<Void> onRequestMethodUriRaw(RequestContext ctx_, String method, URI
989989
}
990990
ctx.setMethod(method);
991991
ctx.setScheme(uri.scheme());
992-
if (uri.supportsRaw()) {
993-
ctx.setRawURI(uri.raw());
994-
} else {
995-
try {
996-
URI encodedUri = new URI(null, null, uri.path(), uri.query(), null);
997-
String q = encodedUri.getRawQuery();
998-
StringBuilder encoded = new StringBuilder();
999-
encoded.append(encodedUri.getRawPath());
1000-
if (null != q && !q.isEmpty()) {
1001-
encoded.append('?').append(q);
992+
if (ctx.getSavedRawURI() == null) {
993+
if (uri.supportsRaw()) {
994+
ctx.setRawURI(uri.raw());
995+
} else {
996+
try {
997+
URI encodedUri = new URI(null, null, uri.path(), uri.query(), null);
998+
String q = encodedUri.getRawQuery();
999+
StringBuilder encoded = new StringBuilder();
1000+
encoded.append(encodedUri.getRawPath());
1001+
if (null != q && !q.isEmpty()) {
1002+
encoded.append('?').append(q);
1003+
}
1004+
ctx.setRawURI(encoded.toString());
1005+
} catch (URISyntaxException e) {
1006+
log.debug("Failed to encode URI '{}{}'", uri.path(), uri.query());
10021007
}
1003-
ctx.setRawURI(encoded.toString());
1004-
} catch (URISyntaxException e) {
1005-
log.debug("Failed to encode URI '{}{}'", uri.path(), uri.query());
10061008
}
1009+
} else {
1010+
log.debug(
1011+
SEND_TELEMETRY,
1012+
"Raw URI already set to '{}'; ignoring new URI callback",
1013+
ctx.getSavedRawURI());
10071014
}
10081015
return maybePublishRequestData(ctx);
10091016
}

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/gateway/AppSecRequestContextSpecification.groovy

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ class AppSecRequestContextSpecification extends DDSpecification {
8181
thrown(IllegalStateException)
8282
}
8383

84-
void 'adding uri a second time is forbidden'() {
84+
void 'setting uri a second time is ignored, first value wins'() {
8585
when:
8686
ctx.rawURI = '/a'
8787
ctx.rawURI = '/b'
8888

8989
then:
90-
thrown(IllegalStateException)
90+
noExceptionThrown()
9191
ctx.savedRawURI == '/a'
9292
}
9393

dd-java-agent/appsec/src/test/groovy/com/datadog/appsec/gateway/GatewayBridgeSpecification.groovy

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,17 @@ class GatewayBridgeSpecification extends DDSpecification {
842842
}
843843

844844

845+
void 'request method URI callback called twice with different URIs does not throw'() {
846+
// Reproduces: https://github.com/DataDog/dd-trace-java/issues/10700
847+
when:
848+
requestMethodURICB.apply(ctx, 'GET', TestURIDataAdapter.create('/a'))
849+
requestMethodURICB.apply(ctx, 'GET', TestURIDataAdapter.create('/b'))
850+
851+
then:
852+
noExceptionThrown()
853+
ctx.data.savedRawURI == '/a'
854+
}
855+
845856
void 'response_start produces appsec context and publishes event'() {
846857
eventDispatcher.getDataSubscribers({
847858
KnownAddresses.RESPONSE_STATUS in it

0 commit comments

Comments
 (0)