Skip to main content

Wyrd - Helpers for Domain driven security

Wyrd provides the following tools to help write more secure code:

  • Constrained Types - Extensions to primitive types (float, int and str) add extra validation rules on the values
  • Read Once wrapper - Wraps any data and ensures that the value can only be accessed a single time. Helps prevent data ending up leaking into the wrong places.

Constrained Types

Statement: Nothing should ever really be modelled as any String or any integer.

Example

A user wants to order a number of books. Can it be zero? Can it be negative?

@add_constraint(lambda x: x > 0, "Order must be at least 1")
class OrderQuantity(ConstrainedInt):
    pass

now:

quantity = OrderQuantity(5)

# works exactly like an int
total_price = quantity * 5

# but you can't create sneaky discount books
quantity = OrderQuantity(-1)
# !! raises ValueError

Multiple constraints

@add_constraint(lambda x: x > 0, "Order must be at least 1")
@add_constraint(lambda x: x < 200, "Our shipping system can't send more then 200")
class OrderQuantity(ConstrainedInt):
    pass

In addition the constraints are guaranteed to execute in order so any expensive checks to run can be listed further down.

Cache results

If you expect the same value multiple times you can add caching for the validation. The actual caching is passed to functools.lru_cache.

@add_constraint(lambda x: len(x) > 0, "The order id must be set")
@add_constraint(lambda x: len(x) < 10, "Order ids are under 10 chars")
@add_constraint(some_really_complicated_checksum, "The order number was invalid")
@cache_constraint_results(maxsize=100)
class OrderId(ConstrainedString):
    pass

Works well with mypy (or other static type checkers)

# The following will type check fine. OrderId is a real type
def retrieve_order(order_id: OrderId):
    ...

maybe something further down only accepts strings:

def _fetch_item_from_db(table_name: str, item_id: str):
    ...

def retrieve_order(order_id: OrderId):
    # The following will type check fine. OrderId extends str
    return _fetch_item_from_db("orders", order_id)

Integrates with pydantic

@add_constraint(lambda x: x > 0, "Order must be at least 1")
class OrderQuantity(ConstrainedInt):
    pass


@add_constraint(lambda x: len(x) == 4, "Invalid order id")
class BookId(ConstrainedString):
    pass


class Order(BaseModel):
    book_quantity: OrderQuantity
    book_id: BookId

Why not use the pydantic version of these constrained types?

The pydantic types only really work with pydantic. Invalid instances can be created by constructing directly. Since constraint checking is triggered by the constructor the constraints will always be true for any instance of the class.

Read Once

This object helps make sure sensitive values don't leak out by accident. Accessing the contents for a second time will trigger an exception.

# Set the content
sensitive = ReadOnce("hello - only once")

# Access the content - works fine
sensitive.get_contents()

# !! RAISES ReadTwiceError exception !!
sensitive.get_contents()

Integrates with pydantic

class SomeData(BaseModel):
    secret: ReadOnce[str]

Metadata

Release files for wyrd 0.8.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 wyrd 0.8.1
File Size Uploaded Provenance
wyrd-0.8.1.tar.gz 18.5 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for wyrd 0.8.1
File Interpreter ABI Platform
wyrd-0.8.1-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 25.5 kB

Release files / wyrd-0.8.1.tar.gz

Download URL wyrd-0.8.1.tar.gz
Size 18.5 kB
Tags Source
SHA-256 checksum
How to use checksums
ad56689141c1d3ca8d8f13a4444ad3a5e1b891e413e71df4a2100dcb98b43a35
BLAKE2b-256 checksum
How to use checksums
fcbc110f53f4450672911c4d4b90f29e9ed185f16cd18d569192fc6f888534ee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Release files / wyrd-0.8.1-py2.py3-none-any.whl

Download URL wyrd-0.8.1-py2.py3-none-any.whl
Size 6.9 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
8b0a475a25b7633c8bd2038d2b0d806806dd8be1f151d4be6362d584a3a5026c
BLAKE2b-256 checksum
How to use checksums
b7ead2d0fd024e7fc46ca1cec3a7d4a891a78834687bd5eee08513dd5cf84cb0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-requests/2.26.0

Security for wyrd 0.8.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