Skip to content

hardening: contain device backup file paths within the configured directories - #216

Merged
xmacan merged 2 commits into
developfrom
fix/contain-backup-paths
Nov 19, 2026
Merged

xmacan merged 2 commits into
developfrom
fix/contain-backup-paths

Conversation

@somethingwithproof

Copy link
Copy Markdown
Member

What

plugin_routerconfigs_download_config() builds the backup file and folder from device-controlled values — the device Directory field, the device Hostname, and a name parsed from the returned configuration — and used them directly when reading, writing, and removing the backup file.

This change routes those values through two new helpers before any path is built:

  • plugin_routerconfigs_safe_name($name) — requires a single filename component (no /, \, NUL, or ./..), applied to the hostname and the parsed configuration name.
  • plugin_routerconfigs_safe_subdir($dir) — keeps the per-device Directory field relative to the configured backup and archive roots, normalising separators, dropping leading slashes, and rejecting any .. component.

A device whose fields fail these checks has its backup skipped with a logged error, instead of acting on a path outside the configured backup/archive directories.

Tests

tests/Security/BackupPathContainmentTest.php covers both helpers (accepted and rejected inputs). Reverting the helpers to their prior behaviour fails the new test; the full plugin suite passes (358 tests, 1897 assertions).

Fixes #215

…ories

The per-device backup file and folder are named from device-controlled
values: the device 'directory' field, the device hostname, and a name
parsed from the returned configuration. plugin_routerconfigs_download_config()
now routes these through two helpers before building the read, write, and
removal paths:

  - plugin_routerconfigs_safe_name() requires a single filename component
    (no separators, NUL, or parent references), used for the hostname and
    the parsed name;
  - plugin_routerconfigs_safe_subdir() keeps the 'directory' field relative
    to the backup and archive roots, rejecting any '..' component.

A device whose fields fail these checks has its backup skipped with a logged
error instead of acting on a path outside the configured roots.

Adds tests/Security/BackupPathContainmentTest.php covering both helpers.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@somethingwithproof somethingwithproof added the hardening Defense-in-depth security hardening label Nov 19, 2026
strpbrk() already rejects '/', '\\', and NUL on every platform, so by the
time control reaches the basename() comparison it can never differ from the
input. The branch was unreachable, leaving its line uncovered. Removing it
keeps the single-component guarantee and restores full changed-line coverage.

Signed-off-by: Thomas Vincent <thomasvincent@gmail.com>
@sonarqubecloud

Copy link
Copy Markdown

@xmacan
xmacan merged commit eeb87b6 into develop Nov 19, 2026
7 checks passed
@xmacan
xmacan deleted the fix/contain-backup-paths branch November 19, 2026 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hardening Defense-in-depth security hardening

Projects

None yet

Development

Successfully merging this pull request may close these issues.

hardening: contain device backup file paths within the configured directories

2 participants