Something went wrong. Try again.
mirror of github.com/PrefectHQ/fastmcp
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475---title: securitysidebarTitle: security---
# `fastmcp.resources.security`
Path-safety policy for templated resource parameters.
Templated resources (`@mcp.resource("file:///{path}")`-style) extractparameter values straight out of the request URI and hand them to theresource function. When those values flow into filesystem or URIconstruction, a malicious client can smuggle path-traversal payloads(`../`, absolute paths, null bytes) through the template.
`ResourceSecurity` screens extracted parameter values *before* theresource handler runs. It is applied by default to every templatedread, mirroring the posture of the underlying MCP SDK's`ResourceSecurity` (traversal, absolute paths, and null bytes rejected).
The screening reuses the SDK's component-based traversal check, so avalue that merely *contains* dots (e.g. `HEAD~3..HEAD`, `v1..v2`,`file.tar.gz`) is not rejected — only an actual `..` path segment is.
## Classes
### `InheritSecurity` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/security.py#L49" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Sentinel type: inherit the server-wide resource-security default.
Distinguishes "no per-component policy was set" (inherit whatever theserver configured) from an explicit ``None`` (screening disabled forthis component).
### `ResourceSecurity` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/security.py#L76" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
Security policy applied to extracted resource template parameters.
These checks run after a URI has matched a template and itsparameter values have been extracted and percent-decoded. They catchpath-traversal and absolute-path injection regardless of how thevalue was encoded in the URI (literal, `%2F`, `%5C`, `%2E%2E`).
All checks default on. Screen a value like `HEAD~3..HEAD` (dotsinside a single segment) passes — only a standalone `..` segment istreated as traversal.
**Methods:**
#### `validate` <sup><a href="https://github.com/PrefectHQ/fastmcp/blob/main/fastmcp_slim/fastmcp/resources/security.py#L127" target="_blank"><Icon icon="github" style="width: 14px; height: 14px;" /></a></sup>
```pythonvalidate(self, params: Mapping[str, object]) -> str | None```
Check all parameter values against the configured policy.
String values (and lists of strings, from wildcard `{path*}`parameters that span multiple segments) are screened; non-stringvalues are ignored, since traversal is a string-path concern.
**Args:**- `params`: Extracted template parameters.
**Returns:**- The name of the first parameter that fails, or `None` if all- values pass.