feat(security): Add CSRF protection for resource-intensive endpoints #109
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.



Summary
Implements CSRF protection for resource-intensive endpoints to prevent cross-site request abuse. Addresses security vulnerability where attackers could force victims to trigger expensive Git clone operations.
Changes
Backend
New Middleware:
strictContentType(apps/backend/src/middlewares/strictContentType.ts)application/jsoncontent type for POST/PUT/DELETE requestsX-Requested-Withheader to prevent simple CSRF attacksApplied Protection (
apps/backend/src/index.ts)/api/repositories(handles cloning)/api/commits(handles streaming)Unit Tests (
apps/backend/__tests__/unit/middlewares/strictContentType.unit.test.ts)Frontend
apps/frontend/src/services/api.ts)X-Requested-With: XMLHttpRequestheader to all API requestsSecurity Impact
✅ Blocks preflightless CSRF attacks: Requests with
application/x-www-form-urlencodedortext/plainare rejected✅ Requires CORS preflight:
application/jsoncontent type triggers browser preflight checks✅ Custom header protection:
X-Requested-Withheader cannot be set by HTML forms✅ Resource protection: Prevents unauthorized triggering of expensive Git operations
Testing
Automated Tests
Manual Validation (as per issue)
Test 1: Block form-encoded requests
Test 2: Block text/plain requests
Test 3: Block JSON without custom header
Test 4: Allow valid requests
Breaking Changes
Content-Type: application/jsonheaderX-Requested-With: XMLHttpRequestheader (or any value)This is intentional to prevent CSRF attacks. The frontend has been updated accordingly.
Related
Closes #98
Validation Checklist (from AGENTS.md)
pnpm lintpassespnpm buildsucceedspnpm test:backendpasses (825/825 tests)