pyddock 0.3.1__tar.gz → 0.3.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyddock-0.3.1 → pyddock-0.3.3}/PKG-INFO +1 -1
- {pyddock-0.3.1 → pyddock-0.3.3}/pyproject.toml +1 -1
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_base.py +37 -10
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_fs_enforcement.py +28 -5
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/default_config.toml +4 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/shell_executor.py +41 -12
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_jailbreak_regression.py +106 -3
- {pyddock-0.3.1 → pyddock-0.3.3}/uv.lock +1 -1
- {pyddock-0.3.1 → pyddock-0.3.3}/.gitignore +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/.gitignore +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/106d94c8123c0256 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/1685e03b3aa38c02 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/1deca112ec6f2f3c +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/2488a3a95c48acfb +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/2a94d83a13979745 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/499f1d5a639a382a +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/4ee6f24fa151b65d +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/5b1b2189b70f5ef2 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/5d6f8b26b2137fe1 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/61f9b905d7da14e0 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/67a7f4ab2d372f23 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/6c6f5083ece3f86b +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/827e18b40f235443 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/8401e4039d635fbc +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/8490e7baefc8c8c3 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/a7c132b836381d0d +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/b395547bc71e5c52 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/constants/ca50661510a40488 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/tmp/tmpl8oe65gb +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/tmp/tmpqwper0dp +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/unicode_data/15.1.0/charmap.json.gz +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/.hypothesis/unicode_data/15.1.0/codec-utf-8.json.gz +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/LICENSE +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/README.md +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/publish-to-pypi.ps1 +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/__init__.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_audit_enforcement.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_enforcement.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_import_hook.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_prewarm.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_process_utils.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_proxies.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_runtime.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/_subprocess_patch.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/ast_validator.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/cli.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/config.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/executor.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/script_registry.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/server.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/__init__.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/fs_append.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/fs_delete.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/read_file.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/stat_file.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/tools/str_replace.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/src/pyddock/venv_manager.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/__init__.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/_config_helpers.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_ast_properties.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_attr_reflection_hardening.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_audit_hook_backstop.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_chmod_integration.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_config.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_deny_always_wins.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_deny_messages.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_env_policy.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_executor.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_file_tools.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_filesystem_guards.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_proxy_properties.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_realistic.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_runtime.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_sandbox_escape_hardening.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_security_guards.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_server.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_shell_arg_paths.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_shell_executor.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_shell_properties.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_subprocess_enforcement.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_subprocess_properties.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_subst_trusted_path.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_trusted_prefixes_uv_cache.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_venv_stamp.py +0 -0
- {pyddock-0.3.1 → pyddock-0.3.3}/tests/test_workspace_imports.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyddock
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.3
|
|
4
4
|
Summary: MCP server that gives AI agents pre-approved Python, shell, and file I/O tools with declarative policy controls
|
|
5
5
|
Project-URL: Homepage, https://github.com/portablestew/pyddock
|
|
6
6
|
Project-URL: Repository, https://github.com/portablestew/pyddock
|
|
@@ -36,25 +36,39 @@ del _os_for_path
|
|
|
36
36
|
_ORIGINALS: dict[str, Any] = {}
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
def
|
|
40
|
-
"""
|
|
39
|
+
def ntfs_stream_base(path: Any) -> str | None:
|
|
40
|
+
"""Return the base path of an NTFS alternate-data-stream (ADS) reference.
|
|
41
41
|
|
|
42
42
|
On Windows, ``name:stream`` (and ``name::$DATA``, ``dir:stream``) addresses a
|
|
43
43
|
*different* securable object than the path's lexical components imply: a write
|
|
44
44
|
to ``.pyddock:pwned`` lands a data stream on the ``.pyddock`` directory, yet
|
|
45
45
|
the lexical leaf ``.pyddock:pwned`` is not a child of ``.pyddock`` — so a
|
|
46
46
|
``relative_to('.pyddock')`` containment check decides "not protected" and the
|
|
47
|
-
write slips through.
|
|
48
|
-
(a not-yet-existing stream resolves to the literal), so the only sound defense
|
|
49
|
-
is to reject the syntax up front.
|
|
47
|
+
write slips through.
|
|
50
48
|
|
|
51
49
|
A ``:`` is only legal as the drive designator at index 1 of the FIRST path
|
|
52
50
|
component (``C:\\...`` or the drive-relative ``C:rest``). A ``:`` anywhere else
|
|
53
|
-
|
|
51
|
+
opens a named stream on the component to its left. When such a reference is
|
|
52
|
+
present this returns the lexical BASE — everything up to (but not including)
|
|
53
|
+
the stream ``:`` — i.e. the object the stream would attach to. Returns None
|
|
54
|
+
when no stream reference is present, and on non-Windows, where ``:`` is an
|
|
54
55
|
ordinary, legal filename character.
|
|
56
|
+
|
|
57
|
+
Callers existence-check the resolved base to decide whether the reference is
|
|
58
|
+
security-relevant: the ADS containment-evasion attack aliases a stream onto
|
|
59
|
+
an *existing* (and typically protected) object — ``.pyddock``, a workspace
|
|
60
|
+
module dir, the stdlib — all of which always exist on disk. A colon in a
|
|
61
|
+
string that merely looks path-like (e.g. a commit message) has a base that
|
|
62
|
+
does not exist, so it is not a real stream reference.
|
|
63
|
+
|
|
64
|
+
NOTE: this is purely lexical and makes NO exception for URL-looking strings
|
|
65
|
+
(``https://...``); a ``scheme:`` prefix still carries a stream-bearing ``:``.
|
|
66
|
+
The one place a URL is a legitimate value — a shell CLI argument — filters
|
|
67
|
+
URLs out *before* they reach this check (see ``_looks_like_path`` in
|
|
68
|
+
shell_executor), so URLs never get here in the first place.
|
|
55
69
|
"""
|
|
56
70
|
if _os_name != "nt":
|
|
57
|
-
return
|
|
71
|
+
return None
|
|
58
72
|
parts = str(path).replace("/", "\\").split("\\")
|
|
59
73
|
for idx, comp in enumerate(parts):
|
|
60
74
|
if not comp:
|
|
@@ -63,9 +77,22 @@ def has_ntfs_stream(path: Any) -> bool:
|
|
|
63
77
|
# First component may carry a 'X:' drive designator (X: or X:rest).
|
|
64
78
|
if idx == 0 and len(comp) >= 2 and comp[0].isalpha() and comp[1] == ":":
|
|
65
79
|
start = 2
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
80
|
+
ci = comp.find(":", start)
|
|
81
|
+
if ci != -1:
|
|
82
|
+
base_parts = parts[:idx] + [comp[:ci]]
|
|
83
|
+
return "\\".join(base_parts)
|
|
84
|
+
return None
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def has_ntfs_stream(path: Any) -> bool:
|
|
88
|
+
"""Pure-syntactic NTFS ADS detector — True if *path* carries a stream ``:``.
|
|
89
|
+
|
|
90
|
+
See ``ntfs_stream_base`` for the parsing rules. This is the lexical primitive
|
|
91
|
+
(existence-agnostic). Enforcement layers DO NOT call this directly: they
|
|
92
|
+
existence-gate via ``ntfs_stream_base`` so a colon in a non-path string does
|
|
93
|
+
not raise a false positive. Returns False on non-Windows.
|
|
94
|
+
"""
|
|
95
|
+
return ntfs_stream_base(path) is not None
|
|
69
96
|
|
|
70
97
|
|
|
71
98
|
def canonical_path(path: Any) -> pathlib.Path:
|
|
@@ -14,7 +14,7 @@ import tempfile as _tempfile_module
|
|
|
14
14
|
import _io as _cio_module
|
|
15
15
|
from typing import Any
|
|
16
16
|
|
|
17
|
-
from pyddock._base import _ORIGINALS, _PYDDOCK_DIR, _find_deny_hint, canonical_path,
|
|
17
|
+
from pyddock._base import _ORIGINALS, _PYDDOCK_DIR, _find_deny_hint, canonical_path, ntfs_stream_base
|
|
18
18
|
from pyddock._import_hook import _caller_is_trusted
|
|
19
19
|
from pyddock._audit_enforcement import install_audit_enforcement
|
|
20
20
|
from pyddock._process_utils import make_child_env
|
|
@@ -54,16 +54,30 @@ def apply_filesystem_scoping(
|
|
|
54
54
|
"""
|
|
55
55
|
return canonical_path(p)
|
|
56
56
|
|
|
57
|
+
def _resolve_scope_path(p: str) -> pathlib.Path:
|
|
58
|
+
"""Resolve a scope path entry to an absolute, canonicalized path.
|
|
59
|
+
|
|
60
|
+
Supports:
|
|
61
|
+
- "~" or "~/..." — expanded via os.path.expanduser
|
|
62
|
+
- Absolute paths (e.g. "C:\\Users\\...") — used as-is
|
|
63
|
+
- Relative paths (e.g. ".", "src") — joined with workspace_root
|
|
64
|
+
"""
|
|
65
|
+
expanded = _real_os.path.expanduser(p)
|
|
66
|
+
path = pathlib.Path(expanded)
|
|
67
|
+
if path.is_absolute():
|
|
68
|
+
return _abspath(path)
|
|
69
|
+
return _abspath(workspace_root / expanded)
|
|
70
|
+
|
|
57
71
|
# Canonicalize allowed paths (realpath: resolves symlinks/junctions/8.3
|
|
58
72
|
# names) so they compare consistently with canonicalized candidate paths.
|
|
59
73
|
resolved_writable = [
|
|
60
|
-
|
|
74
|
+
_resolve_scope_path(p) for p in writable_paths
|
|
61
75
|
]
|
|
62
76
|
|
|
63
77
|
# "*" means unrestricted reads
|
|
64
78
|
unrestricted_reads = "*" in readable_paths
|
|
65
79
|
resolved_readable = [
|
|
66
|
-
|
|
80
|
+
_resolve_scope_path(p) for p in readable_paths if p != "*"
|
|
67
81
|
]
|
|
68
82
|
|
|
69
83
|
# .pyddock/ is always excluded from writes (self-modification protection)
|
|
@@ -243,7 +257,12 @@ def apply_filesystem_scoping(
|
|
|
243
257
|
path = _real_os.fsdecode(path)
|
|
244
258
|
if _is_null_device(path):
|
|
245
259
|
return
|
|
246
|
-
|
|
260
|
+
# NTFS alternate data stream whose base object EXISTS — aliasing a
|
|
261
|
+
# stream onto an existing (often protected) object evades containment
|
|
262
|
+
# checks. Gated on existence to avoid false positives on colon-bearing
|
|
263
|
+
# strings whose base does not exist.
|
|
264
|
+
_stream_base = ntfs_stream_base(path)
|
|
265
|
+
if _stream_base is not None and _real_os.path.exists(_stream_base):
|
|
247
266
|
raise PermissionError(
|
|
248
267
|
f"PermissionError: Cannot read '{path}' — NTFS alternate data "
|
|
249
268
|
f"streams (':' in a path component) are not permitted. Use a "
|
|
@@ -271,7 +290,11 @@ def apply_filesystem_scoping(
|
|
|
271
290
|
path = _real_os.fsdecode(path)
|
|
272
291
|
if _is_null_device(path):
|
|
273
292
|
return
|
|
274
|
-
|
|
293
|
+
# NTFS alternate data stream whose base object EXISTS (see _check_read).
|
|
294
|
+
# The base of '.pyddock:x' is '.pyddock' (always present) — caught; a
|
|
295
|
+
# colon in a non-existent-base string is not a real stream reference.
|
|
296
|
+
_stream_base = ntfs_stream_base(path)
|
|
297
|
+
if _stream_base is not None and _real_os.path.exists(_stream_base):
|
|
275
298
|
raise PermissionError(
|
|
276
299
|
f"PermissionError: Cannot write to '{path}' — NTFS alternate "
|
|
277
300
|
f"data streams (':' in a path component) are not permitted. A "
|
|
@@ -69,7 +69,9 @@ P4 = "p4python" # Restricted to non-submit operations
|
|
|
69
69
|
# Filesystem scoping for run_python.
|
|
70
70
|
# Paths are relative to the workspace root (the --workspace directory or CWD).
|
|
71
71
|
# "." = the workspace directory itself (and everything under it)
|
|
72
|
+
# "~" = user home directory (os.path.expanduser); "~/subdir" works too
|
|
72
73
|
# "*" = unrestricted (entire host filesystem)
|
|
74
|
+
# Absolute paths (e.g. "C:\\Data") are also accepted and used as-is.
|
|
73
75
|
# Writes are always denied to .pyddock/ (self-modification protection) and to
|
|
74
76
|
# paths matching shell-executable script patterns (write-then-execute prevention).
|
|
75
77
|
[filesystem]
|
|
@@ -90,6 +92,8 @@ readable_paths = ["*"]
|
|
|
90
92
|
'/\.ssh/' = "deny-agent" # SSH keys — paramiko/GitPython may need access
|
|
91
93
|
'/\.aws/credentials$' = "deny-agent" # AWS credentials — boto3 needs access
|
|
92
94
|
'/\.aws/sso/cache/' = "deny-agent" # AWS SSO tokens — boto3 needs access
|
|
95
|
+
'/\.aws/login/cache/' = "deny-agent" # AWS SSO/OIDC login token cache — boto3 needs access
|
|
96
|
+
'/\.aws/cli/cache/' = "deny-agent" # AWS assume-role credential cache — boto3 needs access
|
|
93
97
|
'/\.docker/config\.json$' = "deny-agent" # Docker registry auth — docker SDK may need access
|
|
94
98
|
'/\.kube/config$' = "deny-agent" # Kubernetes credentials — k8s client may need access
|
|
95
99
|
'/\.gnupg/' = "deny-all" # GPG private keys — no library reader in sandbox
|
|
@@ -15,12 +15,19 @@ from pathlib import Path
|
|
|
15
15
|
from typing import Any
|
|
16
16
|
|
|
17
17
|
from pyddock.config import PyddockConfig, ShellPolicyConfig, find_deny_hint
|
|
18
|
-
from pyddock._base import canonical_path,
|
|
18
|
+
from pyddock._base import canonical_path, ntfs_stream_base
|
|
19
19
|
from pyddock._process_utils import get_startupinfo, kill_and_drain, make_child_env, truncate_output
|
|
20
20
|
|
|
21
21
|
import shutil
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
# A scheme://… URI prefix (http://, https://, ssh://, ftp://, …). Used to keep
|
|
25
|
+
# URLs from being treated as local filesystem path candidates in shell args.
|
|
26
|
+
# Matches RFC-3986 scheme syntax: ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
|
|
27
|
+
# followed by "://".
|
|
28
|
+
_URL_SCHEME_RE = re.compile(r"^[A-Za-z][A-Za-z0-9+.\-]*://")
|
|
29
|
+
|
|
30
|
+
|
|
24
31
|
def _abspath(p: Path) -> Path:
|
|
25
32
|
"""Canonicalize a path for shell-arg containment checks.
|
|
26
33
|
|
|
@@ -39,8 +46,19 @@ def _looks_like_path(arg: str) -> bool:
|
|
|
39
46
|
Returns True if the arg contains path separators, starts with '.',
|
|
40
47
|
or starts with a drive letter (Windows). Excludes UNC-style paths
|
|
41
48
|
(//server/...) and Perforce depot paths (//depot/...) which are not
|
|
42
|
-
local filesystem targets
|
|
49
|
+
local filesystem targets, and ``scheme://...`` URLs (http, https, ssh,
|
|
50
|
+
ftp, ...), which are likewise not local paths.
|
|
51
|
+
|
|
52
|
+
The URL exclusion is also what keeps a URL argument from reaching
|
|
53
|
+
`has_ntfs_stream`: a URL's ``scheme:`` prefix (and any ``host:port``)
|
|
54
|
+
carries colons that the strict stream check would otherwise reject as an
|
|
55
|
+
NTFS alternate-data-stream reference (false positive). Filtering URLs out
|
|
56
|
+
here — rather than relaxing `has_ntfs_stream` itself — keeps that primitive
|
|
57
|
+
strict for the real filesystem operations that depend on it.
|
|
43
58
|
"""
|
|
59
|
+
# Exclude scheme://... URLs (http, https, ssh, ftp, ...) — not local paths.
|
|
60
|
+
if _URL_SCHEME_RE.match(arg):
|
|
61
|
+
return False
|
|
44
62
|
# Exclude //... paths (UNC paths, Perforce depot paths)
|
|
45
63
|
if arg.startswith("//"):
|
|
46
64
|
return False
|
|
@@ -483,11 +501,18 @@ def evaluate_arg_paths(
|
|
|
483
501
|
# NTFS alternate data streams alias a different object than their
|
|
484
502
|
# lexical path implies (e.g. "--output=.pyddock:x" writes onto the
|
|
485
503
|
# protected .pyddock dir while evading the relative_to() check).
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
504
|
+
# Gated on existence of the base object: protected targets always
|
|
505
|
+
# exist, so the attack is still caught, while a colon in a value
|
|
506
|
+
# whose base does not exist is not treated as a stream.
|
|
507
|
+
_stream_base = ntfs_stream_base(candidate)
|
|
508
|
+
if _stream_base is not None:
|
|
509
|
+
_base_p = Path(_stream_base)
|
|
510
|
+
_base_abs = _base_p if _base_p.is_absolute() else workspace_root / _base_p
|
|
511
|
+
if _base_abs.exists():
|
|
512
|
+
return (
|
|
513
|
+
f"Argument '{arg}' uses an NTFS alternate data stream "
|
|
514
|
+
f"(':' in a path component), which is not permitted."
|
|
515
|
+
)
|
|
491
516
|
# Resolve relative to workspace root (same as command cwd)
|
|
492
517
|
resolved = _abspath(workspace_root / candidate)
|
|
493
518
|
|
|
@@ -566,11 +591,15 @@ def evaluate_cwd(
|
|
|
566
591
|
return None
|
|
567
592
|
|
|
568
593
|
cwd_str = cwd.decode("utf-8", "surrogateescape") if isinstance(cwd, bytes) else str(cwd)
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
)
|
|
594
|
+
_cwd_stream_base = ntfs_stream_base(cwd_str)
|
|
595
|
+
if _cwd_stream_base is not None:
|
|
596
|
+
_cwd_base_p = Path(_cwd_stream_base)
|
|
597
|
+
_cwd_base_abs = _cwd_base_p if _cwd_base_p.is_absolute() else workspace_root / _cwd_base_p
|
|
598
|
+
if _cwd_base_abs.exists():
|
|
599
|
+
return (
|
|
600
|
+
f"cwd '{cwd_str}' uses an NTFS alternate data stream (':' in a path "
|
|
601
|
+
f"component), which is not permitted."
|
|
602
|
+
)
|
|
574
603
|
ws_root_abs = _abspath(workspace_root)
|
|
575
604
|
pyddock_dir = _abspath(workspace_root / ".pyddock")
|
|
576
605
|
resolved = _abspath(workspace_root / Path(cwd_str))
|
|
@@ -8,9 +8,13 @@
|
|
|
8
8
|
2. NTFS alternate data streams — `open(".pyddock:pwned", "w")` writes a data
|
|
9
9
|
stream onto the protected `.pyddock` directory while the lexical leaf
|
|
10
10
|
`.pyddock:pwned` evades the `relative_to('.pyddock')` containment check.
|
|
11
|
-
Fixed by
|
|
12
|
-
(
|
|
13
|
-
and the shell arg-path /
|
|
11
|
+
Fixed by detecting a `:` stream reference and rejecting it when the stream's
|
|
12
|
+
BASE object exists on disk (ntfs_stream_base + an existence check), wired
|
|
13
|
+
into the shared _check_read/_check_write closures and the shell arg-path /
|
|
14
|
+
cwd scanners. The existence gate is sound because every protected target
|
|
15
|
+
(.pyddock, workspace module dirs, the stdlib) always exists, so the attack is
|
|
16
|
+
still blocked — while a colon in a string that merely looks path-like (e.g. a
|
|
17
|
+
commit message) has a non-existent base and is no longer a false positive.
|
|
14
18
|
"""
|
|
15
19
|
|
|
16
20
|
from __future__ import annotations
|
|
@@ -138,12 +142,93 @@ class TestHasNtfsStream:
|
|
|
138
142
|
]:
|
|
139
143
|
assert has_ntfs_stream(bad) is True, bad
|
|
140
144
|
|
|
145
|
+
@pytest.mark.skipif(sys.platform != "win32", reason="NTFS ADS is Windows-only")
|
|
146
|
+
def test_stream_base_extraction(self) -> None:
|
|
147
|
+
# ntfs_stream_base returns the object the stream attaches to (or None).
|
|
148
|
+
from pyddock._base import ntfs_stream_base
|
|
149
|
+
|
|
150
|
+
assert ntfs_stream_base(".pyddock:pwned") == ".pyddock"
|
|
151
|
+
assert ntfs_stream_base(".pyddock:pwned.txt") == ".pyddock"
|
|
152
|
+
assert ntfs_stream_base("C:/Git/pyddock/.pyddock:pwned") == "C:\\Git\\pyddock\\.pyddock"
|
|
153
|
+
assert ntfs_stream_base("dir/file.txt:s") == "dir\\file.txt"
|
|
154
|
+
assert ntfs_stream_base("file.txt") is None
|
|
155
|
+
assert ntfs_stream_base("C:\\abs\\file") is None
|
|
156
|
+
|
|
141
157
|
@pytest.mark.skipif(sys.platform != "win32", reason="NTFS ADS is Windows-only")
|
|
142
158
|
def test_drive_relative_not_flagged(self) -> None:
|
|
143
159
|
# 'C:rest' (drive-relative) and 'C:\\...' are the only legal colons.
|
|
144
160
|
assert has_ntfs_stream("C:relative\\file") is False
|
|
145
161
|
assert has_ntfs_stream("C:\\abs\\file") is False
|
|
146
162
|
|
|
163
|
+
@pytest.mark.skipif(sys.platform != "win32", reason="NTFS ADS is Windows-only")
|
|
164
|
+
def test_primitive_stays_strict_for_url_disguised_stream(self) -> None:
|
|
165
|
+
# has_ntfs_stream is a low-level filesystem primitive: it must NOT make
|
|
166
|
+
# an exception for URL-looking strings. A scheme:// prefix slapped onto a
|
|
167
|
+
# stream-bearing path (e.g. open("https://.pyddock:pwned", "w")) resolves
|
|
168
|
+
# to an in-workspace lexical path and would otherwise slip past the
|
|
169
|
+
# writable_paths containment check. The primitive must still reject it;
|
|
170
|
+
# URL filtering belongs to the shell-arg layer (_looks_like_path), not
|
|
171
|
+
# here. See the matching regression in TestUrlArgsNotPathCandidates.
|
|
172
|
+
assert has_ntfs_stream("https://.pyddock:pwned") is True
|
|
173
|
+
assert has_ntfs_stream("http://host/file.txt:stream") is True
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
class TestUrlArgsNotPathCandidates:
|
|
177
|
+
"""URLs passed as shell CLI arguments must not be treated as local paths.
|
|
178
|
+
|
|
179
|
+
Regression: a URL argument (e.g. `-Url https://host/a/b`) tripped the
|
|
180
|
+
NTFS-stream check because the scheme's `:` (and any `host:port`) looked like
|
|
181
|
+
an alternate-data-stream reference. URLs are filtered out at the
|
|
182
|
+
`_looks_like_path` layer so they never reach the stream check, while the
|
|
183
|
+
primitive itself stays strict for real filesystem paths.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
@pytest.mark.parametrize(
|
|
187
|
+
"url",
|
|
188
|
+
[
|
|
189
|
+
"https://api.example.com/projects/abc/designs/xyz",
|
|
190
|
+
"http://example.com/path",
|
|
191
|
+
"https://host:8443/path", # host:port colon
|
|
192
|
+
"ssh://git@github.com/org/repo.git",
|
|
193
|
+
"ftp://files.example.com/data.txt",
|
|
194
|
+
],
|
|
195
|
+
)
|
|
196
|
+
def test_urls_are_not_path_candidates(self, url: str) -> None:
|
|
197
|
+
from pyddock.shell_executor import _extract_path_candidates, _looks_like_path
|
|
198
|
+
|
|
199
|
+
assert _looks_like_path(url) is False, url
|
|
200
|
+
assert _extract_path_candidates(url) == [], url
|
|
201
|
+
|
|
202
|
+
def test_url_arg_passes_arg_path_scan(self, tmp_path: Path) -> None:
|
|
203
|
+
# A URL argument must not be rejected by the arg-path scanner in either
|
|
204
|
+
# "protected" or "workspace" mode.
|
|
205
|
+
for mode in ("protected", "workspace"):
|
|
206
|
+
reason = evaluate_arg_paths(
|
|
207
|
+
["-Url", "https://api.example.com/projects/abc/designs/xyz", "-Raw"],
|
|
208
|
+
arg_paths=mode,
|
|
209
|
+
workspace_root=tmp_path,
|
|
210
|
+
workspace_module_dirs={},
|
|
211
|
+
shell_command_patterns=[],
|
|
212
|
+
)
|
|
213
|
+
assert reason is None, (mode, reason)
|
|
214
|
+
|
|
215
|
+
def test_stream_in_flag_value_still_blocked(self, tmp_path: Path) -> None:
|
|
216
|
+
# The URL exclusion must NOT weaken the output-flag stream attack: a
|
|
217
|
+
# value that is a stream-bearing path (not a scheme://) is still scanned.
|
|
218
|
+
(tmp_path / ".pyddock").mkdir() # base object the stream targets
|
|
219
|
+
reason = evaluate_arg_paths(
|
|
220
|
+
["-o=.pyddock:pwned"],
|
|
221
|
+
arg_paths="protected",
|
|
222
|
+
workspace_root=tmp_path,
|
|
223
|
+
workspace_module_dirs={},
|
|
224
|
+
shell_command_patterns=[],
|
|
225
|
+
)
|
|
226
|
+
if sys.platform == "win32":
|
|
227
|
+
assert reason is not None and "alternate data stream" in reason
|
|
228
|
+
else:
|
|
229
|
+
# NTFS ADS is Windows-only; on POSIX ':' is a legal filename char.
|
|
230
|
+
assert reason is None or "alternate data stream" not in reason
|
|
231
|
+
|
|
147
232
|
|
|
148
233
|
class TestAdsWriteBlockedEndToEnd:
|
|
149
234
|
@pytest.mark.skipif(sys.platform != "win32", reason="NTFS ADS is Windows-only")
|
|
@@ -152,6 +237,9 @@ class TestAdsWriteBlockedEndToEnd:
|
|
|
152
237
|
) -> None:
|
|
153
238
|
"""open('.pyddock:pwned', 'w') must be rejected — it would otherwise
|
|
154
239
|
write a data stream onto the protected .pyddock directory."""
|
|
240
|
+
# The server always creates .pyddock at startup; mirror that here so the
|
|
241
|
+
# stream's base object exists (the existence gate keys off this).
|
|
242
|
+
(workspace / ".pyddock").mkdir(exist_ok=True)
|
|
155
243
|
config = PyddockConfig(
|
|
156
244
|
execution=ExecutionConfig(timeout=30.0),
|
|
157
245
|
imports=ImportsConfig(allowed=["pathlib", "io"]),
|
|
@@ -403,6 +491,7 @@ class TestAdsArgPathScanBlocked:
|
|
|
403
491
|
def test_shell_output_arg_with_stream_rejected(self, tmp_path: Path) -> None:
|
|
404
492
|
"""A shell arg like --output=.pyddock:x must be rejected by the arg
|
|
405
493
|
scanner (same containment gap as the fs guard)."""
|
|
494
|
+
(tmp_path / ".pyddock").mkdir() # base object the stream targets
|
|
406
495
|
reason = evaluate_arg_paths(
|
|
407
496
|
["--output=.pyddock:pwned"],
|
|
408
497
|
arg_paths="workspace",
|
|
@@ -438,3 +527,17 @@ class TestAdsArgPathScanBlocked:
|
|
|
438
527
|
shell_command_patterns=["^git$"],
|
|
439
528
|
)
|
|
440
529
|
assert reason is None
|
|
530
|
+
|
|
531
|
+
def test_stream_value_with_nonexistent_base_allowed(self, tmp_path: Path) -> None:
|
|
532
|
+
"""Existence gate (false-positive fix): a colon-bearing, path-like value
|
|
533
|
+
whose base object does NOT exist is not treated as a stream. This is what
|
|
534
|
+
keeps colons in non-path strings (e.g. a 'feat: ...' commit message that
|
|
535
|
+
happens to contain a '/') from being rejected as ADS references."""
|
|
536
|
+
reason = evaluate_arg_paths(
|
|
537
|
+
["-o=notes:draft/today.txt"],
|
|
538
|
+
arg_paths="workspace",
|
|
539
|
+
workspace_root=tmp_path,
|
|
540
|
+
workspace_module_dirs={},
|
|
541
|
+
shell_command_patterns=[],
|
|
542
|
+
)
|
|
543
|
+
assert reason is None, reason
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|