Skip to content

Potential fix for code scanning alert no. 9: Uncontrolled data used in path expression - #74

Merged
CameronBrooks11 merged 9 commits into
mainfrom
alert-autofix-9
May 10, 2026
Merged

CameronBrooks11 merged 9 commits into
mainfrom
alert-autofix-9

Conversation

@CameronBrooks11

Copy link
Copy Markdown
Member

Potential fix for https://github.com/CameronBrooks11/fiberpath/security/code-scanning/9

General fix approach: enforce strict validation of untrusted path strings before constructing filesystem paths. Accept only relative, non-empty, non-traversing paths, then canonicalize and enforce allowed-root containment.

Best concrete fix in fiberpath_api/path_policy.py:

  • In _resolve_user_path, add pre-validation on user_path:
    • reject NUL byte input,
    • reject absolute paths (both POSIX /... and Windows drive/UNC forms),
    • reject any .. traversal segment.
  • Then keep existing resolution and _is_within_roots containment checks.
    This preserves intended functionality (reading files under configured roots) while removing risky/ambiguous input forms and addressing all 3 variants because all endpoints call enforce_input_path_policy.

No route-file changes are required; central policy hardening covers plan.py, simulate.py, and validate.py.

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…n path expression

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread fiberpath_api/path_policy.py Fixed
…h-policy tests

- Remove blanket rejection of all absolute paths; instead resolve them and
  validate against configured roots (403 if outside, accepted if inside).
- Relative paths continue to be resolved from each root in order.
- NUL bytes, .. traversal segments, and empty/whitespace paths still
  rejected with 400 before any filesystem access.
- Add tests/api/test_path_policy.py covering all rejected cases (NUL,
  POSIX absolute outside root, Windows drive path, UNC, .. traversal
  with forward/backslash separators, empty, whitespace, relative outside
  root) and accepted cases (bare filename, nested relative, absolute inside
  root, absolute in subdirectory, path in second of multiple roots, single-
  dot segments).
Comment thread fiberpath_api/path_policy.py Fixed
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread fiberpath_api/path_policy.py Fixed
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread fiberpath_api/path_policy.py Fixed
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Comment thread fiberpath_api/path_policy.py Fixed
CameronBrooks11 and others added 4 commits May 10, 2026 13:09
…ed in path expression'

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
… roots

The bot's autofix re-introduced two problems:
1. Blanket 400 rejection of all absolute paths - breaks integration tests and
   legitimate clients that supply full paths inside a configured root.
2. _SAFE_PATH_SEGMENT_RE whitelist ([A-Za-z0-9._ -]) rejects parens/plus/etc.
   in real filenames (e.g. AvBay(470mm)single.wind).

Restore the correct approach: absolute paths that resolve within a configured
root are accepted; those outside get 403. Relative paths are resolved from
each root in order. NUL bytes and '..' segments are still rejected with 400
before any filesystem access.

Also fix ruff lint errors in test_path_policy.py (I001 import order, E501
line-too-long on two method signatures).
…ests

Root cause of CI failures on Linux/macOS:
  On POSIX, backslash is a regular filename character, so C:\Windows\...
  and \\server\share\... were treated as relative paths that resolved
  happily within the configured root - no exception raised.

Fix in _resolve_user_path:
  - Normalise backslashes to forward slashes before all checks.
  - Reject POSIX absolute paths (leading /) with 400.
  - Reject Windows drive-absolute paths (alpha + ':') with 400.
  - UNC paths (\\server\share) normalise to //... and are caught by the
    leading-/ check above - no separate branch needed.
  - Build the returned path from Path(*validated_parts) joined onto a
    trusted root, never from the raw user string.  This breaks the taint
    chain that triggered the CodeQL 'Uncontrolled data used in path
    expression' alert.

Integration test updates (test_plan_route, test_simulate_route):
  - Pass relative paths ('input.wind', 'program.gcode') to the API
    instead of absolute tmp_path-based paths, matching the new policy.
  - Replace 'rejects path outside allowed roots' (403) tests with
    'rejects absolute path' (400) and 'rejects traversal' (400) tests.

test_path_policy updates:
  - Split single POSIX-absolute test into dedicated tests per path type
    (POSIX /, Windows drive C:, Windows drive C:/, UNC backslash, UNC /).
  - All of the above now assert 400 - cross-platform, no skipif needed.
  - Replace 'absolute inside root accepted' tests with
    'absolute_path_always_rejected' (400).
  - Fix multi-root accepted test: relative paths resolve from the first
    root, so test each root independently.
@CameronBrooks11
CameronBrooks11 marked this pull request as ready for review May 10, 2026 18:07
@CameronBrooks11
CameronBrooks11 merged commit 016b2e4 into main May 10, 2026
18 checks passed
@CameronBrooks11
CameronBrooks11 deleted the alert-autofix-9 branch May 10, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants