-
Notifications
You must be signed in to change notification settings - Fork 60
FIX: prevent native log format-string injection #791
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
Sumit Sarabhai (sumitmsft)
wants to merge
5
commits into
main
Choose a base branch
from
sumitsar/fix-vfind-149-format-string
base: main
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.
+89
−12
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
eee0ace
FIX: prevent native log format-string injection
sumitmsft 189cfb6
FIX: suppress false positive on printf annotation
sumitmsft e386bf0
Merge branch 'main' into sumitsar/fix-vfind-149-format-string
bewithgaurav ddadd5d
Merge branch 'main' into sumitsar/fix-vfind-149-format-string
bewithgaurav 6b5e047
Merge branch 'main' into sumitsar/fix-vfind-149-format-string
bewithgaurav 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| """Regression guards for native printf-style logging.""" | ||
|
|
||
| import re | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| _PYBIND_DIR = Path(__file__).resolve().parents[1] / "mssql_python" / "pybind" | ||
| _LOGGER_HEADER = _PYBIND_DIR / "logger_bridge.hpp" | ||
| _CMAKE = _PYBIND_DIR / "CMakeLists.txt" | ||
| pytestmark = pytest.mark.skipif( | ||
| not _PYBIND_DIR.is_dir(), | ||
| reason="requires a source checkout; isolated wheel tests omit the source tree", | ||
| ) | ||
|
|
||
|
|
||
| def _code_without_comments(text): | ||
| text = re.sub( | ||
| r"/\*.*?\*/", | ||
| lambda match: "\n" * match.group().count("\n"), | ||
| text, | ||
| flags=re.DOTALL, | ||
| ) | ||
| return "\n".join( | ||
| "" if line.lstrip().startswith("#define LOG") else re.sub(r"//.*", "", line) | ||
| for line in text.splitlines() | ||
| ) | ||
|
|
||
|
|
||
| def test_native_log_calls_use_literal_format_strings(): | ||
| dynamic_calls = [] | ||
| pattern = re.compile(r"\bLOG(?:_INFO|_WARNING|_ERROR)?\s*\(\s*(.)") | ||
| paths = ( | ||
| path | ||
| for suffix in ("*.cpp", "*.hpp", "*.h") | ||
| for path in _PYBIND_DIR.rglob(suffix) | ||
| if "build" not in path.relative_to(_PYBIND_DIR).parts | ||
| ) | ||
| for path in paths: | ||
| code = _code_without_comments(path.read_text(encoding="utf-8")) | ||
| for match in pattern.finditer(code): | ||
| if match.group(1) != '"': | ||
| line_number = code.count("\n", 0, match.start()) + 1 | ||
| source_line = code.splitlines()[line_number - 1].strip() | ||
| dynamic_calls.append( | ||
| f"{path.relative_to(_PYBIND_DIR)}:{line_number}: {source_line}" | ||
| ) | ||
|
|
||
| assert not dynamic_calls, f"native LOG calls must use literal format strings: {dynamic_calls}" | ||
|
|
||
|
|
||
| def test_logger_bridge_enables_compile_time_format_checks(): | ||
| header = _LOGGER_HEADER.read_text(encoding="utf-8") | ||
| cmake = _CMAKE.read_text(encoding="utf-8") | ||
|
|
||
| assert "__attribute__((format(printf, format_index, first_argument)))" in header | ||
| assert "DevSkim: ignore DS154189" in header | ||
| assert "MSSQL_PRINTF_FORMAT(4, 5)" in header | ||
| assert "MSSQL_PRINTF_FORMAT(1, 0)" in header | ||
| assert 'CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"' in cmake | ||
| assert "-Wformat=2" in cmake | ||
| assert "-Werror=format-security" in cmake | ||
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.
suggestion: add a subprocess regression with DEBUG logging enabled and
SELECT CAST(1 AS sql_variant) AS [%s%n%p%%]. assert that it raises the unsupported-type error without crashing and preserves the alias verbatim in the native log.the source guard is useful, but it doesn't exercise the original failure path.