fix: harden syft-restrict against verifier and obfuscator bypasses#9436
Open
pjwerneck wants to merge 12 commits into
Open
fix: harden syft-restrict against verifier and obfuscator bypasses#9436pjwerneck wants to merge 12 commits into
pjwerneck wants to merge 12 commits into
Conversation
…or f-string conversions
… scope checks Fix ast.comprehension check relying on unavailable lineno, therefore unreachable code.
…handling and range errors
0213862 to
fc54f9d
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR strengthens syft-restrict’s static verification and obfuscation layers to close multiple identified bypass techniques, adds regression tests for each bypass class, and refreshes the documentation to match the evolved threat model and implementation.
Changes:
- Harden verifier rules around call-target resolution, aliasing,
self/clstrust, and f-string interpolation to prevent reflection/dynamic-escape bypasses. - Update obfuscation to correctly blank f-string literal text under Python 3.12+ (PEP 701 tokenization), plus refactor shared AST/range utilities into
astutil.py. - Add targeted regression tests (bypasses + range validation) and restructure docs/README for clarity.
Reviewed changes
Copilot reviewed 15 out of 15 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| packages/syft-restrict/tests/verify/test_whitelist.py | Updates whitelist expectations for f-strings to reflect new interpolation rejection behavior. |
| packages/syft-restrict/tests/verify/test_ranges.py | Adds regression test ensuring malformed private ranges raise instead of silently passing. |
| packages/syft-restrict/tests/verify/test_bypasses.py | Adds comprehensive regression coverage for verifier bypass classes (aliasing, reflection, f-strings, self/cls trust, etc.). |
| packages/syft-restrict/tests/obfuscate/test_obfuscate.py | Adds a test ensuring f-string literal text is blanked across tokenizer variants (incl. Python 3.12+). |
| packages/syft-restrict/src/syft_restrict/verifier.py | Major verifier hardening + refactor: central violation codes, stronger call/name checks, self-attr trust tracking, and f-string interpolation rejection. |
| packages/syft-restrict/src/syft_restrict/runner.py | Switches to shared astutil helpers for range normalization and scanning. |
| packages/syft-restrict/src/syft_restrict/policy.py | Expands banned builtins and improves documentation/comments; minor cleanup. |
| packages/syft-restrict/src/syft_restrict/obfuscator.py | Refactors passes; adds PEP 701 f-string token handling; uses shared astutil. |
| packages/syft-restrict/src/syft_restrict/astutil.py | New shared helper module for AST and range utilities (scan, dotted paths, range normalization). |
| packages/syft-restrict/src/syft_restrict/init.py | Updates package-level docs/pointers to new docs structure. |
| packages/syft-restrict/README.md | Simplifies and reorients README toward usage + docs pointers. |
| packages/syft-restrict/docs/verify.md | New/expanded explanation of verifier behavior, edge cases, and limitations. |
| packages/syft-restrict/docs/code-layout.md | New short guide to module responsibilities. |
| packages/syft-restrict/docs/blacklist.md | New consolidated “what is rejected” doc with violation codes. |
| packages/syft-restrict/disallowed-ast-examples.md | Removes legacy table-style doc (replaced by new docs structure). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Closes a series of bypasses in
syft-restrict's static verifier and obfuscator, found throughiterative adversarial review. Update documentation.
Changes
type,__build_class__,print, and thedunder-proxy builtins
repr/str/ascii/format/bytes.from X import name, homoglyph identifiers, andcommon identifier-aliasing patterns that evaded the call-target checks.
!r/!s/!a), the{x=}debug form, and plaininterpolation (
f"{x}") all invoke__format__/__repr__/__str__with noCallnode — allare now rejected instead of only the conversion-flag forms.
self/clsparameter vetting: reassigningself/cls, reusing them as an unrelatedparameter name, and non-first-parameter/nested-function/lambda cases no longer grant the
self.<name>trust exemption.self.<attr>trust bypasses: tuple-unpack and for-loop assignment targets are now trackedby the safety table, and local-variable aliasing (including multi-hop copies) is tracked so
tmp = self.fn; tmp(x)is checked the same wayself.fn(x)is.def/classstatements whose name shadows a trusted import alias or visible wrapper name(previously only rebinding via assignment was checked).
tokenizer (previously left un-obfuscated on 3.12+).
docs/disallowed-ast-examples.mdas per-construct subheadings instead of a wide table;add
docs/code-layout.md; simplifyREADME.md.astutil.pyand refactorverifier.py/obfuscator.pyforreadability (central violation-code registry,
_SelfAttrTrusthelper, split obfuscator passes),with no behavior change.
Testing
tests/verify/test_bypasses.pywith a regression test per bypass classtests/verify/test_ranges.pyfor the range-validation fix.Asana task
https://app.asana.com/1/1185126988600652/project/1216249688888494/task/1216266561077080?focus=true