-
Notifications
You must be signed in to change notification settings - Fork 48
[FIX]: contain deserialized payload artifacts #106
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
Hinotoi-agent
wants to merge
6
commits into
microsoft:main
Choose a base branch
from
Hinotoi-agent:fix/artifact-load-containment
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.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8d57639
fix: contain loaded payload artifacts
Hinotoi-agent 261a966
refactor: use relative_to for artifact prefix check
Hinotoi-agent 2e19dad
test: cover artifact path validation branches
Hinotoi-agent b6cef53
fix: reject symlinked artifact directories
Hinotoi-agent 107e8ce
fix: normalize invalid artifact reference types
Hinotoi-agent c48d3ce
fix: clarify artifact reference validation
Hinotoi-agent 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
Some comments aren't visible on the classic Files Changed page.
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,121 @@ | ||
| # Copyright (c) Microsoft Corporation. | ||
| # Licensed under the MIT license. | ||
|
|
||
| """Security tests for payload artifact path handling.""" | ||
|
|
||
| import json | ||
| from pathlib import Path | ||
|
|
||
| import pytest | ||
|
|
||
| from rampart.payloads._store import PayloadStore | ||
|
|
||
|
|
||
| def _write_collection_record(collection_dir: Path, artifact: object) -> None: | ||
| collection_dir.mkdir(parents=True, exist_ok=True) | ||
| record: dict[str, object] = { | ||
| "id": "safe-id", | ||
| "content": "binary", | ||
| "format": "pdf", | ||
| "metadata": {}, | ||
| "artifact": artifact, | ||
| } | ||
| (collection_dir / "payloads.jsonl").write_text(json.dumps(record) + "\n") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "artifact", | ||
| [ | ||
| "../outside.pdf", | ||
| "artifacts/../outside.pdf", | ||
| "/tmp/outside.pdf", | ||
| "outside.pdf", | ||
| "artifacts", | ||
| ], | ||
| ) | ||
|
Hinotoi-agent marked this conversation as resolved.
|
||
| def test_payload_store_rejects_deserialized_artifact_escape( | ||
| tmp_path: Path, | ||
| artifact: str, | ||
| ) -> None: | ||
| """Serialized artifact paths must stay under the collection artifacts dir.""" | ||
| collection_dir = tmp_path / "store" / "collection" | ||
| _write_collection_record(collection_dir, artifact) | ||
|
|
||
| store = PayloadStore(root=tmp_path / "store") | ||
| with pytest.raises(ValueError, match="Invalid artifact path"): | ||
| store.load("collection") | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "artifact", | ||
| [None, 7, ["artifacts/file.pdf"], {"path": "artifacts/file.pdf"}], | ||
| ) | ||
| def test_payload_store_rejects_non_string_deserialized_artifact( | ||
| tmp_path: Path, | ||
| artifact: object, | ||
| ) -> None: | ||
| """Serialized artifact references must be strings.""" | ||
| collection_dir = tmp_path / "store" / "collection" | ||
| _write_collection_record(collection_dir, artifact) | ||
|
|
||
| store = PayloadStore(root=tmp_path / "store") | ||
| with pytest.raises(ValueError, match="Invalid artifact path"): | ||
| store.load("collection") | ||
|
|
||
|
|
||
| def test_payload_store_rejects_deserialized_artifact_symlink_escape( | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """Serialized artifact paths cannot resolve through symlinks outside artifacts.""" | ||
| collection_dir = tmp_path / "store" / "collection" | ||
| artifacts_dir = collection_dir / "artifacts" | ||
| artifacts_dir.mkdir(parents=True) | ||
| outside = tmp_path / "outside.pdf" | ||
| outside.write_bytes(b"outside") | ||
| symlink = artifacts_dir / "linked.pdf" | ||
| try: | ||
| symlink.symlink_to(outside) | ||
| except OSError as exc: | ||
| pytest.skip(f"symlinks are not available on this platform: {exc}") | ||
|
|
||
| _write_collection_record(collection_dir, "artifacts/linked.pdf") | ||
|
|
||
| store = PayloadStore(root=tmp_path / "store") | ||
| with pytest.raises(ValueError, match="escapes"): | ||
| store.load("collection") | ||
|
|
||
|
|
||
| def test_payload_store_rejects_deserialized_artifacts_directory_symlink_escape( | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """The collection artifacts directory cannot resolve outside the collection.""" | ||
| collection_dir = tmp_path / "store" / "collection" | ||
| collection_dir.mkdir(parents=True) | ||
| outside_dir = tmp_path / "outside" | ||
| outside_dir.mkdir() | ||
| (outside_dir / "linked.pdf").write_bytes(b"outside") | ||
| try: | ||
| (collection_dir / "artifacts").symlink_to( | ||
| outside_dir, | ||
| target_is_directory=True, | ||
| ) | ||
| except OSError as exc: | ||
| pytest.skip(f"symlinks are not available on this platform: {exc}") | ||
|
|
||
| _write_collection_record(collection_dir, "artifacts/linked.pdf") | ||
|
|
||
| store = PayloadStore(root=tmp_path / "store") | ||
| with pytest.raises(ValueError, match=r"artifacts directory.*escapes"): | ||
| store.load("collection") | ||
|
|
||
|
|
||
| def test_payload_store_rejects_missing_deserialized_artifact( | ||
| tmp_path: Path, | ||
| ) -> None: | ||
| """A valid serialized artifact path must refer to an existing file.""" | ||
| collection_dir = tmp_path / "store" / "collection" | ||
| _write_collection_record(collection_dir, "artifacts/gone.pdf") | ||
|
|
||
| store = PayloadStore(root=tmp_path / "store") | ||
| with pytest.raises(FileNotFoundError, match="Missing artifact"): | ||
| store.load("collection") | ||
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.
Uh oh!
There was an error while loading. Please reload this page.