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

Handle serialization exceptions in IAST #6102

Merged
merged 1 commit into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -3,9 +3,12 @@
import com.datadog.iast.model.VulnerabilityBatch;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class VulnerabilityEncoding {

private static final Logger log = LoggerFactory.getLogger(VulnerabilityEncoding.class);
private static final int MAX_SPAN_TAG_SIZE = 25000;

static final Moshi MOSHI =
Expand All @@ -18,10 +21,15 @@ public class VulnerabilityEncoding {
MOSHI.adapter(TruncatedVulnerabilities.class);

public static String toJson(final VulnerabilityBatch value) {
String json = BATCH_ADAPTER.toJson(value);
return json.getBytes().length > MAX_SPAN_TAG_SIZE
? getExceededTagSizeJson(new TruncatedVulnerabilities(value.getVulnerabilities()))
: json;
try {
String json = BATCH_ADAPTER.toJson(value);
return json.getBytes().length > MAX_SPAN_TAG_SIZE
? getExceededTagSizeJson(new TruncatedVulnerabilities(value.getVulnerabilities()))
: json;
} catch (Exception ex) {
log.debug("Vulnerability serialization error", ex);
return "{\"vulnerabilities\":[]}";
}
}

static String getExceededTagSizeJson(final TruncatedVulnerabilities truncatedVulnerabilities) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,25 @@ class VulnerabilityEncodingTest extends DDSpecification {

}

void 'exception during serialization is caught'() {
given:
final value = new VulnerabilityBatch()
final type = Mock(VulnerabilityType) {
name() >> { throw new RuntimeException("ERROR") }
}
final vuln = new Vulnerability(type, null, null)
value.add(vuln)

when:
final result = VulnerabilityEncoding.toJson(value)

then:
JSONAssert.assertEquals('''{
"vulnerabilities": [
]
}''', result, true)
}

private static String generateLargeString(){
int targetSize = 25 * 1024
StringBuilder sb = new StringBuilder()
Expand Down
Loading