-
Notifications
You must be signed in to change notification settings - Fork 18
fix(codeql): batch-4 medium-sev + warnings + log-injection defense #102
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
1d0c15c
fix(codeql): batch-4 warnings + medium-sev cleanup
marcusds 372730d
feat(observability): sanitize newlines in log fields (log-injection d…
marcusds 11c7395
fix(codeql,review): address codex + roundtable findings
marcusds 9692987
chore: re-vendor quickstart SDK + sync 0.0.0.0 noqa suppressions
marcusds 63f386e
fix(review): address coderabbit findings on batch-4 review fixups
marcusds 1bbe461
chore: re-vendor quickstart SDK after socket-with refactor
marcusds 8f344f9
fix(review): round-2 review findings
marcusds a1b62d6
chore: re-vendor quickstart SDK after SO_REUSEADDR change
marcusds 1ba7ac4
refactor(customizer): trim write-failure handling to minimal exit-fro…
marcusds aabfe5b
Merge branch 'main' into fix-codeql-issues-batch-4/mschwab
marcusds 51c3f88
fix(review): address PR feedback on batch-4
marcusds 125fed4
fix(review): use "message" key for validation error to preserve clien…
marcusds 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
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
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
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
88 changes: 88 additions & 0 deletions
88
packages/nmp_common/tests/observability/test_structured_logging.py
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,88 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| """Unit tests for the structured-logging processors.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import logging | ||
|
|
||
| import pytest | ||
| from nmp.common.observability.structured_logging import _sanitize_log_strings | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("raw", "expected"), | ||
| [ | ||
| ("admin\n[ERROR] forged log line", "admin [ERROR] forged log line"), | ||
| ("with\r\ncrlf", "with crlf"), | ||
| ("tab\tis fine", "tab\tis fine"), | ||
| ("plain string", "plain string"), | ||
| ("nel\x85next", "nel next"), | ||
| ("ls\u2028lsep", "ls lsep"), | ||
| ("ps\u2029psep", "ps psep"), | ||
| ], | ||
| ) | ||
| def test_sanitize_log_strings_replaces_newline_variants(raw: str, expected: str) -> None: | ||
| event = {"event": "test", "user": raw} | ||
| result = _sanitize_log_strings(logging.getLogger(), "info", event) | ||
| assert result["user"] == expected | ||
|
|
||
|
|
||
| def test_sanitize_log_strings_leaves_non_string_values_alone() -> None: | ||
| event = {"event": "test", "count": 5, "ok": True, "items": [1, 2, 3]} | ||
| result = _sanitize_log_strings(logging.getLogger(), "info", event) | ||
| assert result == event | ||
|
|
||
|
|
||
| def test_sanitize_log_strings_skips_clean_strings() -> None: | ||
| event = {"event": "test", "name": "default/workspace"} | ||
| result = _sanitize_log_strings(logging.getLogger(), "info", event) | ||
| assert result["name"] == "default/workspace" | ||
|
|
||
|
|
||
| def test_sanitize_log_strings_sanitizes_event_key() -> None: | ||
| """The 'event' key carries the primary log message — must be sanitized too.""" | ||
| event = {"event": "user input was: bad\ninjected line"} | ||
| result = _sanitize_log_strings(logging.getLogger(), "info", event) | ||
| assert result["event"] == "user input was: bad injected line" | ||
|
|
||
|
|
||
| def test_sanitize_log_strings_sanitizes_exception_field() -> None: | ||
| """structlog.format_exc_info writes a multi-line traceback into 'exception'. | ||
|
|
||
| The sanitizer must run after format_exc_info so attacker-controlled | ||
| exception messages can't forge log entries. | ||
| """ | ||
| event = {"event": "boom", "exception": "Traceback (most recent call last):\n ...\nValueError: pwn"} | ||
| result = _sanitize_log_strings(logging.getLogger(), "info", event) | ||
| assert "\n" not in result["exception"] | ||
|
|
||
|
|
||
| def test_initialize_logging_wires_sanitizer_into_chain() -> None: | ||
| """Regression guard: 177 dismissed py/log-injection alerts depend on the sanitizer being in the chain.""" | ||
| import logging as stdlib_logging | ||
|
|
||
| import structlog | ||
| from nmp.common.observability.structured_logging import _sanitize_log_strings, initialize_logging | ||
|
|
||
| root = stdlib_logging.getLogger() | ||
| saved_handlers = list(root.handlers) | ||
| saved_level = root.level | ||
| try: | ||
| root.handlers.clear() | ||
| initialize_logging() | ||
| # Locate the ProcessorFormatter by type, not handler index, so a future | ||
| # refactor that reorders or adds handlers doesn't silently false-pass. | ||
| formatters = [ | ||
| h.formatter for h in root.handlers if isinstance(h.formatter, structlog.stdlib.ProcessorFormatter) | ||
| ] | ||
| assert formatters, "initialize_logging() did not attach a structlog ProcessorFormatter" | ||
| chain = getattr(formatters[0], "foreign_pre_chain", None) or [] | ||
| assert _sanitize_log_strings in chain, ( | ||
| "_sanitize_log_strings missing from structlog chain — log-injection defense is disabled" | ||
| ) | ||
| finally: | ||
| root.handlers.clear() | ||
| root.handlers.extend(saved_handlers) | ||
| root.setLevel(saved_level) |
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
6 changes: 3 additions & 3 deletions
6
sdk/python/nemo-platform/src/nemo_platform/quickstart/preflight.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
6 changes: 3 additions & 3 deletions
6
sdk/python/nemo-platform/src/nemo_platform/quickstart/validators.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
3 changes: 2 additions & 1 deletion
3
sdk/python/nemo-platform/tests/vendored/nemo_platform_ext/config/test_config.py
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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.
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.
Uh oh!
There was an error while loading. Please reload this page.