[pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci
This commit is contained in:
pre-commit-ci[bot] 2026-07-19 12:07:48 +00:00
commit 27311984ac
4 changed files with 18 additions and 33 deletions

View file

@ -380,6 +380,7 @@ def _guarded_import_module(name, package = None):
_guard_loaded_httpcore_modules()
return module
def _guard_legacy_source_loader():
cls = importlib.machinery.SourceFileLoader
original = getattr(cls, "load_module", None)
@ -450,12 +451,8 @@ def _make_network_guard_audit():
origin_relative = relpath(origin_path, root).replace("\\", "/")
code_relative = relpath(code_path, root).replace("\\", "/")
return (
(
origin_relative == package
or origin_relative.startswith(f"{package}/")
)
and (code_relative == package or code_relative.startswith(f"{package}/"))
)
origin_relative == package or origin_relative.startswith(f"{package}/")
) and (code_relative == package or code_relative.startswith(f"{package}/"))
except (OSError, ValueError):
return False
return False
@ -483,9 +480,7 @@ def _make_network_guard_audit():
):
frame = frame.f_back
continue
return not (
frame_uses_package(frame, "httpx") or frame_uses_package(frame, "httpcore")
)
return not (frame_uses_package(frame, "httpx") or frame_uses_package(frame, "httpcore"))
return True
def audit(event, args):
@ -494,18 +489,18 @@ def _make_network_guard_audit():
if not isinstance(fullname, str):
return
root = fullname.split(".", 1)[0]
if root in blocked or (
root in direct_blocked and sandbox_requested_import(2)
):
if root in blocked or (root in direct_blocked and sandbox_requested_import(2)):
blocked_error(root)
return
if event not in {"socket.connect", "socket.connect_ex", "socket.getaddrinfo"}:
return
if httpcore_network_active():
return
if package_in_stack("httpcore", 2) or package_in_stack(
"anyio", 2
) or package_in_stack("trio", 2):
if (
package_in_stack("httpcore", 2)
or package_in_stack("anyio", 2)
or package_in_stack("trio", 2)
):
blocked_error("httpcore")
return audit

View file

@ -492,18 +492,13 @@ def _python_payload_launches_startup_bypass(code: str, depth: int) -> bool:
if command_node is None:
continue
nested = _static_python_command_argument(command_node)
if nested is not None and _sandbox_python_startup_bypasses_guard(
nested, depth + 1
):
if nested is not None and _sandbox_python_startup_bypasses_guard(nested, depth + 1):
return True
return False
def _segment_python_launch_bypasses_guard(
segment: list[str],
python_index: int,
environment_tainted: bool,
depth: int,
segment: list[str], python_index: int, environment_tainted: bool, depth: int
) -> bool:
if environment_tainted or _segment_mutates_sandbox_python_env(segment, python_index):
return True
@ -3229,7 +3224,9 @@ def _render_html_computed_network_access(code: str, depth: int = 0) -> bool:
arguments = _js_call_arguments(code, match.end())
if arguments is None:
return True
method = (match.group("insert") or match.group("contextual") or match.group("write")).lower()
method = (
match.group("insert") or match.group("contextual") or match.group("write")
).lower()
if _render_html_markup_call_reaches_network(arguments, method, depth):
return True
@ -6186,11 +6183,7 @@ def _check_signal_escape_patterns(code: str):
def _source_loader_module_name(self, node) -> str | None:
if isinstance(node, ast.Name):
return self.source_loader_names.get(node.id)
if (
isinstance(node, ast.Call)
and self._is_source_file_loader(node.func)
and node.args
):
if isinstance(node, ast.Call) and self._is_source_file_loader(node.func) and node.args:
return self._static_string(node.args[0])
return None

View file

@ -187,7 +187,7 @@ def test_bash_blocklist_enforced_when_sandboxed(captured_popen):
'find . -type f -execdir bash -c "python -I -c import\\ boto3" ;',
'python$IFS-S -c "import boto3"',
"python -c \"import subprocess; subprocess.run(['python','-S','-c','import boto3'])\"",
"python -c \"import os; os.system(\\\"python -S -c 'import boto3'\\\")\"",
'python -c "import os; os.system(\\"python -S -c \'import boto3\'\\")"',
],
)
def test_bash_blocks_python_startup_guard_bypasses(captured_popen, command):

View file

@ -1131,10 +1131,7 @@ def test_render_html_gated_only_when_networked():
is True
)
assert (
rh(
"<script>document.createRange().createContextualFragment("
"'<p>Local</p>')</script>"
)
rh("<script>document.createRange().createContextualFragment('<p>Local</p>')</script>")
is False
)
assert rh("<script>[img.src] = ['https://evil/x']</script>") is True