Skip to content

fix: add Content-Security-Policy header to all responses (#848)#874

Merged
komalharshita merged 1 commit into
komalharshita:mainfrom
Aryanbansal-05:Aryan-05-4
Jun 13, 2026
Merged

fix: add Content-Security-Policy header to all responses (#848)#874
komalharshita merged 1 commit into
komalharshita:mainfrom
Aryanbansal-05:Aryan-05-4

Conversation

@Aryanbansal-05

@Aryanbansal-05 Aryanbansal-05 commented Jun 13, 2026

Copy link
Copy Markdown
Contributor
  • Add CSP header restricting script, style, and resource sources
  • Prevents XSS and unauthorized resource loading

Summary [required]

The application sets several security headers but was missing a Content-Security-Policy (CSP) header, leaving it more vulnerable to XSS attacks and unauthorized resource loading. This PR adds a CSP header to the existing add_security_headers function in app.py, restricting the sources from which scripts, styles, images, and other resources can be loaded.
Related Issue
Closes #848

Type of Change [required]

  • Bug fix — resolves a broken behaviour
  • [ ]

What Was Changed [required]

| File | Change made |
app.py | Added Content-Security-Policy header to the add_security_headers after-request hook, restricting default-src, script-src, style-src, img-src, font-src, connect-src, and frame-ancestors to 'self'

How to Test This PR [required]

  1. Clone this branch: git checkout your-branch-name
  2. Install dependencies: pip install -r requirements.txt
  3. Run the app: python app.py
  4. Open http://127.0.0.1:5000 and...
  5. Run the tests: python tests/test_basic.py

Expected test output:

27 passed, 0 failed out of 27 tests

Test Results [required]

Verified app.py imports and runs correctly:
python -c "import app; print('OK')" → OK

Note: tests/test_basic.py currently fails to collect due to a
pre-existing import error (WEIGHT_LEVEL not found in utils.recommender),
unrelated to this change — already fixed in PR #834.
This PR only modifies the add_security_headers function in app.py.

Self-Review Checklist [required]

  • I have read CONTRIBUTING.md and followed all guidelines
  • My branch name follows the convention: feat/, fix/, docs/, data/, style/, test/
  • I have run python tests/test_basic.py and all 27 tests pass
  • I have run flake8 . locally and there are no errors
  • I have not introduced any print() or console.log() debug statements
  • Every new function I wrote has a docstring
  • I have not modified files outside the scope of the linked issue
  • If I changed the UI, I tested it at 375px (mobile) and 1280px (desktop)
  • If I added a project to the dataset, it has all required JSON fields

Notes for Reviewer

The CSP policy currently allows 'unsafe-inline' for scripts and styles to avoid breaking existing inline <script>/<style> usage in templates. If the maintainer prefers a stricter policy (nonce-based or hash-based), templates would need to be refactored to remove inline scripts/styles — happy to follow up with that as a separate PR if desired.

…ta#848)

- Add CSP header restricting script, style, and resource sources
- Prevents XSS and unauthorized resource loading
@vercel

vercel Bot commented Jun 13, 2026

Copy link
Copy Markdown

@Aryanbansal-05 is attempting to deploy a commit to the komalsony234-1530's projects Team on Vercel.

A member of the Team first needs to authorize it.

@Aryanbansal-05

Copy link
Copy Markdown
Contributor Author

Hi @komalharshita,

Just flagging — the failing CI checks here are due to the pre-existing WEIGHT_LEVEL import error in tests/test_basic.py (already fixed in PR #834), not related to this change. This PR only adds the CSP header to add_security_headers in app.py. Happy to rebase once #834 is merged. Thanks!

@komalharshita

Copy link
Copy Markdown
Owner

Approved for merge!

@komalharshita komalharshita merged commit e7a7c39 into komalharshita:main Jun 13, 2026
4 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Missing Content Security Policy (CSP) Header

2 participants