Fix ZAP scan security vulnerabilities with proper CSP and security headers
This PR addresses multiple security vulnerabilities identified in the ZAP baseline security scan for minespace-test.apps.silver.devops.gov.bc.ca.
Issues Fixed
The ZAP scan identified several critical security issues:
- Content Security Policy (CSP) failures - Missing CSP directives for robots.txt and sitemap.xml endpoints
- Insufficient site isolation - Missing Cross-Origin headers to prevent Spectre-class vulnerabilities
- Improper cache control - Missing proper cache directives for static endpoints
- Missing security headers - Lack of X-Content-Type-Options and other protective headers
Changes Made
Added Explicit Security Endpoints
-
robots.txt endpoint with proper
text/plainContent-Type and security headers -
sitemap.xml endpoint with proper
application/xmlContent-Type and security headers - Trailing slash redirects (301) for both endpoints to handle URL variations
Enhanced Helmet Security Configuration
- Default CSP policy when CONTENT_SECURITY_POLICY environment variable is not set
- Cross-Origin-Embedder-Policy: require-corp for Spectre vulnerability mitigation
- Cross-Origin-Opener-Policy: same-origin for process isolation
- Cross-Origin-Resource-Policy: same-origin for resource protection
Security Headers Middleware
Added comprehensive security headers to all responses:
-
X-Content-Type-Options: nosniffto prevent MIME sniffing attacks -
Referrer-Policy: strict-origin-when-cross-originfor privacy protection - Enhanced CORS headers for better isolation
Example
Before this fix, accessing /robots.txt would fall through to the static file handler without proper security headers:
GET /robots.txt
Response: 404 or fallback HTML with missing security headers
After this fix:
GET /robots.txt
Content-Type: text/plain
Cache-Control: public, max-age=86400
Cross-Origin-Resource-Policy: same-origin
X-Content-Type-Options: nosniff
User-agent: *
Disallow: /
Impact
- Backward compatibility maintained - All existing endpoints continue to work unchanged
- Minimal code changes - Only 2 files modified with surgical precision
- Comprehensive security - Addresses all ZAP scan vulnerabilities
- Performance neutral - No impact on existing functionality
The changes are applied to both minespace-web and core-web servers to ensure consistent security across all applications.
Fixes #3651.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
@simensma-fresh 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.
I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.