Skip to main content

crabcrop_url

Build and sign crabcrop URLs from Python. Standard library only; the signing scheme is specified in spec/url-signing.md and this package runs the canonical spec/test-vectors.json as its test suite, so it cannot drift from the server.

Usage

from crabcrop_url import build_sealed_url, build_url, build_srcset, purge_group_signature, purge_signature, seal_url, sign, unseal, upload_signature, verify

# A signed URL. The operation string is passed verbatim and may
# contain several segments and preset: references.
url = build_url(
    "https://images.example.com",
    "your-secret",
    "crop:1200x630:smart/filters:format(webp)",
    "https://example.com/photo.jpg",
)

# An expiring URL (unix seconds), emitted as a leading exp: segment.
expiring = build_url(
    "https://images.example.com",
    "your-secret",
    "300x200",
    "https://example.com/photo.jpg",
    expires=1893456000,
)

# A responsive srcset: one {width}x0 URL per width.
srcset = build_srcset(
    "https://images.example.com",
    "your-secret",
    "https://example.com/photo.jpg",
    [400, 800, 1200],
)

# The signature for a remainder, if you assemble URLs yourself. It
# raises ValueError for a remainder that begins with the reserved
# segment "crabcrop" (is_reserved_remainder), as every builder does:
# the derived subkeys are signatures of labels under that segment.
signature = sign("your-secret", "300x200/https://example.com/photo.jpg")

# Verify a signature in constant time (malformed tags return False).
ok = verify("your-secret", signature, "300x200/https://example.com/photo.jpg")

# Purge signatures for the server's /purge and /purge-group endpoints
# are domain-separated under a derived subkey; the helpers sign the
# right strings so callers never assemble them by hand.
purge = purge_signature("your-secret", "300x200/https://example.com/photo.jpg")
group = purge_group_signature("your-secret", "thumb")

# Upload signatures for the server's PUT /upload/<signature>/<name>
# endpoint live in a third domain, under their own derived subkey.
# Pass an expiry (unix seconds) for a token that stops working
# then; its path is /upload/<signature>/exp:<expiry>/<name>.
upload = upload_signature("your-secret", "heroes/photo.jpg")
expiring = upload_signature("your-secret", "heroes/photo.jpg", 1893456000)

# Sealed URLs (issue #255): the whole remainder encrypted into an
# opaque token in the signature slot, for public links whose recipe
# should stay unreadable. The server serves the sealed and plain
# forms of one render from the same cache entry. Sealing alone
# needs the cryptography package: pip install crabcrop-url[seal]
sealed_url = build_sealed_url(
    "https://images.example.com",
    "your-secret",
    "300x200",
    "https://example.com/photo.jpg",
    name="photo.jpg",
)
remainder = unseal("your-secret", seal_url("your-secret", "300x200/https://example.com/photo.jpg"))

Pass None as the secret to produce the literal unsafe signature segment, for servers running with CRABCROP_ALLOW_UNSAFE or in presets-only mode. The secret may be str or bytes.

The builders raise ValueError on operations or a source that can never verify: ?, #, whitespace, control and non-ASCII characters, and " < > \ ^ ` { | }, which a browser rewrites before an <img src> or fetch() request leaves the page (\ becomes /, the rest are percent-encoded). Percent-encode those in the source first (x{y}.jpg as x%7By%7D.jpg); the error names the encoding. The full rules are in spec/url-signing.md.

Tests

python3 -m unittest discover -s tests

Metadata

Release files for crabcrop-url 0.11.0

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

Source distribution (sdist)

Source distribution for crabcrop-url 0.11.0
File Size Uploaded Provenance Actions
crabcrop_url-0.11.0.tar.gz 14.0 kB Without provenance Details for crabcrop_url-0.11.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for crabcrop-url 0.11.0
File Interpreter ABI Platform Actions
crabcrop_url-0.11.0-py3-none-any.whl Python 3 none any Details for crabcrop_url-0.11.0-py3-none-any.whl

Total release size: 23.0 kB

Release files / crabcrop_url-0.11.0.tar.gz

Download URL crabcrop_url-0.11.0.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
f7448b38d2267c2114b886d2511d932b2c306aa6bc0f13c4475331c1125258e2
BLAKE2b-256 checksum
How to use checksums
478f22061d1e5f2a89a04121e0418983a1a649f16ac72f0a030b455bddbc0c37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Release files / crabcrop_url-0.11.0-py3-none-any.whl

Download URL crabcrop_url-0.11.0-py3-none-any.whl
Size 9.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
401b92f74311519a21df0a6647c8b53d57140cd42ea629becedcfdf18de52e02
BLAKE2b-256 checksum
How to use checksums
e039166f129f5dad868ce72b1b837cfda9288e82b1560f0648e97401fbc18072
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.16

Security for crabcrop-url 0.11.0

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.

Release history Release notifications | RSS feed

This release

0.11.0 This release

2 release files No provenance

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