fix(api): confine absolute-path asset URIs to the asset directory#3117
Merged
Conversation
The asset-create serializers accept a `/`-prefixed `uri` after only an `isfile()` check, then `rename` it into the asset store and serve it back verbatim via the content endpoint. With auth disabled (the default install), any LAN client could POST `uri=/data/.anthias/anthias.conf` to read `django_secret_key` (or the SQLite DB's password hashes) and, because `rename` moves the source, corrupt the device. Confine `/`-prefixed URIs to `settings['assetdir']` in the shared `validate_uri`, resolving symlinks on both sides so a staged symlink can't escape. The only legitimate absolute-path flow — the two-step `file_asset` upload that stages files as `<assetdir>/<uuid>.tmp` — stays within the asset dir and is unaffected. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
There was a problem hiding this comment.
Pull request overview
This PR addresses a security flaw in the asset-create API where absolute-path (/-prefixed) uri values could reference arbitrary readable files on the host, which were then moved into the asset store and retrievable via the content endpoint.
Changes:
- Added
_is_within_assetdir()and applied it invalidate_uri()to confine absolute-path URIs tosettings['assetdir']usingrealpath+ trailing-separator prefix checks. - Added a regression test ensuring asset creation rejects absolute-path URIs that resolve outside the asset directory and that no destructive
renameoccurs.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/anthias_server/api/tests/test_assets.py |
Adds regression test for rejecting absolute-path URIs outside the asset directory and ensuring no file move / DB persistence occurs. |
src/anthias_server/api/serializers/__init__.py |
Introduces _is_within_assetdir() and updates validate_uri() to reject absolute-path URIs outside the configured asset directory. |
Comments suppressed due to low confidence (1)
src/anthias_server/api/serializers/init.py:60
validate_uriraises a genericExceptionfor invalid user input. In these API views, that can bubble up as a 500 instead of a structured 400 validation response; with the new assetdir confinement, more invalid requests will hit this path. Prefer raising DRFValidationErrorkeyed to theurifield soserializer.is_valid()returnsFalseand the views respond with HTTP 400.
def validate_uri(uri: str) -> None:
if uri.startswith('/'):
if not _is_within_assetdir(uri) or not path.isfile(uri):
raise Exception('Invalid file path. Failed to add asset.')
else:
if not validate_url(uri):
raise Exception('Invalid URL. Failed to add asset.')
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Address review: raise DRF ValidationError keyed on `uri` from `validate_uri` so `serializer.is_valid()` catches it and the create view returns a 400 rather than a 500 (`validate_uri` only runs inside the create serializers' `prepare_asset`). The regression test now asserts the exact 400 and the `uri` error key, so it can't pass on an unrelated server error. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
The asset-create API accepts an absolute-path (
/-prefixed)uriafter only an existence check, then moves it into the asset store and serves it back — a pre-auth arbitrary file read (and destructive move) on a default install.validate_uri(api/serializers/__init__.py) only ranpath.isfile(uri)for a/-prefixed URI, with no confinement to the asset directory.prepare_assetthenrenames that file into<assetdir>/<uuid>(source removed), andAssetContentViewMixin.getreads it back as base64.auth_backenddefaults to'',@authorizedno-ops, so any unauthenticated LAN client could:POST /api/v2/assets {"uri": "/data/.anthias/anthias.conf", "mimetype": "image", ...}→ the file is moved into the asset store.GET /api/v1/assets/<id>/content→ base64 ofanthias.conf, which containsdjango_secret_key(enough to forge signed sessions and the internal-auth token). Targetinganthias.dbdiscloses operator password hashes; either target is also destroyed by the move.Fix
Confine
/-prefixed URIs tosettings['assetdir']in the sharedvalidate_uri, applyingrealpathto both sides so a symlink staged inside the asset dir can't resolve back out. The trailing separator prevents a sibling-prefix bypass (<assetdir>_evil).The only legitimate absolute-path flow is the two-step
file_assetupload, which stages files as<assetdir>/<uuid>.tmp— inside the asset dir, so it is unaffected. The HTML create path already rejects absolute paths viavalidate_url; this gap was API-only.Tests
Adds
test_create_asset_rejects_absolute_uri_outside_assetdir(parametrized across v1/v1.1/v1.2/v2): a real file placed outside the asset dir is rejected before therename, no row is persisted, and the source file is left intact. Verified the confinement accepts a genuine in-assetdirpath and blocks/etc/passwd, the sibling.anthias/anthias.conf,..traversal, and the_evilsibling-prefix case.🤖 Generated with Claude Code