Skip to content

fix: enforce guard max prompt length#820

Open
aishwaryagangaraj-web wants to merge 10 commits into
SdSarthak:mainfrom
aishwaryagangaraj-web:fix/guard-max-prompt-length
Open

fix: enforce guard max prompt length#820
aishwaryagangaraj-web wants to merge 10 commits into
SdSarthak:mainfrom
aishwaryagangaraj-web:fix/guard-max-prompt-length

Conversation

@aishwaryagangaraj-web
Copy link
Copy Markdown
Contributor

Summary

Closes #763

This PR enforces GUARD_MAX_PROMPT_LENGTH validation at the API request boundary for Guard scan endpoints.

Previously, /guard/scan accepted arbitrarily large prompt payloads despite the configured GUARD_MAX_PROMPT_LENGTH already existing in settings. Validation only occurred later in the Guard pipeline, increasing unnecessary CPU and memory usage.

This change rejects oversized prompts early at the request schema level and applies the same validation to batch scan requests.

Changes Made

  • Enforced GUARD_MAX_PROMPT_LENGTH on ScanRequest
  • Added min_length=1 and max_length=settings.GUARD_MAX_PROMPT_LENGTH
  • Applied the same per-prompt validation to BulkScanRequest
  • Prevented /guard/scan/batch from bypassing prompt length restrictions
  • Added validation tests for oversized prompts in both single and batch scan endpoints

Type of Change

  • Bug fix
  • New feature
  • Documentation update
  • Refactor
  • Tests
  • Infra / CI

Checklist

  • I have read CONTRIBUTING.md
  • My code follows the project style (PEP 8 for Python, ESLint for TS)
  • I have added/updated tests where relevant
  • pytest backend/tests/ passes locally
  • I have not committed .env or any secrets
  • I have updated documentation if needed

Testing

python -m pytest backend/tests/test_guard_api.py
python -m pytest backend/tests

Result:

  • 13 tests passed in test_guard_api.py
  • 328 tests passed overall
  • 0 failures

Copy link
Copy Markdown
Owner

@SdSarthak SdSarthak left a comment

Choose a reason for hiding this comment

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

The GuardPrompt type alias for prompt length enforcement is a clean approach. However this PR also removes the webhook trigger block that was intentionally added — the deliver_webhook call on guard_block decisions is a feature, not dead code. Please restore it before merging.

This PR also conflicts with recent merges to guard.py. After rebasing and restoring the webhook block, this is ready to merge.

@SdSarthak SdSarthak added gssoc:approved GSSoC approved contribution — required for points to count level:intermediate Intermediate difficulty task type:bug Bug fix labels May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

gssoc:approved GSSoC approved contribution — required for points to count level:intermediate Intermediate difficulty task type:bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Guard scan accepts unbounded prompt payloads despite max length setting

2 participants