Skip to main content

syft-restrict

A static analyzer for Python source files that default-denies dynamic constructs in a private region, typically the model logic that must be kept private, while allowing the public region to be reviewed by the data owner in an obfuscated copy.

Overview

syft-restrict splits a Python file into two regions:

  • Private — the lines the user marks as private, typically the model logic that must not be readable. Mark them with # syft-restrict: obfuscate-start / # syft-restrict: obfuscate-end or # syft-restrict: hide-start / # syft-restrict: hide-end comments in the source.
  • Public — every other line outside the marked ranges: imports, data loading, wrappers. It is never checked or obfuscated. The recipient of the obfuscated file can read it directly.

Given that split, syft-restrict executes the following three steps:

  • The auditor checks the allow-list of library calls and operators against a catalog of known safe/dual-use/unsafe paths, and warns on any uncatalogued paths.
  • The verifier statically walks the private region and analyzes it against a default-deny policy. Every construct must be explicitly allowed, or verification fails and reports each offending line.
  • The obfuscator runs after a clean verify. It creates an obfuscated copy by rewriting the private region — renaming identifiers, blanking constants, replacing lines with ■■■■■■■■ — so the logic can't be read back out, while the public region is copied through untouched.

The verifier is inspired by RestrictedPython, but differs in a few ways:

  • It analyzes — it doesn't run. It never executes the source. Only the static checks decide pass/fail.
  • The public code may import allowlisted libraries and call into them. Imports are not allowed in private code, and any library call must be explicitly allow-listed.
  • Dynamic Python lives in the public part. Anything dynamic the author needs (file/tokenizer loading, the generation loop, wrappers around library methods) must be written in the public region — where it can be reviewed directly — and may be called by the private part.

Usage

Mark the private region in the source with comments:

# syft-restrict: obfuscate-start
def attention(x):
    ...
# syft-restrict: obfuscate-end

MODEL_ID = "gemma-2b"  # syft-restrict: hide

See docs/verify.md for the full marker syntax (single-line markers, nesting rules, and error cases).

Then run:

import syft_restrict as restrict

result = restrict.run(
    "gemma_inference.py",
    # default-deny: allow ONLY the exact paths this model calls, nothing else in jax/flax
    allow_functions=[
        "jax.numpy.einsum", "jax.numpy.mean", "jax.numpy.square", "jax.numpy.arange",
        "jax.numpy.sin", "jax.numpy.cos", "jax.numpy.concatenate", "jax.numpy.tril",
        "jax.numpy.triu", "jax.numpy.ones", "jax.numpy.where", "jax.numpy.repeat",
        "jax.numpy.sqrt", "jax.numpy.transpose", "jax.numpy.array", "jax.numpy.float32",
        "jax.numpy.bool_", "jax.lax.rsqrt", "jax.nn.softmax", "jax.nn.gelu",
        "flax.linen.Module",
        "jax.lax", "jax.nn",  # module refs the deep-path calls (jax.lax.rsqrt, jax.nn.softmax) resolve through
    ],
    allow_operators=["arithmetic", "indexing", "comparison"],  # operators allowed ON A VALUE
)
# On success: writes gemma_inference.obfuscated.py and returns result.certificate.
# On a policy violation: raises PolicyViolation naming each offending line.
# If the file has no `# syft-restrict: ...` markers: raises MarkerError.

The auditor warns on any allow_functions that are categorized as dual-use or unsafe, whether they are used or not, but the verifier fails only on actual violations in the private region. The auditor is advisory only, and does not block verification. See docs/audit.md for instructions on how to build the catalog for the specific libraries in use.

The private region is designated only by # syft-restrict: ... comment markers in the source; run() resolves them and refuses a file that carries none. This is how examples/gemma_inference.py generates its obfuscated copy — see examples/generate.py.

Two optional strictness flags, both defaulting to the permissive behavior:

result = restrict.run(
    "gemma_inference.py",
    allow_functions=[...],  # as above
    allow_operators=["arithmetic", "indexing", "comparison"],
    allow_local_assignments=True,   # False: a local aliased to a callable can't be called by name
    allow_base_class_attributes=True,  # False: a never-assigned self.<attr> is not presumed inherited
)

If syft-restrict was successfully executed on the true original file, the obfuscated file can be safely shared without exposing the private section.

Pass strict=False to run to get a RunResult with .ok / .violations instead of an exception.

Documentation

Metadata

Release files for syft-restrict 0.1.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for syft-restrict 0.1.1
File Size Uploaded Provenance Actions
syft_restrict-0.1.1.tar.gz 79.9 kB Without provenance Details for syft_restrict-0.1.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for syft-restrict 0.1.1
File Interpreter ABI Platform Actions
syft_restrict-0.1.1-py3-none-any.whl Python 3 none any Details for syft_restrict-0.1.1-py3-none-any.whl

Total release size: 119.7 kB

Release files / syft_restrict-0.1.1.tar.gz

Download URL syft_restrict-0.1.1.tar.gz
Size 79.9 kB
Tags Source
SHA-256 checksum
How to use checksums
d2bf99ec0a5d2c818975bf9fddcd8d1887f6cf1eedf230488884446259f30b92
BLAKE2b-256 checksum
How to use checksums
9bd2b04a6d74f41281b00e597057fc502ea6f66e298c31a5ebc61d5da3a4a07b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / syft_restrict-0.1.1-py3-none-any.whl

Download URL syft_restrict-0.1.1-py3-none-any.whl
Size 39.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d954d80dbce170f3792388bd93964aa52269287a26f070c0b035b39591396408
BLAKE2b-256 checksum
How to use checksums
b6a8593250d67270865f2739bb16d8b714a9eb289d88be484b8a03374180d506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for syft-restrict 0.1.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page