-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
183 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
dd-java-agent/agent-iast/src/main/java/com/datadog/iast/sink/SessionRewritingModuleImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.datadog.iast.sink; | ||
|
||
import com.datadog.iast.Dependencies; | ||
import com.datadog.iast.IastRequestContext; | ||
import com.datadog.iast.model.Evidence; | ||
import com.datadog.iast.model.VulnerabilityType; | ||
import com.datadog.iast.overhead.Operations; | ||
import datadog.trace.api.gateway.IGSpanInfo; | ||
import datadog.trace.api.iast.IastContext; | ||
import datadog.trace.api.iast.sink.SessionRewritingModule; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentSpan; | ||
import datadog.trace.bootstrap.instrumentation.api.AgentTracer; | ||
import java.util.Map; | ||
import javax.annotation.Nullable; | ||
|
||
public class SessionRewritingModuleImpl extends SinkModuleBase implements SessionRewritingModule { | ||
|
||
private static final String JSESSIONID = ";jsessionid"; | ||
|
||
public SessionRewritingModuleImpl(Dependencies dependencies) { | ||
super(dependencies); | ||
} | ||
|
||
@Override | ||
public void onRequestEnd(IastContext ctx, IGSpanInfo igSpanInfo) { | ||
if (!(ctx instanceof IastRequestContext)) { | ||
return; | ||
} | ||
final IastRequestContext iastRequestContext = (IastRequestContext) ctx; | ||
Map<String, Object> tags = igSpanInfo.getTags(); | ||
String url = (String) tags.get("http.url"); | ||
if (url == null || !url.contains(JSESSIONID)) { | ||
return; | ||
} | ||
if (isIgnorableResponseCode((Integer) tags.get("http.status_code"))) { | ||
return; | ||
} | ||
final AgentSpan span = AgentTracer.activeSpan(); | ||
if (!overheadController.consumeQuota(Operations.REPORT_VULNERABILITY, span)) { | ||
return; | ||
} | ||
String reason = "URL: " + url; | ||
String referrer = iastRequestContext.getReferrer(); | ||
if (referrer != null && !referrer.isEmpty()) { | ||
reason = reason + " Referrer: " + referrer; | ||
} | ||
final Evidence result = new Evidence(reason); | ||
report(span, VulnerabilityType.SESSION_REWRITING, result); | ||
} | ||
|
||
@Override | ||
public boolean isIgnorableResponseCode(@Nullable Integer httpStatus) { | ||
return httpStatus != null && httpStatus >= 400; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
94 changes: 94 additions & 0 deletions
94
...-agent/agent-iast/src/test/groovy/com/datadog/iast/sink/SessionRewritingModuleTest.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
package com.datadog.iast.sink | ||
|
||
import com.datadog.iast.IastModuleImplTestBase | ||
import com.datadog.iast.Reporter | ||
import com.datadog.iast.RequestEndedHandler | ||
import com.datadog.iast.model.Vulnerability | ||
import com.datadog.iast.model.VulnerabilityType | ||
import datadog.trace.api.gateway.Flow | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.sink.InsecureAuthProtocolModule | ||
import datadog.trace.api.iast.sink.SessionRewritingModule | ||
import datadog.trace.api.internal.TraceSegment | ||
|
||
class SessionRewritingModuleTest extends IastModuleImplTestBase{ | ||
|
||
private static final REFERRER_URL = "https://localhost:8080/insecure/login.html" | ||
private static final JSESSIONID_URL = REFERRER_URL + ";jsessionid=1A530637289A03B07199A44E8D531427" | ||
private static final EVIDENCE = "URL: "+ JSESSIONID_URL | ||
private static final REFERRER_EVIDENCE = EVIDENCE + " Referrer: " + REFERRER_URL | ||
|
||
private SessionRewritingModule module | ||
|
||
def setup() { | ||
InstrumentationBridge.clearIastModules() | ||
module = new SessionRewritingModuleImpl(dependencies) | ||
InstrumentationBridge.registerIastModule(module) | ||
} | ||
|
||
@Override | ||
protected Reporter buildReporter() { | ||
return Mock(Reporter) | ||
} | ||
|
||
@Override | ||
protected TraceSegment buildTraceSegment() { | ||
return Mock(TraceSegment) | ||
} | ||
|
||
void 'check session rewriting'() { | ||
given: | ||
final handler = new RequestEndedHandler(dependencies) | ||
span.getTags() >> [ | ||
'http.status_code': status_code, | ||
'http.url' : url | ||
] | ||
if(referrer != null){ | ||
ctx.referrer = referrer | ||
} | ||
|
||
|
||
when: | ||
def flow = handler.apply(reqCtx, span) | ||
|
||
then: | ||
flow.getAction() == Flow.Action.Noop.INSTANCE | ||
flow.getResult() == null | ||
1 * traceSegment.setTagTop("_dd.iast.enabled", 1) | ||
if (expected != null) { | ||
1 * reporter.report(_, _) >> { args -> assertEvidence(args[1] as Vulnerability, expected) } | ||
} else { | ||
0 * reporter.report(_, _) | ||
} | ||
|
||
where: | ||
url | referrer | status_code | expected | ||
JSESSIONID_URL | REFERRER_URL | 200 | REFERRER_EVIDENCE | ||
JSESSIONID_URL | null | 200 | EVIDENCE | ||
JSESSIONID_URL | null | 300 | EVIDENCE | ||
JSESSIONID_URL | null | 400 | null | ||
REFERRER_URL | REFERRER_URL | 200 | null | ||
} | ||
|
||
void 'ignore if context is null'(){ | ||
when: | ||
module.onRequestEnd(null, null) | ||
|
||
then: | ||
0 * _ | ||
} | ||
|
||
|
||
private static void assertVulnerability(final Vulnerability vuln) { | ||
assert vuln != null | ||
assert vuln.getType() == VulnerabilityType.SESSION_REWRITING | ||
assert vuln.getLocation() != null | ||
} | ||
|
||
private static void assertEvidence(final Vulnerability vuln, final String expected) { | ||
assertVulnerability(vuln) | ||
final evidence = vuln.getEvidence() | ||
assert evidence != null | ||
assert evidence.value == expected | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
internal-api/src/main/java/datadog/trace/api/iast/sink/SessionRewritingModule.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
package datadog.trace.api.iast.sink; | ||
|
||
public interface SessionRewritingModule extends HttpRequestEndModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters