-
Notifications
You must be signed in to change notification settings - Fork 70
feat: add sanitized stack trace reporting to telemetry error events #1080
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
vaish-gujjari
wants to merge
2
commits into
aws-deadline:mainline
Choose a base branch
from
vaish-gujjari:stack-trace-reporting
base: mainline
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| """ | ||
| Stack trace sanitizer for Deadline Cloud client telemetry. | ||
|
|
||
| Uses an allowlist approach: only explicitly chosen fields (sanitized filename, | ||
| line number, function name, exception type) are emitted. Source code context | ||
| and exception messages are intentionally omitted as they could contain | ||
| customer data. | ||
|
|
||
| Conforms to ADR 2024-02-19: "No customer content or other information provided | ||
| by the customer can be submitted, such as bucket names, file names, or similar." | ||
| """ | ||
|
|
||
| import traceback | ||
| from typing import FrozenSet, List | ||
|
|
||
| # Packages we control — safe to include relative paths for | ||
| _KNOWN_PACKAGES: FrozenSet[str] = frozenset( | ||
| { | ||
| "deadline", | ||
| "openjd", | ||
| "boto3", | ||
| "botocore", | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def _sanitize_path(filepath: str) -> str: | ||
| """Replace a full file path with the package-relative portion or bare filename.""" | ||
| if filepath.startswith("<"): | ||
| return filepath | ||
|
|
||
| parts = filepath.replace("\\", "/").split("/") | ||
|
|
||
| for i, part in enumerate(parts): | ||
| stem = part.split(".")[0] | ||
| if stem in _KNOWN_PACKAGES: | ||
| return "/".join(parts[i:]) | ||
|
|
||
| for i, part in enumerate(parts): | ||
| if part == "site-packages" and i + 1 < len(parts): | ||
| return "/".join(parts[i + 1 :]) | ||
|
|
||
| return parts[-1] | ||
|
|
||
|
|
||
| def _sanitize_traceback(te: traceback.TracebackException) -> List[str]: | ||
| """Recursively format a TracebackException chain using only allowlisted fields.""" | ||
| lines: List[str] = [] | ||
|
|
||
| # Handle chained exceptions (cause or context) | ||
| if te.__cause__ is not None: | ||
| lines.extend(_sanitize_traceback(te.__cause__)) | ||
| lines.append("\nThe above exception was the direct cause of the following exception:\n") | ||
| elif te.__context__ is not None and not te.__suppress_context__: | ||
| lines.extend(_sanitize_traceback(te.__context__)) | ||
| lines.append("\nDuring handling of the above exception, another exception occurred:\n") | ||
|
|
||
| lines.append("Traceback (most recent call last):") | ||
| for frame in te.stack: | ||
| safe_path = _sanitize_path(frame.filename) | ||
| lines.append(f' File "{safe_path}", line {frame.lineno}, in {frame.name}') | ||
| # Intentionally omit frame.line — source code context could | ||
| # contain credentials, customer data, or other sensitive values | ||
|
|
||
| # Only emit the exception type, not the message | ||
| exc_name = te.exc_type.__qualname__ if te.exc_type else "UnknownException" | ||
| lines.append(exc_name) | ||
|
|
||
| return lines | ||
|
|
||
|
|
||
| def sanitize_exception(exc: BaseException) -> str: | ||
| """Format and sanitize a live exception using only allowlisted fields.""" | ||
| te = traceback.TracebackException.from_exception(exc) | ||
| return "\n".join(_sanitize_traceback(te)) | ||
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
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.
Instead of this denylist approach (taking the whole exception, putting into a string and then looking for things to take out), consider an allowlist approach. You could use something like
traceback.extract_tb()to work with structure data instead, and only emit the fields you explicitly choose.This would look something like:
This gives you filename, lineno, and name as discrete values you can sanitize individually, and it avoids passing through anything unexpected.
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.
You would need to consider chained exceptions as well, but
traceback.TracebackExceptionshould have the full chain.