Reading preferences
Concept

Security Boundaries in Python Systems

Validate at trust boundaries and design explicit failure handling.

python, security, trust boundaries, validation, production

Trust changes at boundaries

External requests, files, dependencies, subprocesses, and databases are separate trust boundaries. Treat inputs as untrusted until checked for the specific operation. Validation, authentication, and authorisation answer different questions: is the input acceptable, who is the caller, and may that caller perform this action?

Python illustration

def page_size(value):
    if not isinstance(value, int) or isinstance(value, bool):
        raise TypeError("page size must be an integer")
    if not 1 <= value <= 100:
        raise ValueError("page size must be between 1 and 100")
    return value

print(page_size(20))

Prepared output — not live execution:

20

The function rejects booleans explicitly because bool is a subclass of int, and rejects values outside the allowed range. A caller must still handle these failures appropriately; the check does not authenticate or authorise anyone.

See the reasoning

Check the input boundary

Compare accepted and rejected inputs without confusing validation with authorisation. This is a prepared demonstration, not live code execution.

Receive 20

Validation decision
  1. Input20Current focus
  2. Typepending
  3. Rangepending
  4. Resultpending

The caller supplies an integer; identity and permission are separate checks.

Protect the full lifecycle

Use parameterised queries for values, allowlist dynamic identifiers, and avoid constructing shell commands from untrusted input. When subprocesses are required, prefer an argument list and shell=False, but still validate arguments and choose a trusted executable; this does not make arbitrary programs safe. Never unpickle untrusted data or evaluate it as Python.

Limit input sizes and resource use. Protect secrets outside source code, minimise permissions, update dependencies deliberately, and review their provenance. Do not log secrets or sensitive personal data. Return an appropriate failure without exposing internal tracebacks to untrusted callers, while retaining safe diagnostics for maintainers.

Filesystem access requires careful boundary design: checking or resolving a path alone does not eliminate symbolic-link races. Production reliability also needs tests, observability, backups where applicable, and a recovery plan. These are principles for separate systems; Intuvo remains static.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. What is a trust boundary?
2. Does input validation replace authorisation?
3. Where should production credentials be kept?
4. How should a service handle an unexpected failure?

References and further reading