Skip to content

Path traversal: shopping list API and /recipe, /directory routes read files outside the recipe directory #530

Closed

Description

@LeMyst

Summary

Several cook server routes pass a client-supplied recipe path to the filesystem without checking it, so .., an absolute path, a drive letter (C:) or a UNC share (\\host\share) reaches files outside the recipe directory.

On Windows, even looking up a UNC path is enough to leak something: Windows connects to that host and sends the user's NTLM hash before anything is read.

Checked against main at 352a5a7.

Affected routes

Shopping list API (src/server/handlers/shopping_list.rs)

Route Handler What happens to the path
POST /api/shopping_list shopping_list Each entry's recipe becomes RecipeSource::Path, then goes extract_ingredients → find::get_recipe → cooklang_find::get_recipe, which joins it to the base directory. The response contains the ingredients of any .cook file on disk.
POST /api/shopping_list/add_menu add_menu_to_shopping_list Calls cooklang_find::get_recipe directly on payload.path.
POST /api/shopping_list/add add_to_shopping_list Stores payload.path in .shopping-list as given. It is resolved later: the shopping list page posts stored paths back to /api/shopping_list, and aggregate_current_ingredient_names resolves them on remove/compact.

None of these calls handlers::common::check_path, which the other API routes use.

UI routes (src/server/ui.rs, src/web/builders.rs)

/recipe/{*path} and /directory/{*path} have the same class of bug. axum percent-decodes the path, so ..%2F arrives as ../:

  • /recipe/..%2F..%2FWindows%2Fwin.ini renders win.ini as a recipe.
  • /directory/C:%2F lists the whole drive.

These are GET routes, and the default CORS policy lets any origin read GET responses, so any web page the user visits can read these. That makes them the more serious half.

Who can reach the shopping list endpoints

cors::write_guard refuses cross-origin non-GET requests, and the preflight only allows GET, so by default a page on another site cannot call these POST routes. A client that sends no Origin header passes the guard, though. That includes:

  • any local process;
  • any LAN client when the server runs with --host;
  • a cross-site page when the server runs with --cors-origin <that origin> or --no-csrf-check.

Reproduction

With recipes/ served and Outside-Secret.cook (containing Add @leaked-secret-marker{1}.) placed next to it, outside the served directory:

cook server ./recipes --port 9080
curl -s -X POST http://127.0.0.1:9080/api/shopping_list \
  -H 'Content-Type: application/json' \
  -d '[{"recipe":"../Outside-Secret"}]'

This returns 200 with {"name":"leaked-secret-marker", …} in the categories. /add and /add_menu also return 200 for the same path, and /add saves it to .shopping-list.

The complication: ../ recipe references

The obvious fix is to refuse every path that isn't a plain relative path. That breaks something, though: Cooklang allows parent-relative references such as @../sauces/tomato{}, and parse_reference keeps .. in components.

Today every reference is resolved from the recipe root, and a leading ./ is simply stripped. The seed depends on this: Breakfast/Mexican Style Burrito.cook writes @./Shared/Red Beans and means Shared/Red Beans.cook at the root, not a file next to itself. The same root-relative rule applied to .. means ../sauces/tomato always resolves outside the collection, whoever wrote it:

  • add_menu_to_shopping_list stores the reference with its ../ prefix (it strips only ./), and the shopping list then resolves it from the root, outside the directory.
  • Expansion::expand in crates/core/src/shopping_list/mod.rs and doctor::broken_references resolve it the same way.

So as things stand, a ../ reference either doesn't work or reads outside the root. A validator applied naively would turn that into a hard failure for any menu that uses one.

Proposed fix

Branch: LeMyst:fix/shopping-list-path-traversal, with two commits:

  1. 9652975: /recipe and /directory. Adds a single lexical check, is_safe_relative_path, run where the path is joined. It is purely lexical on purpose, because a filesystem lookup of a UNC path is itself the leak. On Windows it also refuses : in any component (past a drive letter, a : can only name an NTFS alternate data stream). The three copies of the check that had grown up (handlers::common, handlers::recipes, edit_page) now call it.
  2. c81609e: shopping list.
    • Reference resolution (find::resolve_reference in cookcli-core): a reference that contains .. steps up from the directory of the file that writes it; any other reference is read from the recipe root, exactly as before. The result is normalised, and a reference that still climbs out of the root (or normalises to something like ./C:/Windows/win.ini) resolves to nothing. It is then skipped with a warning, the same way references nested too deep or leading back into their own chain already are. Expansion::expand and doctor::broken_references use it.
    • Validation: all three endpoints run check_path before any filesystem access and return a 400 JSON error. Entries already stored in .shopping-list that fail the check are skipped during aggregation, so a list written before the fix cannot still read outside the directory.
    • Storage: add_menu stores the resolved, root-relative path (Shared/Sauce rather than ../Shared/Sauce). included_references is compared after resolving both sides, which keeps what the old ./-stripping did and also covers ../ references stored resolved.
    • is_safe_relative_path moves into cookcli-core next to the resolver and is re-exported from crate::util, so existing callers are unchanged.

Nothing that works today changes: the seed's nested ./Shared/... references resolve as before, add_menu on Weekly Plan.menu stores the same paths, and cook doctor validate still reports every reference valid.

Tests

  • tests/path_traversal_test.rs and tests/shopping_list_path_traversal_test.rs spawn cook server on <tmp>/recipes with a recipe outside that directory. The shopping list tests send their requests with no Origin header, so the write guard lets them through.
  • Unit tests in cookcli-core cover resolve_reference and the expansion of ../ references.
  • All 5 new shopping list end-to-end tests fail against current main (one shows the leaked marker in a 200 response) and pass with the fix.
  • cargo fmt, cargo clippy --all-targets and cargo test --workspace are clean.

Known gap (display only, not part of the fix)

The recipe and menu pages still build reference links from the reference as written (/recipe/./Shared/Vinaigrette). Browsers normalise the ./ away, so those links work. For a ../ reference, though, the browser resolves the link above /recipe/, so the link is broken. No path from those links reaches the filesystem unchecked. Fixing it means changing four near-identical spots in src/web/builders.rs and src/server/handlers/menus.rs that have snapshot tests, so I'd rather do it separately.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions