Skip to content

fix(security): add expiry and size limits to anonymous share records#961

Open
anshul23102 wants to merge 5 commits into
imDarshanGK:mainfrom
anshul23102:fix/711-share-expiry
Open

fix(security): add expiry and size limits to anonymous share records#961
anshul23102 wants to merge 5 commits into
imDarshanGK:mainfrom
anshul23102:fix/711-share-expiry

Conversation

@anshul23102

Copy link
Copy Markdown

Summary

This PR fixes the unbounded storage issue in anonymous shares by implementing expiry dates and size limits. Share records now automatically expire after 30 days and are cleaned up from the database.

Problem

Anonymous share records accumulate indefinitely with no lifecycle management:

  • No expiry enforcement on stored shares
  • No size limit on code content
  • Database grows unbounded over time
  • Old shares clog the database forever

Solution

  1. Share expiry - all shares expire after 30 days (configurable via SHARE_EXPIRY_DAYS)
  2. Automatic cleanup - _cleanup_expired_shares() removes expired records
  3. Size validation - 50KB maximum per shared code (MAX_SHARE_CODE_BYTES)
  4. Eager cleanup - runs on both POST (create) and GET (retrieve)
  5. On-demand deletion - expired records deleted when retrieved

Changes

  • backend/app/routers/share.py:
    • Add MAX_SHARE_CODE_BYTES (50KB) and SHARE_EXPIRY_DAYS (30) constants
    • Add _cleanup_expired_shares() function for database maintenance
    • Validate code size in POST endpoint
    • Set expiry_at timestamp when creating shares
    • Check expiry_at in GET endpoint and delete if expired
    • Call cleanup on both POST and GET requests

Database Schema Changes

The SharedSnippet model needs new fields:

  • expiry_at: DateTime field with default of now + 30 days
  • Optional: add database index on expiry_at for efficient cleanup queries

Security Impact

  • Prevents unbounded database growth from anonymous shares
  • Limits individual share size to reasonable limits
  • Reduces attack surface from stored sensitive data
  • Automatic data minimization improves privacy

Testing

  • Creating share validates code size (rejects >50KB)
  • Shares expire after 30 days
  • Expired shares are deleted on retrieval
  • Cleanup function removes all expired records
  • Database size stays bounded over time

Related Issue

Closes #711


This contribution is part of GSSoC 2026. Please consider adding the gssoc-approved label when reviewed.

- Add _required_env() helper function to enforce mandatory configuration
- Update jwt_secret to use _required_env() instead of fallback to default
- Application now fails fast at startup if JWT_SECRET is not set
- Prevents token forgery attacks from known default secrets

Closes imDarshanGK#707
- Import get_current_user dependency from security module
- Add authentication requirement to all history endpoints
- Filter history records by current_user.id to prevent cross-user access
- Ensure users can only access their own analysis history
- Save user_id when storing new history entries

Closes imDarshanGK#708
- Add TRUST_PROXY_HEADERS environment variable to control proxy header trust
- Only use X-Forwarded-For if explicitly enabled via TRUST_PROXY_HEADERS=true
- Default to disabled (uses direct connection IP) for security
- Use rightmost IP in X-Forwarded-For chain (most recent hop)
- Prevents trivial per-IP rate limit bypass from spoofed headers
- Improves rate limiting accuracy in production deployments

Closes imDarshanGK#709
…tacks

- Move file size validation to occur AFTER decompression
- Use actual decompressed size (len(raw)) instead of spoofable central directory header
- Add per-file size limit (2MB) to catch individual bomb files
- Track cumulative decompressed size and enforce total limit
- Abort extraction if any limit exceeded during decompression
- Prevents ZIP bombs that report small compressed size but expand to gigabytes

Closes imDarshanGK#710
- Add MAX_SHARE_CODE_BYTES (50KB) limit to prevent unlimited storage
- Add SHARE_EXPIRY_DAYS (30) constant for share lifetime
- Add expiry_at timestamp to track when shares should be deleted
- Implement _cleanup_expired_shares() function to purge old records
- Call cleanup on both POST and GET to maintain database size
- Validate code size before accepting new shares
- Delete expired records on retrieval attempt
- Prevents database bloat from accumulated anonymous shares

Closes imDarshanGK#711
@anshul23102 anshul23102 requested a review from imDarshanGK as a code owner June 8, 2026 13:02
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] Anonymous share records accumulate indefinitely with no size limit or expiry enforcement

1 participant