Skip to content

HistoryService: path traversal sanitization and exception resilience#636

Merged
TheJoeFin merged 2 commits intobetter-memfrom
copilot/sub-pr-634
Mar 10, 2026
Merged

HistoryService: path traversal sanitization and exception resilience#636
TheJoeFin merged 2 commits intobetter-memfrom
copilot/sub-pr-634

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

HistoryService used persisted WordBorderInfoFileName values directly in Path.Combine without sanitization, enabling path traversal. File delete and deserialization paths also had no error handling, meaning a corrupt/locked file could crash history operations.

Changes

  • GetWordBorderInfosAsync: sanitize WordBorderInfoFileName via Path.GetFileName() and reject non-.json extensions before path combination; wrap sidecar file IO + deserialization in IOException/JsonException handlers with fallthrough to inline JSON; wrap inline JSON deserialization in JsonException handler returning []
  • DeleteHistoryFile: wrap File.Delete in IOException/UnauthorizedAccessException — locked files log and continue instead of throwing
  • DeleteUnusedWordBorderFiles: same per-iteration exception handling so one inaccessible sidecar doesn't abort cleanup of the rest
// Before — rooted or traversal path escapes history dir
string wordBorderInfoPath = Path.Combine(historyBasePath, history.WordBorderInfoFileName);

// After — sanitized + extension-validated
string sanitizedFileName = Path.GetFileName(history.WordBorderInfoFileName);
if (!string.IsNullOrWhiteSpace(sanitizedFileName)
    && string.Equals(Path.GetExtension(sanitizedFileName), ".json", StringComparison.OrdinalIgnoreCase))
{
    string wordBorderInfoPath = Path.Combine(historyBasePath, sanitizedFileName);
    ...
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

…vice

Co-authored-by: TheJoeFin <7809853+TheJoeFin@users.noreply.github.com>
Copilot AI changed the title [WIP] Improve memory usage and persistence for data HistoryService: path traversal sanitization and exception resilience Mar 10, 2026
@TheJoeFin TheJoeFin marked this pull request as ready for review March 10, 2026 22:45
@TheJoeFin TheJoeFin merged commit 7007d11 into better-mem Mar 10, 2026
@TheJoeFin TheJoeFin deleted the copilot/sub-pr-634 branch March 10, 2026 22:45
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