Skip to content

fix: normalize env palace_path and validate KG date fields#817

Open
Kesshite wants to merge 4 commits intoMemPalace:developfrom
Kesshite:fix/security-config-validation
Open

fix: normalize env palace_path and validate KG date fields#817
Kesshite wants to merge 4 commits intoMemPalace:developfrom
Kesshite:fix/security-config-validation

Conversation

@Kesshite
Copy link
Copy Markdown
Contributor

Summary

  • MEMPALACE_PALACE_PATH env var was used without expanduser() or abspath(), allowing path traversal via ../../tmp/evil. Now normalized consistently with the --palace CLI arg.
  • as_of, valid_from, ended date params in KG MCP tools reached SQLite without format validation. Invalid strings silently broke temporal filtering (queries returned empty instead of matching facts). Now validated as ISO-8601 at the MCP boundary.

Refs: #809 (Findings 7, 8)

What changed

mempalace/config.py

  • palace_path property — applies os.path.abspath(os.path.expanduser()) to env var values

mempalace/mcp_server.py

  • _validate_date() — new helper accepting YYYY-MM or YYYY-MM-DD, raises ValueError otherwise
  • tool_kg_query() — validates as_of
  • tool_kg_add() — validates valid_from
  • tool_kg_invalidate() — validates ended

tests/test_config.py

  • test_env_override — updated to assert normalized absolute path

tests/test_config_extra.py

  • test_env_palace_path_normalizes_traversal — verifies ../ resolved
  • test_env_palace_path_expands_tilde — verifies ~/ expanded

tests/test_mcp_server.py

  • test_kg_query_rejects_invalid_date
  • test_kg_query_accepts_valid_date
  • test_kg_query_accepts_year_month
  • test_kg_add_rejects_invalid_valid_from
  • test_kg_invalidate_rejects_invalid_ended

Test plan

  • pytest tests/test_config.py tests/test_config_extra.py -v — 23/23 passed
  • pytest tests/test_mcp_server.py::TestDateValidation -v — 5/5 passed
  • pytest tests/ -v --ignore=tests/benchmarks — 693 passed, 2 failed (pre-existing version mismatch)
  • ruff check — all checks passed
  • ruff format --check — all files formatted
  • No new dependencies added

@igorls igorls added area/kg Knowledge graph area/mcp MCP server and tools bug Something isn't working labels Apr 14, 2026
@Kesshite Kesshite force-pushed the fix/security-config-validation branch from 68c2d86 to 59fc0dd Compare April 14, 2026 11:07
@bensig
Copy link
Copy Markdown
Collaborator

bensig commented Apr 15, 2026

hey @Kesshite — this conflicts with develop now. pls rebase and we'll get it in. thanks!

@bensig
Copy link
Copy Markdown
Collaborator

bensig commented Apr 15, 2026

@Kesshite pls check conflicts

config.py:
- Apply os.path.abspath(os.path.expanduser()) to MEMPALACE_PALACE_PATH
  and MEMPAL_PALACE_PATH env vars, preventing path traversal via
  crafted values like '../../tmp/evil'. Consistent with --palace CLI
  arg which already uses abspath in mcp_server.py.

mcp_server.py:
- Add _validate_date() helper accepting YYYY-MM or YYYY-MM-DD format
- Apply to tool_kg_query (as_of), tool_kg_add (valid_from), and
  tool_kg_invalidate (ended) at the MCP boundary
- Invalid dates now return a clear error instead of silently breaking
  temporal filtering (queries returning empty when facts exist)

Tests:
- Path traversal normalization (../../ evil resolved)
- Tilde expansion (~/ resolved to home dir)
- Existing env override test updated for abspath behavior
- Invalid date rejection (not-a-date, 2026-99-99, yesterday)
- Valid date acceptance (YYYY-MM-DD, YYYY-MM)

Refs: MemPalace#809
…ce arg

- config.py palace_path: normalize all sources (env var, config file,
  default) with expanduser+abspath, not just env vars — a crafted
  config.json with "../../evil" was not being resolved
- mcp_server.py: add expanduser before abspath on --palace CLI arg,
  so --palace ~/path expands correctly before being stored in env var
- test_config.py: wrap test_env_override in try/finally to prevent
  env var leak on assertion failure; update test_config_from_file
  to handle Windows drive letter prefix from abspath

Refs: MemPalace#809
@Kesshite Kesshite force-pushed the fix/security-config-validation branch from 59fc0dd to 016d424 Compare April 15, 2026 11:29
@Kesshite
Copy link
Copy Markdown
Contributor Author

Rebased on latest develop — resolved the 3 conflicts in mcp_server.py where sanitize_name was renamed to sanitize_kg_value upstream. Our _validate_date() calls now sit alongside the updated sanitizer. All tests pass locally (46 targeted, 0 failed). Thanks @bensig.

Formatting-only changes on 7 files that arrived from develop
during rebase and were not formatted upstream. No logic changes.
Reformatted 6 upstream test files with ruff 0.4.x (matching CI
pinned version) to pass ruff format --check in CI pipeline.
@Kesshite Kesshite force-pushed the fix/security-config-validation branch from 4d44054 to 618d324 Compare April 15, 2026 11:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/kg Knowledge graph area/mcp MCP server and tools bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants