itsdangerous
Sign and verify data with HMAC so tampering is detectable, including time-limited signed tokens.
Official Documentation ↗v2.2.0. Check the official docs for the latest API surface.Installation
pip install itsdangerousMethods
No methods match this filter.
itsdangerous signs serialized data so a server can trust a value that passed through an untrusted client. It is the mechanism behind signed cookies, unsubscribe links, and password-reset tokens.
Integrity, not secrecy
The signature detects modification. The payload remains readable, so never place secrets inside a signed token that is handed to a browser.
Expiry and errors
loads(token, max_age=...) enforces a time limit. Catch SignatureExpired and BadSignature separately: expired tokens are a normal user flow, while a bad signature indicates tampering or a rotated key.
Errors
The official API reference declares the following error types:
itsdangerous.exc.BadSignature(exception) — official text: “exception itsdangerous.exc.BadSignature(message, payload=None) — Raised if a signature does not match.” (source: https://itsdangerous.palletsprojects.com/en/stable/exceptions/#itsdangerous.exc.BadSignature, fetched 2026-11-08T16:03:08.4363995+08:00)itsdangerous.exc.BadTimeSignature(exception) — official text: “exception itsdangerous.exc.BadTimeSignature(message, payload=None, date_signed=None) — Raised if a time-based signature is invalid.” (source: https://itsdangerous.palletsprojects.com/en/stable/exceptions/#itsdangerous.exc.BadTimeSignature, fetched 2026-11-08T16:03:08.4363995+08:00)itsdangerous.exc.SignatureExpired(exception) — official text: “exception itsdangerous.exc.SignatureExpired(message, payload=None, date_signed=None) — Raised if a signature timestamp is older than max_age.” (source: https://itsdangerous.palletsprojects.com/en/stable/exceptions/#itsdangerous.exc.SignatureExpired, fetched 2026-11-08T16:03:08.4363995+08:00)itsdangerous.exc.BadData(exception) — official text: “exception itsdangerous.exc.BadData(message) — Raised if bad data of any sort was encountered. This is the base for all exceptions that ItsDangerous defines.” (source: https://itsdangerous.palletsprojects.com/en/stable/exceptions/#itsdangerous.exc.BadData, fetched 2026-11-08T16:03:08.4363995+08:00)
Security
Official security statements captured from the package documentation:
- 签名只能防篡改,数据对接收方可见;密钥必须保密且足够复杂 — “The receiver can see the data, but they can not modify it unless they also have your key. So if you keep the key secret and complex, you will be fine.” (source: https://itsdangerous.palletsprojects.com/en/stable/#, fetched 2026-11-08T16:03:08.4363995+08:00)
API reference evidence
Fetched from official sources on 2026-11-08T16:03:08.4363995+08:00 via WebFetch 官方文档站(url_safe / serializer / exceptions / 首页).
URLSafeTimedSerializer→ signature from https://itsdangerous.palletsprojects.com/en/stable/url_safe/#itsdangerous.url_safe.URLSafeTimedSerializer; official text: “class itsdangerous.url_safe.URLSafeTimedSerializer(secret_key: str | bytes | Iterable[str] | Iterable[bytes], salt: str | bytes | None = b’itsdangerous’”serializer.dumps→ signature from https://itsdangerous.palletsprojects.com/en/stable/serializer/#itsdangerous.serializer.Serializer.dumps; official text: “dumps(obj, salt=None) — Returns a signed string serialized with the internal serializer.”serializer.loads→ signature from https://itsdangerous.palletsprojects.com/en/stable/serializer/#itsdangerous.serializer.Serializer.loads; official text: “loads(s, salt=None, **kwargs) — Reverse of dumps(). Raises BadSignature if the signature validation fails.”
FAQ
- Is itsdangerous encryption?
- No. It signs data with HMAC, which proves integrity and authenticity but does not hide the payload. Anyone holding the token can decode it; use encryption when confidentiality is required.
- Where should the secret key come from?
- Load it from an environment variable or a secret manager, and rotate it deliberately. A leaked key lets an attacker mint valid tokens for every user.
- How do timed tokens work?
- The token embeds a timestamp. loads(token, max_age=seconds) rejects tokens older than the limit, which is how password-reset and email-confirmation links expire.
- Can I use this for passwords?
- No. Signed tokens are not password hashes. Use a slow KDF such as argon2 or bcrypt for credentials and itsdangerous only for integrity-protected payloads.
Report a problem with this API docOpens a pre-filled issue on GitHub.