-
Notifications
You must be signed in to change notification settings - Fork 292
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
Propagation of translateEscapes of String class #8186
Merged
sezen-datadog
merged 27 commits into
master
from
sezen.leblay/APPSEC-55380-translateEscapes-propagation
Jan 16, 2025
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
fdda8e1
APPSEC-55380 String taint tracking: translateEscapes
sezen-datadog edbd4c1
TU correction
sezen-datadog 91f4b56
non null correction
sezen-datadog d794020
builds
sezen-datadog 1d28044
add module to StringCallSiteTest
sezen-datadog e16856d
add module to StringCallSiteTest
sezen-datadog fe5078c
smoke test
sezen-datadog 33d3163
settings correction
sezen-datadog 94fb227
test unicode added
sezen-datadog 82e2530
Use env-entry to add tags per webapp deployment (#8138)
amarziali 00c8cd0
fix github issue creation (#8179)
tlhunter 542f3b8
Skip jacoco coverage for internal class (#8183)
amarziali 19bafcb
Merge branch 'master' into sezen.leblay/APPSEC-55380-translateEscapes…
sezen-datadog 3c388a5
smoke test spring boot for jv17
sezen-datadog 70db9f3
whoops
sezen-datadog 1037009
unit test
sezen-datadog 977bff8
Merge branch 'master' into sezen.leblay/APPSEC-55380-translateEscapes…
sezen-datadog ca67979
unit test suppression of equal
sezen-datadog b8c0685
mario's idea for j17 tests
sezen-datadog 30c46d4
mario's idea for j17 tests
sezen-datadog 378bd84
a few more tests
sezen-datadog 65ee3e0
Merge branch 'master' into sezen.leblay/APPSEC-55380-translateEscapes…
sezen-datadog 13965a7
fix formatting
sezen-datadog 4956aeb
fix formatting
sezen-datadog 6013e2a
pr comments
sezen-datadog 28db26f
revert StringModuleTest.groovy
sezen-datadog 44f3db9
add ignore to string translate escapes test if version less than 15
sezen-datadog File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
43 changes: 43 additions & 0 deletions
43
dd-java-agent/instrumentation/java-lang/java-lang-15/build.gradle
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,43 @@ | ||
plugins { | ||
id 'idea' | ||
} | ||
|
||
ext { | ||
minJavaVersionForTests = JavaVersion.VERSION_15 | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
apply plugin: 'call-site-instrumentation' | ||
|
||
muzzle { | ||
pass { | ||
coreJdk() | ||
} | ||
} | ||
|
||
idea { | ||
module { | ||
jdkName = '17' | ||
} | ||
} | ||
|
||
csi { | ||
javaVersion = JavaLanguageVersion.of(17) | ||
} | ||
|
||
addTestSuiteForDir('latestDepTest', 'test') | ||
|
||
dependencies { | ||
testRuntimeOnly project(':dd-java-agent:instrumentation:iast-instrumenter') | ||
} | ||
|
||
project.tasks.withType(AbstractCompile).configureEach { | ||
setJavaVersion(it, 17) | ||
if (it.name != 'compileCsiJava') { | ||
sourceCompatibility = JavaVersion.VERSION_15 | ||
targetCompatibility = JavaVersion.VERSION_15 | ||
if (it instanceof JavaCompile) { | ||
it.options.release.set(15) | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
...a-lang-15/src/main/java/datadog/trace/instrumentation/java/lang/jdk15/StringCallSite.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,27 @@ | ||
package datadog.trace.instrumentation.java.lang.jdk15; | ||
|
||
import datadog.trace.agent.tooling.csi.CallSite; | ||
import datadog.trace.api.iast.IastCallSites; | ||
import datadog.trace.api.iast.InstrumentationBridge; | ||
import datadog.trace.api.iast.Propagation; | ||
import datadog.trace.api.iast.propagation.StringModule; | ||
|
||
@Propagation | ||
@CallSite( | ||
spi = IastCallSites.class, | ||
enabled = {"datadog.trace.api.iast.IastEnabledChecks", "isMajorJavaVersionAtLeast", "15"}) | ||
public class StringCallSite { | ||
@CallSite.After("java.lang.String java.lang.String.translateEscapes()") | ||
public static String afterTranslateEscapes( | ||
@CallSite.This final String self, @CallSite.Return final String result) { | ||
final StringModule module = InstrumentationBridge.STRING; | ||
try { | ||
if (module != null) { | ||
module.onStringTranslateEscapes(self, result); | ||
} | ||
} catch (final Throwable e) { | ||
module.onUnexpectedException("afterTranslateEscapes threw", e); | ||
} | ||
return result; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...5/src/test/groovy/datadog/trace/instrumentation/java/lang/jdk15/StringCallSiteTest.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,40 @@ | ||
package datadog.trace.instrumentation.java.lang.jdk15 | ||
|
||
import com.github.javaparser.utils.StringEscapeUtils | ||
import datadog.trace.agent.test.AgentTestRunner | ||
import datadog.trace.api.iast.InstrumentationBridge | ||
import datadog.trace.api.iast.propagation.StringModule | ||
import foo.bar.TestStringJDK15Suite | ||
import spock.lang.Requires | ||
|
||
@Requires({ | ||
jvm.java15Compatible | ||
}) | ||
class StringCallSiteTest extends AgentTestRunner { | ||
|
||
@Override | ||
protected void configurePreAgent() { | ||
injectSysConfig("dd.iast.enabled", "true") | ||
} | ||
|
||
def 'test string translate escapes call site'() { | ||
setup: | ||
final iastModule = Mock(StringModule) | ||
InstrumentationBridge.registerIastModule(iastModule) | ||
|
||
when: | ||
final result = TestStringJDK15Suite.stringTranslateEscapes(input) | ||
|
||
then: | ||
result == output | ||
1 * iastModule.onStringTranslateEscapes(input, output) | ||
|
||
where: | ||
input | output | ||
"HelloThisisaline" | "HelloThisisaline" | ||
"Hello\tThis is a line" | "Hello"+ StringEscapeUtils.unescapeJava("\\u0009") +"This is a line" | ||
/Hello\sThis is a line/ | "Hello"+ StringEscapeUtils.unescapeJava("\\u0020") +"This is a line" | ||
/Hello\"This is a line/ | "Hello"+ StringEscapeUtils.unescapeJava("\\u0022") +"This is a line" | ||
/Hello\0This is a line/ | "Hello"+ StringEscapeUtils.unescapeJava("\\u0000") +"This is a line" | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...nt/instrumentation/java-lang/java-lang-15/src/test/java/foo/bar/TestStringJDK15Suite.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,18 @@ | ||
package foo.bar; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public abstract class TestStringJDK15Suite { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(TestStringJDK15Suite.class); | ||
|
||
private TestStringJDK15Suite() {} | ||
|
||
public static String stringTranslateEscapes(String self) { | ||
LOGGER.debug("Before string translate escapes {}", self); | ||
final String result = self.translateEscapes(); | ||
LOGGER.debug("After string translate escapes {}", result); | ||
return result; | ||
} | ||
} |
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,35 @@ | ||
plugins { | ||
id 'idea' | ||
id 'java-test-fixtures' | ||
} | ||
|
||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
|
||
description = 'iast-smoke-tests-utils-java-17' | ||
|
||
idea { | ||
module { | ||
jdkName = '17' | ||
} | ||
} | ||
|
||
dependencies { | ||
api project(':dd-smoke-tests') | ||
compileOnly group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.0.RELEASE' | ||
|
||
testFixturesImplementation testFixtures(project(":dd-smoke-tests:iast-util")) | ||
} | ||
|
||
project.tasks.withType(AbstractCompile).configureEach { | ||
setJavaVersion(it, 17) | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
if (it instanceof JavaCompile) { | ||
it.options.release.set(17) | ||
} | ||
} | ||
|
||
forbiddenApisMain { | ||
failOnMissingClasses = false | ||
} |
17 changes: 17 additions & 0 deletions
17
...l-17/src/main/java/datadog/smoketest/springboot/controller/StringOperationController.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,17 @@ | ||
package datadog.smoketest.springboot.controller; | ||
|
||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/string") | ||
public class StringOperationController { | ||
|
||
@PostMapping("/translateEscapes") | ||
public String translateEscapes(@RequestParam(value = "parameter") final String parameter) { | ||
parameter.translateEscapes(); | ||
return "ok"; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...ast-util-17/src/testFixtures/groovy/datadog/smoketest/AbstractIast17SpringBootTest.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,60 @@ | ||
package datadog.smoketest | ||
|
||
import com.github.javaparser.utils.StringEscapeUtils | ||
import okhttp3.FormBody | ||
import okhttp3.Request | ||
|
||
import static datadog.trace.api.config.IastConfig.IAST_DEBUG_ENABLED | ||
import static datadog.trace.api.config.IastConfig.IAST_DETECTION_MODE | ||
import static datadog.trace.api.config.IastConfig.IAST_ENABLED | ||
|
||
abstract class AbstractIast17SpringBootTest extends AbstractIastServerSmokeTest { | ||
|
||
@Override | ||
ProcessBuilder createProcessBuilder() { | ||
String springBootShadowJar = System.getProperty('datadog.smoketest.springboot.shadowJar.path') | ||
|
||
List<String> command = [] | ||
command.add(javaPath()) | ||
command.addAll(defaultJavaProperties) | ||
command.addAll(iastJvmOpts()) | ||
command.addAll((String[]) ['-jar', springBootShadowJar, "--server.port=${httpPort}"]) | ||
ProcessBuilder processBuilder = new ProcessBuilder(command) | ||
processBuilder.directory(new File(buildDirectory)) | ||
// Spring will print all environment variables to the log, which may pollute it and affect log assertions. | ||
processBuilder.environment().clear() | ||
return processBuilder | ||
} | ||
|
||
protected List<String> iastJvmOpts() { | ||
return [ | ||
withSystemProperty(IAST_ENABLED, true), | ||
withSystemProperty(IAST_DETECTION_MODE, 'FULL'), | ||
withSystemProperty(IAST_DEBUG_ENABLED, true), | ||
] | ||
} | ||
|
||
void 'test String translateEscapes'() { | ||
setup: | ||
final url = "http://localhost:${httpPort}/string/translateEscapes" | ||
final body = new FormBody.Builder() | ||
.add('parameter', value) | ||
.build() | ||
final request = new Request.Builder().url(url).post(body).build() | ||
|
||
|
||
when: | ||
client.newCall(request).execute() | ||
|
||
then: | ||
hasTainted { tainted -> | ||
tainted.value == expected | ||
} | ||
|
||
where: | ||
value | expected | ||
"withEscape\ttab" | "withEscape" + Character.toString((char)9) + "tab" | ||
"withEscape\nnewline" | "withEscape" + StringEscapeUtils.unescapeJava("\\u000A")+ "newline" | ||
"withEscape\bbackline" | "withEscape" + StringEscapeUtils.unescapeJava("\\u0008")+ "backline" | ||
} | ||
} |
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,45 @@ | ||
plugins { | ||
id 'java' | ||
id 'org.springframework.boot' version '2.7.15' | ||
id 'io.spring.dependency-management' version '1.0.15.RELEASE' | ||
id 'java-test-fixtures' | ||
} | ||
|
||
ext { | ||
minJavaVersionForTests = JavaVersion.VERSION_17 | ||
} | ||
|
||
apply from: "$rootDir/gradle/java.gradle" | ||
description = 'SpringBoot Java 17 Smoke Tests.' | ||
|
||
repositories { | ||
mavenCentral() | ||
} | ||
|
||
dependencies { | ||
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.2.0.RELEASE' | ||
|
||
testImplementation project(':dd-smoke-tests') | ||
testImplementation testFixtures(project(":dd-smoke-tests:iast-util:iast-util-17")) | ||
testImplementation testFixtures(project(':dd-smoke-tests:iast-util')) | ||
|
||
implementation project(':dd-smoke-tests:iast-util:iast-util-17') | ||
} | ||
|
||
project.tasks.withType(AbstractCompile).configureEach { | ||
setJavaVersion(it, 17) | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
if (it instanceof JavaCompile) { | ||
it.options.release.set(17) | ||
} | ||
} | ||
|
||
forbiddenApisMain { | ||
failOnMissingClasses = false | ||
} | ||
|
||
tasks.withType(Test).configureEach { | ||
dependsOn "bootJar" | ||
jvmArgs "-Ddatadog.smoketest.springboot.shadowJar.path=${tasks.bootJar.archiveFile.get()}" | ||
} |
14 changes: 14 additions & 0 deletions
14
.../springboot-java-17/src/main/java/datadog/smoketest/springboot/SpringbootApplication.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,14 @@ | ||
package datadog.smoketest.springboot; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringbootApplication { | ||
|
||
public static void main(final String[] args) { | ||
SpringApplication.run(SpringbootApplication.class, args); | ||
System.out.println("Started in " + ManagementFactory.getRuntimeMXBean().getUptime() + "ms"); | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
...gboot-java-17/src/test/groovy/datadog/smoketest/springboot/IastSpringBootSmokeTest.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,6 @@ | ||
package datadog.smoketest.springboot | ||
|
||
import datadog.smoketest.AbstractIast17SpringBootTest | ||
|
||
class IastSpringBootSmokeTest extends AbstractIast17SpringBootTest { | ||
} | ||
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need empty test class?