fix(history): sanitize query input to prevent FTS5 search syntax errors (#1026) by Pcmhacker-piro · Pull Request #1132 · imDarshanGK/AI-dev-assistant · GitHub
Skip to content

fix(history): sanitize query input to prevent FTS5 search syntax errors (#1026) - #1132

Open
Pcmhacker-piro wants to merge 4 commits into
imDarshanGK:mainfrom
Pcmhacker-piro:fix/history-search-injection-1026
Open

Pcmhacker-piro wants to merge 4 commits into
imDarshanGK:mainfrom
Pcmhacker-piro:fix/history-search-injection-1026

Conversation

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor

Description

Sanitizes the q query parameter in the /history/search endpoint to prevent SQLite FTS5 query syntax errors. Double-quote characters are removed, and search terms are individually wrapped in double quotes. This prevents syntax injection errors (e.g. from single double-quotes or dangling operators like AND, OR, *, (, )) which previously returned 500 Internal Server Error and leaked implementation details.

Related Issue

Fixes #1026

Type of change

  • Bug fix
  • New feature / enhancement
  • Documentation update
  • Test addition
  • Refactor

Checklist

  • I have read CONTRIBUTING.md
  • My branch is up to date with main
  • I have run pytest -v and all tests pass (Note: Local machine is running Python 3.9, which raises runtime TypeErrors on Python 3.10's newer | Union pipe syntax. Confirmed changes syntactically and added tests; validation will run in GitHub Actions CI).
  • I have not introduced duplicate issues or features
  • My PR title follows the format: feat/fix/docs/test: short description
  • I have added tests for new features (Level 2 and 3 issues)
  • No hardcoded secrets or API keys in my code
  • This PR is linked to a GSSoC 2026 issue

Screenshots (if frontend change)

N/A

Test evidence

GitHub Actions CI

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

@imDarshanGK imDarshanGK left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Pcmhacker-piro resolve conflicts

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

heyy @imDarshanGK
i fix the issue so pls check it

@imDarshanGK

Copy link
Copy Markdown
Owner

@Pcmhacker-piro resolve conflicts

…nd resolve conflicts

- database.py: keep PR's sanitization logic (includes q[:200] truncation)
- test_history.py: keep both test_search_history_syntax_injection (PR) and test_search_history_max_length (upstream)
- CHANGELOG.md: keep both changelog entries
@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

Hey @imDarshanGK, conflicts have been resolved. Here is what was done:

  • backend/app/services/database.py: Both sides added q = q[:200] truncation; kept the PR's full sanitization logic (which includes the truncation plus FTS5 query sanitization).
  • backend/tests/test_history.py: Kept both test_search_history_syntax_injection (PR) and test_search_history_max_length (upstream/main).
  • docs/CHANGELOG.md: Kept both changelog entries (sanitization from PR, max_length from upstream/main).

The PR should now be mergeable. Please take a look!

@Pcmhacker-piro

Copy link
Copy Markdown
Contributor Author

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.

[Bug] FTS5 query injection in /history/search allows malformed search expressions

2 participants