Potential fix for code scanning alert no. 22: Use of externally-controlled format string#3041
Closed
Potential fix for code scanning alert no. 22: Use of externally-controlled format string#3041
Conversation
…olled format string Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Potential fix for https://github.com/apache/drill/security/code-scanning/22
At a high level, the fix is to prevent any user-controlled string from being used as a format string to
String.formator similar APIs. Instead, the formatting helper should either (a) always treat the input as literal text, or (b) if formatting with arguments is desired, use a constant format string that incorporates the user data via%s, not as the pattern itself.The single best fix here is to change
UserException.Builder.message(String format, Object... args)so that it no longer treats the first parameter as a format string. We can preserve the public API (callers still passmessage("x %s y", arg)), but internally we will build the message by concatenating the base text and the argument representations instead of re-invokingString.format. This completely eliminates the externally-controlled format-string sink while keeping behavior close enough for error reporting. Concretely, incommon/src/main/java/org/apache/drill/common/exceptions/UserException.java, in theBuilder.messagemethod, we will replace:with logic that, when
args.length > 0, convertsformatandargsinto a single string without usingString.formaton untrusted input, e.g.,format + " " + Arrays.toString(args). This keeps existing functionality (message still contains both the base text and argument values) and removes the vulnerable call. No other files need code changes for this specific alert; the upstream flows into this method are then harmless, because the method no longer treats the parameter as a format string.Implementation details:
UserException.Builder.messageinUserException.java.java.util.Arraysat the top of the file, as we will useArrays.toString(args)to render the argument list.UserExceptionis changed (we preserve the wrapping semantics, context handling, etc.).BaseOptionManager,QueryResources,QueryWrapper,RestQueryRunner, orStatusResourcesfor this specific CWE; once the sink is safe, those taint flows are no longer exploitable.Suggested fixes powered by Copilot Autofix. Review carefully before merging.