-
Notifications
You must be signed in to change notification settings - Fork 201
Fix scan alert #151 by testing user-provided CMake arguments in setup.py
#1006
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
Closed
Closed
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0152e9f
Fix scan alert #151 by adding tests to setup.py
mhucka 10d0c1a
Add tests of the new code in setup.py
mhucka 086e901
Merge branch 'main' into fix-code-scan-alert-151
mhucka bce50b4
Apply checks/format-incremental
mhucka 528719d
Need to copy setup.py to Docker location
mhucka 021b28e
Code simplification
mhucka e9eb70e
Slight code simplifications
mhucka 4541964
Apply check/format-incremental
mhucka 87539f4
Merge branch 'main' into fix-code-scan-alert-151
mhucka 305925d
Update setup.py
mhucka 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # Copyright 2026 Google LLC | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # https://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Test setup.py code.""" | ||
|
|
||
| import os | ||
| import subprocess | ||
| import sys | ||
|
|
||
|
|
||
| def run_setup(cmake_args, root_dir): | ||
| env = os.environ.copy() | ||
| env["CMAKE_ARGS"] = cmake_args | ||
|
|
||
| try: | ||
| # Using sys.executable to run setup.py with the same Python executable. | ||
| return subprocess.run( | ||
| [sys.executable, "setup.py", "build_ext"], | ||
| env=env, | ||
| capture_output=True, | ||
| text=True, | ||
| cwd=str(root_dir), | ||
| ) | ||
| except FileNotFoundError as e: | ||
| # This Dummy class emulates what subprocess.run() returns. | ||
| class Dummy: | ||
| stderr = f"Command or file not found: {e}" | ||
| returncode = 1 | ||
|
|
||
| return Dummy() | ||
| except PermissionError as e: | ||
|
|
||
| class Dummy: | ||
| stderr = f"Permission denied: {e}" | ||
| returncode = 1 | ||
|
|
||
| return Dummy() | ||
| except OSError as e: | ||
|
|
||
| class Dummy: | ||
| stderr = f"OS error occurred: {e}" | ||
| returncode = 1 | ||
|
|
||
| return Dummy() | ||
| except Exception as e: | ||
| # Fallback for unexpected errors in subprocess.run itself | ||
| class Dummy: | ||
| stderr = f"Unexpected error: {str(e)}" | ||
| returncode = 1 | ||
|
|
||
| return Dummy() | ||
|
mhucka marked this conversation as resolved.
|
||
|
|
||
|
|
||
| def test_valid_cmake_args(pytestconfig): | ||
| res = run_setup("-DCMAKE_CXX_STANDARD=17", pytestconfig.rootpath) | ||
| # If it fails, it shouldn't be because of our validation. | ||
| assert "arguments must begin with a dash" not in res.stderr | ||
|
|
||
|
|
||
| def test_invalid_cmake_args_no_dash(pytestconfig): | ||
| res = run_setup("NOT_A_FLAG", pytestconfig.rootpath) | ||
| assert "arguments must begin with a dash" in res.stderr | ||
|
|
||
|
|
||
| def test_invalid_cmake_args_malicious(pytestconfig): | ||
| res = run_setup("-DVAR=VAL ; rm -rf /", pytestconfig.rootpath) | ||
| assert "arguments must begin with a dash" in res.stderr | ||
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.
As a general comment, term dummy is problematic and should be avoided in naming things, go/respectful-words.
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.
Oh yes, quite right. Thank you for pointing that out.