Skip to content

Escape control characters in log messages - #7586

Open
davidh44 wants to merge 9 commits into
masterfrom
hdavidh/common-log-sanitizer
Open

davidh44 wants to merge 9 commits into
masterfrom
hdavidh/common-log-sanitizer

Conversation

@davidh44

@davidh44 davidh44 commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Motivation and Context

SDK log statements include values from applications, services or the file system, and these values can contain control characters. This change centralizes control-character escaping in utils and applies it to those log statements while preserving attached stack traces.

#7543 introduced the escaping behavior for S3 Transfer Manager. This change moves that implementation to software.amazon.awssdk.utils.LogSanitizer so SDK modules use the same rules.

Modifications

  1. Move the Transfer Manager LogSanitizer to utils as a protected cross-module utility. Keep its escaping rules unchanged.
  2. Add sanitize(Throwable) for log statements that attach exceptions. It returns safe exceptions unchanged, copies and sanitizes unsafe throwable graphs, and returns a type-identifying fallback if reading a custom throwable fails.
  3. Add a private nested EscapedThrowable class for the copies that sanitize(Throwable) returns. An exception's message cannot be changed in place, so the logger needs a separate object with escaped messages, stack frames, causes and suppressed exceptions. The class skips capturing its own stack trace, and its toString() returns the original class name and escaped message.
  4. Apply the shared sanitizer to affected logs in auth, metrics, SDK core, CRT core, the Netty NIO HTTP client, Amazon S3, Amazon SQS, and S3 Transfer Manager.
  5. Sanitize complete request messages in MakeHttpRequestStage, RetryableStageHelper, and the Netty ResponseHandler, including requests supplied through custom SdkHttpRequest implementations.
  6. Check the log level, for example with isDebugEnabled() or isWarnEnabled(), before each log call that attaches an exception through sanitize(Throwable). Message escaping already runs inside lazy suppliers, but the exception argument is evaluated eagerly, so the check avoids unnecessary work for disabled log levels.
  7. Document the escaping rules in docs/guidelines/logging-guidelines.md.

Commit breakdown

  1. Centralize log sanitization in utils adds the shared utility and moves Transfer Manager to it.
  2. Sanitize common log values in core covers auth, file, metric, app ID and CRT logs.
  3. Sanitize HTTP request logs covers SDK core request logs and Netty.
  4. Sanitize S3 multipart logs covers S3 multipart and presigned Range logs.
  5. Sanitize SQS batch manager logs covers SQS visibility-reset logs.
  6. Document log sanitization in logging guidelines adds the escaping rules to the logging guidelines.

Behavior changes

  1. Control characters in logged values appear as escape sequences. For example, when deleting a file named report\n2026.txt fails:

Before:

ERROR Failed to delete destination file '/tmp/report
2026.txt' after reading the service response failed.

After:

ERROR Failed to delete destination file '/tmp/report\n2026.txt' after reading the service response failed.
  1. Attached exceptions without control characters are logged unchanged. Those with control characters are logged as an EscapedThrowable copy, which Log4j2 and Logback render as software.amazon.awssdk.utils.LogSanitizer$EscapedThrowable: java.io.IOException: bad\nvalue. Tools that filter or group by exception class see EscapedThrowable, and subtype fields such as AwsServiceException.statusCode() are not available.

Testing

Added unit tests.

Screenshots (if appropriate)

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Performance improvement (non-breaking change which improves latency, throughput, or resource use)
  • Deprecation (marks existing functionality as deprecated)
  • Removal (removes previously deprecated functionality)
  • Documentation (changes to documentation only)
  • Other (tests, build, CI, or refactoring with no customer-visible change)

Checklist

  • I have read the CONTRIBUTING document
  • Local run of mvn install succeeds
  • My code follows the code style of this project
  • My change requires a change to the Javadoc documentation
  • I have updated the Javadoc documentation accordingly
  • I have added tests to cover my changes
  • All new and existing tests passed
  • I have added a changelog entry. Adding a new entry must be accomplished by running the scripts/new-change script and following the instructions. Commit the new file created by the script in .changes/next-release with your changes.
  • My change is to implement 1.11 parity feature and I have updated LaunchChangelog

AI tools

  • I wrote this pull request description myself, not with an AI tool
  • I have reviewed all changes in this pull request myself, including any generated by AI tools

License

  • I confirm that this pull request can be released under the Apache 2 license

@davidh44
davidh44 requested a review from a team as a code owner November 24, 2026 22:23
@davidh44
davidh44 force-pushed the hdavidh/common-log-sanitizer branch from af08fc3 to 66e3a91 Compare November 24, 2026 22:56
@davidh44 davidh44 added the no-api-surface-area-change Indicate there is no API surface area change and thus API surface area review is not required label Nov 24, 2026
@davidh44
davidh44 requested a review from dagnir November 24, 2026 23:26

@dagnir dagnir left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent pointed out we might want to escape here too; at least for exception, I don't think date is necessary since it's coming from the service

try {
return Optional.of(DateUtils.parseRfc822Date(serverDate));
} catch (RuntimeException e) {
log.warn(() -> "Unable to parse clock skew offset from response: " + serverDate, e);
return Optional.empty();
}

Comment on lines 283 to 294
public void logBackingOff(Duration backoffDelay) {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Retryable error detected. Will retry in " +
backoffDelay.toMillis() + "ms. Request attempt number " +
attemptNumber, lastException);
}

public void logAcquireFailureBackingOff(Duration acquireFailureBackoffDelay) {
SdkStandardLogger.REQUEST_LOGGER.debug(() -> "Unable to acquire sufficient retry quota to retry. Will cease retrying in "
+ acquireFailureBackoffDelay.toMillis() + "ms. Request attempt number " +
attemptNumber, lastException);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to sanitize lastException too?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, updated to sanitize

return existing;
}

EscapedThrowable result = new EscapedThrowable(describe(throwable));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sanitized logged exceptions get a new type, which could affect existing filters setup by customers. Can we do a short surface API review for this? We should also mention it in the changelog.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated changelog. Sure, will do surface API review

@davidh44 davidh44 removed the no-api-surface-area-change Indicate there is no API surface area change and thus API surface area review is not required label Nov 25, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants