Reject unsafe filesystem paths from the HACS manifest#5380
Open
frenck wants to merge 2 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens HACS manifest handling by preventing repository-provided manifest fields (filename, persistent_directory) from being used to form unsafe filesystem paths that could escape intended directories.
Changes:
- Introduces
is_safe_relative_path()and applies it to manifest path-like fields. - Enforces safe-relative-path validation in the publish-time schema (
HACS_MANIFEST_JSON_SCHEMA). - Sanitizes unsafe values at runtime in
HacsManifest.from_dict()(dropping them with a warning) and adds/extends tests covering the behavior.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
custom_components/hacs/utils/path.py |
Adds the shared safe-relative-path helper used to gate manifest-derived paths. |
custom_components/hacs/utils/validate.py |
Updates the HACS manifest JSON schema to validate filename/persistent_directory as safe relative paths. |
custom_components/hacs/repositories/base.py |
Applies runtime sanitization of filename/persistent_directory when constructing HacsManifest from a dict. |
tests/utils/test_validate.py |
Adds schema-level tests ensuring unsafe paths and non-strings are rejected. |
tests/utils/test_path.py |
Adds unit tests for is_safe_relative_path() behavior. |
tests/repositories/test_hacs_manifest.py |
Adds runtime tests verifying unsafe values are ignored (and safe ones retained) during from_dict(). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Proposed change
The
filenameandpersistent_directoryvalues fromhacs.jsonend up in filesystem paths ({temp_dir}/{filename}in both zip download paths, and{content.path.local}/{persistent_directory}during install). They were only validated asstr, so a hostile manifest could point them outside the intended directory with values like../../custom_components/eviland place files outside its category lane.This adds a shared
is_safe_relative_path()helper that rejects absolute paths and any..segment (both slash styles), and applies it in two places:HACS_MANIFEST_JSON_SCHEMA: used by the HACS action, so default repository submissions with such values are rejected loudly at publish time.HacsManifest.from_dict: the single construction point for all runtime manifests (fetchedhacs.json, custom repositories, storage restore). Unsafe values are dropped with a warning instead of raising, so a bad stored manifest can never break the startup restore.Nested relative paths like
sub/dirremain allowed, existing legitimate repositories are unaffected.For completeness: the zip extraction paths themselves were checked and are not vulnerable to zip-slip, Python's
zipfile.extractallsanitizes..components and absolute member names. This change is about the manifest values, which bypass that sanitization because they are used to build paths directly.Type of change
Checklist