-
Notifications
You must be signed in to change notification settings - Fork 315
Fix IllegalFormatConversionException StringModuleImpl#onStringFormat #9907
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
Open
jandro996
wants to merge
8
commits into
master
Choose a base branch
from
alejandro.gonzalez/IllegalFormatConversionException-StringModuleImpl#onStringFormat
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21404c9
Fix
jandro996 bdda8b1
New approach
jandro996 083da25
remove tests
jandro996 5da0e58
sef review
jandro996 0b52325
sef review
jandro996 5d2e1b9
Merge branch 'master' into alejandro.gonzalez/IllegalFormatConversion…
jandro996 a7900ad
review
jandro996 e0cf863
Merge branch 'master' into alejandro.gonzalez/IllegalFormatConversion…
jandro996 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 hidden or 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 hidden or 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
147 changes: 147 additions & 0 deletions
147
...la/src/test/groovy/datadog/trace/instrumentation/scala/StringOpsFormatCallSiteTest.groovy
This file contains hidden or 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,147 @@ | ||
| package datadog.trace.instrumentation.scala | ||
|
|
||
| import datadog.trace.api.iast.InstrumentationBridge | ||
| import datadog.trace.api.iast.propagation.StringModule | ||
|
|
||
| /** | ||
| * Tests for StringOpsCallSite.unwrapScalaNumbers functionality. | ||
| * | ||
| * These tests verify that Scala ScalaNumber types (BigDecimal, BigInt) are properly | ||
| * unwrapped to their underlying Java representations (java.math.BigDecimal, java.math.BigInteger) | ||
| * before being passed to IAST's onStringFormat. | ||
| * | ||
| * This prevents IllegalFormatConversionException and ensures correct taint tracking. | ||
| */ | ||
| class StringOpsFormatCallSiteTest extends AbstractIastScalaTest { | ||
|
|
||
| @Override | ||
| String suiteName() { | ||
| return 'foo.bar.TestStringOpsFormatSuite' | ||
| } | ||
|
|
||
| void 'test formatBigDecimal with scala.math.BigDecimal'() { | ||
| setup: | ||
| final iastModule = Mock(StringModule) | ||
| InstrumentationBridge.registerIastModule(iastModule) | ||
|
|
||
| when: | ||
| final result = testSuite.formatBigDecimal('123.456') | ||
|
|
||
| then: | ||
| result == 'Value: 123.456000' | ||
| // Verify that the unwrapped java.math.BigDecimal is passed, not scala.math.BigDecimal | ||
| 1 * iastModule.onStringFormat( | ||
| 'Value: %f', | ||
| { Object[] args -> | ||
| args.length == 1 && | ||
| args[0] != null && | ||
| args[0].getClass() == BigDecimal && | ||
| args[0].toString() == '123.456' | ||
| }, | ||
| 'Value: 123.456000' | ||
| ) | ||
| 0 * iastModule.onUnexpectedException(_, _) | ||
| } | ||
|
|
||
| void 'test formatBigInt with scala.math.BigInt'() { | ||
| setup: | ||
| final iastModule = Mock(StringModule) | ||
| InstrumentationBridge.registerIastModule(iastModule) | ||
|
|
||
| when: | ||
| final result = testSuite.formatBigInt('12345') | ||
|
|
||
| then: | ||
| result == 'Count: 12345' | ||
| // Verify that the unwrapped java.math.BigInteger is passed, not scala.math.BigInt | ||
| 1 * iastModule.onStringFormat( | ||
| 'Count: %d', | ||
| { Object[] args -> | ||
| args.length == 1 && | ||
| args[0] != null && | ||
| args[0].getClass() == BigInteger && | ||
| args[0].toString() == '12345' | ||
| }, | ||
| 'Count: 12345' | ||
| ) | ||
| 0 * iastModule.onUnexpectedException(_, _) | ||
| } | ||
|
|
||
| void 'test formatMultipleScalaNumbers with BigDecimal and BigInt'() { | ||
| setup: | ||
| final iastModule = Mock(StringModule) | ||
| InstrumentationBridge.registerIastModule(iastModule) | ||
|
|
||
| when: | ||
| final result = testSuite.formatMultipleScalaNumbers('99.99', '42') | ||
|
|
||
| then: | ||
| result == 'Decimal: 99.990000, Integer: 42' | ||
| // Verify that both ScalaNumbers are unwrapped | ||
| 1 * iastModule.onStringFormat( | ||
| 'Decimal: %f, Integer: %d', | ||
| { Object[] args -> | ||
| args.length == 2 && | ||
| args[0] != null && | ||
| args[0].getClass() == BigDecimal && | ||
| args[0].toString() == '99.99' && | ||
| args[1] != null && | ||
| args[1].getClass() == BigInteger && | ||
| args[1].toString() == '42' | ||
| }, | ||
| 'Decimal: 99.990000, Integer: 42' | ||
| ) | ||
| 0 * iastModule.onUnexpectedException(_, _) | ||
| } | ||
|
|
||
| void 'test formatMixed with BigDecimal and String'() { | ||
| setup: | ||
| final iastModule = Mock(StringModule) | ||
| InstrumentationBridge.registerIastModule(iastModule) | ||
|
|
||
| when: | ||
| final result = testSuite.formatMixed('3.14', 'hello') | ||
|
|
||
| then: | ||
| result == 'Value: 3.140000, Text: hello' | ||
| // Verify that BigDecimal is unwrapped but String remains unchanged | ||
| 1 * iastModule.onStringFormat( | ||
| 'Value: %f, Text: %s', | ||
| { Object[] args -> | ||
| args.length == 2 && | ||
| args[0] != null && | ||
| args[0].getClass() == BigDecimal && | ||
| args[0].toString() == '3.14' && | ||
| args[1] instanceof String && | ||
| args[1] == 'hello' | ||
| }, | ||
| 'Value: 3.140000, Text: hello' | ||
| ) | ||
| 0 * iastModule.onUnexpectedException(_, _) | ||
| } | ||
|
|
||
| void 'test formatString with regular String arguments (no unwrapping)'() { | ||
| setup: | ||
| final iastModule = Mock(StringModule) | ||
| InstrumentationBridge.registerIastModule(iastModule) | ||
|
|
||
| when: | ||
| final result = testSuite.formatString('left', 'right') | ||
|
|
||
| then: | ||
| result == 'Left: left, Right: right' | ||
| // Verify that String arguments are passed unchanged (no unwrapping needed) | ||
| 1 * iastModule.onStringFormat( | ||
| 'Left: %s, Right: %s', | ||
| { Object[] args -> | ||
| args.length == 2 && | ||
| args[0] instanceof String && | ||
| args[0] == 'left' && | ||
| args[1] instanceof String && | ||
| args[1] == 'right' | ||
| }, | ||
| 'Left: left, Right: right' | ||
| ) | ||
| 0 * iastModule.onUnexpectedException(_, _) | ||
| } | ||
| } |
63 changes: 63 additions & 0 deletions
63
dd-java-agent/instrumentation/scala/src/test/scala/foo/bar/TestStringOpsFormatSuite.scala
This file contains hidden or 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,63 @@ | ||
| package foo.bar | ||
|
|
||
| import org.slf4j.LoggerFactory | ||
|
|
||
| class TestStringOpsFormatSuite { | ||
|
|
||
| private val LOGGER = LoggerFactory.getLogger("TestStringOpsFormatSuite") | ||
|
|
||
| /** Test format with scala.math.BigDecimal using %f placeholder. | ||
| */ | ||
| def formatBigDecimal(value: String): String = { | ||
| LOGGER.debug("Before formatBigDecimal {}", value) | ||
| val bd = BigDecimal(value) | ||
| val pattern = "Value: %f" | ||
| val result: String = pattern.format(bd) | ||
| LOGGER.debug("After formatBigDecimal {}", result) | ||
| result | ||
| } | ||
|
|
||
| /** Test format with scala.math.BigInt using %d placeholder. | ||
| */ | ||
| def formatBigInt(value: String): String = { | ||
| LOGGER.debug("Before formatBigInt {}", value) | ||
| val bi = BigInt(value) | ||
| val pattern = "Count: %d" | ||
| val result: String = pattern.format(bi) | ||
| LOGGER.debug("After formatBigInt {}", result) | ||
| result | ||
| } | ||
|
|
||
| /** Test format with multiple ScalaNumber arguments. | ||
| */ | ||
| def formatMultipleScalaNumbers(decimal: String, integer: String): String = { | ||
| LOGGER.debug("Before formatMultipleScalaNumbers {} {}", Array(decimal, integer): _*) | ||
| val bd = BigDecimal(decimal) | ||
| val bi = BigInt(integer) | ||
| val pattern = "Decimal: %f, Integer: %d" | ||
| val result: String = pattern.format(bd, bi) | ||
| LOGGER.debug("After formatMultipleScalaNumbers {}", result) | ||
| result | ||
| } | ||
|
|
||
| /** Test format with mixed ScalaNumber and regular arguments. | ||
| */ | ||
| def formatMixed(decimal: String, text: String): String = { | ||
| LOGGER.debug("Before formatMixed {} {}", Array(decimal, text): _*) | ||
| val bd = BigDecimal(decimal) | ||
| val pattern = "Value: %f, Text: %s" | ||
| val result: String = pattern.format(bd, text) | ||
| LOGGER.debug("After formatMixed {}", result) | ||
| result | ||
| } | ||
|
|
||
| /** Test format with regular String arguments (no unwrapping needed). | ||
| */ | ||
| def formatString(left: String, right: String): String = { | ||
| LOGGER.debug("Before formatString {} {}", Array(left, right): _*) | ||
| val pattern = "Left: %s, Right: %s" | ||
| val result: String = pattern.format(left, right) | ||
| LOGGER.debug("After formatString {}", result) | ||
| result | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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.
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.
I've changed scala version just to reproduce the same stacktrace reported in the issue tracker. I feel that is not necessary to duplicate this test as is also covered by unit testing