Repository navigation
Harden Forge validation boundaries and fail-closed requirement evidence #33
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c987f33
Enforce artifact file confinement and validation integrity
Daniele-Cangi 7195ee6
Scope quality requirements to unquoted positive mentions
Daniele-Cangi 1b84901
Track assertion provenance through binding changes
Daniele-Cangi 5685ab7
Fail closed on unrecognized numeric expiration policies
Daniele-Cangi 74b6995
Preserve compound expiration policies and mixed prose polarity
Daniele-Cangi 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| """File confinement and content checks at artifact stage boundaries.""" | ||
|
|
||
| import hashlib | ||
| from pathlib import Path, PureWindowsPath | ||
| from typing import Iterable | ||
|
|
||
| from core.forge.contracts import GeneratedFile | ||
|
|
||
|
|
||
| class ArtifactPathError(ValueError): | ||
| """An artifact does not declare distinct, confined relative file paths.""" | ||
|
|
||
|
|
||
| def artifact_file_targets( | ||
| files: Iterable[GeneratedFile], | ||
| root: Path, | ||
| reserved_names: Iterable[str] = (), | ||
| ) -> dict[str, Path]: | ||
| """Check the entire file set before any file is written.""" | ||
| root = root.resolve() | ||
| reserved = {name.casefold() for name in reserved_names} | ||
| targets: dict[str, Path] = {} | ||
| names: set[str] = set() | ||
| device_names = {"con", "prn", "aux", "nul", "conin$", "conout$"} | { | ||
| prefix + number for prefix in ("com", "lpt") for number in "123456789¹²³" | ||
| } | ||
| for generated in files: | ||
| path = generated.path | ||
| if ( | ||
| not isinstance(path, str) | ||
| or not path | ||
| or "\\" in path | ||
| or ":" in path | ||
| or "\x00" in path | ||
| or PureWindowsPath(path).drive | ||
| or any(part in {"", ".", ".."} for part in path.split("/")) | ||
| or any(ord(char) < 32 or char in '<>"|?*' for char in path) | ||
| or any( | ||
| part.endswith((".", " ")) | ||
| or part.partition(".")[0].rstrip(" ").casefold() in device_names | ||
| for part in path.split("/") | ||
| ) | ||
| ): | ||
| raise ArtifactPathError(f"Invalid artifact relative path: {path!r}") | ||
| name = path.casefold() | ||
| if name in names or name.split("/")[0] in reserved: | ||
| raise ArtifactPathError(f"Duplicate or reserved artifact path: {path!r}") | ||
| target = root / path | ||
| if target.resolve() != target or not target.is_relative_to(root): | ||
| raise ArtifactPathError(f"Artifact path is not confined: {path!r}") | ||
| if target.is_symlink(): | ||
| raise ArtifactPathError(f"Artifact file is a symbolic link: {path!r}") | ||
| names.add(name) | ||
| targets[path] = target | ||
| for name in names: | ||
| parts = name.split("/") | ||
| if any("/".join(parts[:index]) in names for index in range(1, len(parts))): | ||
| raise ArtifactPathError(f"Artifact file/directory conflict: {name!r}") | ||
| return targets | ||
|
|
||
|
|
||
| def artifact_file_hashes(materialized: dict[str, Path]) -> dict[str, str]: | ||
| """Snapshot actual written bytes, including platform newline conversion.""" | ||
| return { | ||
| path: hashlib.sha256(target.read_bytes()).hexdigest() | ||
| for path, target in materialized.items() | ||
| } | ||
|
|
||
|
|
||
| def artifact_file_changes( | ||
| materialized: dict[str, Path], expected: dict[str, str], root: Path, | ||
| ) -> list[dict[str, str]]: | ||
| changes: list[dict[str, str]] = [] | ||
| root = root.resolve() | ||
| for path, digest in expected.items(): | ||
| target = materialized[path] | ||
| try: | ||
| if target.is_symlink() or target.resolve() != target or not target.is_relative_to(root): | ||
| reason = "path_changed" | ||
| elif hashlib.sha256(target.read_bytes()).hexdigest() != digest: | ||
| reason = "content_changed" | ||
| else: | ||
| continue | ||
| except (OSError, ValueError): | ||
| reason = "file_unavailable" | ||
| changes.append({"path": path, "reason": reason}) | ||
| return changes |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.