-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fix: harden runtime and supply chain security #6116
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
base: master
Are you sure you want to change the base?
Changes from all commits
91db9e0
4e8f132
1fd0dec
ac497dd
a82d3de
bac50ca
fb65eca
e6eb69e
9ffe3fc
003b1f3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -141,11 +141,14 @@ async def exec( | |
| ) -> dict[str, Any]: | ||
| def _run() -> dict[str, Any]: | ||
| try: | ||
| # nosemgrep: python.lang.security.audit.dangerous-subprocess-use-audit | ||
| # Executes the current interpreter with a fixed argv list and shell=False. | ||
| result = subprocess.run( | ||
| [os.environ.get("PYTHON", sys.executable), "-c", code], | ||
| [sys.executable, "-c", code], | ||
| timeout=timeout, | ||
| capture_output=True, | ||
| text=True, | ||
| shell=False, | ||
| ) | ||
|
Comment on lines
146
to
152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.lang.security.audit.dangerous-subprocess-use-audit): 检测到对 subprocess 函数 Source: opengrep Original comment in Englishsecurity (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. Source: opengrep
Comment on lines
146
to
152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.lang.security.audit.dangerous-subprocess-use-audit): 检测到对子进程函数 来源: opengrep Original comment in Englishsecurity (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. Source: opengrep
Comment on lines
146
to
152
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.lang.security.audit.dangerous-subprocess-use-audit): 检测到对 subprocess 函数 Source: opengrep Original comment in Englishsecurity (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. Source: opengrep
Comment on lines
146
to
152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.lang.security.audit.dangerous-subprocess-use-audit): 检测到对子进程函数 来源:opengrep Original comment in Englishsecurity (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. Source: opengrep
zouyonghe marked this conversation as resolved.
Show resolved
Hide resolved
Comment on lines
146
to
152
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. security (python.lang.security.audit.dangerous-subprocess-use-audit): 检测到对子进程函数 来源:opengrep Original comment in Englishsecurity (python.lang.security.audit.dangerous-subprocess-use-audit): Detected subprocess function 'run' without a static string. If this data can be controlled by a malicious actor, it may be an instance of command injection. Audit the use of this call to ensure it is not controllable by an external resource. You may consider using 'shlex.escape()'. Source: opengrep |
||
| stdout = "" if silent else result.stdout | ||
| stderr = result.stderr if result.returncode != 0 else "" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| import ipaddress | ||
| from urllib.parse import SplitResult, urlsplit, urlunsplit | ||
|
|
||
| _ALLOWED_INSECURE_SUFFIXES = (".local", ".internal") | ||
|
|
||
|
|
||
| def _is_local_or_private_host(hostname: str | None) -> bool: | ||
| if not hostname: | ||
| return False | ||
|
|
||
| normalized = hostname.strip("[]").lower() | ||
| if normalized == "localhost": | ||
| return True | ||
| if normalized.endswith(_ALLOWED_INSECURE_SUFFIXES): | ||
| return True | ||
|
|
||
| try: | ||
| address = ipaddress.ip_address(normalized) | ||
| except ValueError: | ||
| return False | ||
|
|
||
| return address.is_loopback or address.is_private or address.is_link_local | ||
|
|
||
|
|
||
| def require_secure_transport_url( | ||
| url: str, | ||
| *, | ||
| label: str, | ||
| allowed_schemes: set[str], | ||
| ) -> SplitResult: | ||
| parsed = urlsplit(url) | ||
| if parsed.scheme not in allowed_schemes: | ||
| allowed = ", ".join(sorted(allowed_schemes)) | ||
| raise ValueError(f"{label} must use one of: {allowed}") | ||
|
|
||
| if parsed.scheme in {"http", "ws"} and not _is_local_or_private_host( | ||
| parsed.hostname | ||
| ): | ||
| raise ValueError( | ||
| f"{label} must use secure transport (https or wss) for non-local endpoints: {url}", | ||
| ) | ||
|
|
||
| return parsed | ||
|
|
||
|
|
||
| def to_websocket_url(url: str, *, label: str = "WebSocket URL") -> str: | ||
| normalized_url = url.rstrip("/") | ||
| parsed = urlsplit(normalized_url) | ||
| allowed_schemes = {"http", "https", "ws", "wss"} | ||
|
|
||
| if parsed.scheme not in allowed_schemes: | ||
| raise ValueError( | ||
| f"{label} must use the http, https, ws, or wss scheme: {normalized_url}", | ||
| ) | ||
|
|
||
| parsed = require_secure_transport_url( | ||
| normalized_url, | ||
| label=label, | ||
| allowed_schemes=allowed_schemes, | ||
| ) | ||
| scheme_map = { | ||
| "http": "ws", | ||
| "https": "wss", | ||
| "ws": "ws", | ||
| "wss": "wss", | ||
| } | ||
|
|
||
| try: | ||
| ws_scheme = scheme_map[parsed.scheme] | ||
| except KeyError as exc: | ||
| raise ValueError( | ||
| f"{label} must use the http, https, ws, or wss scheme: {normalized_url}", | ||
| ) from exc | ||
|
|
||
| return urlunsplit( | ||
| parsed._replace(scheme=ws_scheme), | ||
| ) |
Uh oh!
There was an error while loading. Please reload this page.