mcpField
C
cors-security-headers

cors-security-headers

github.com/claude-dev-suiteSecurity4644 installsUpdated 2d ago
Repository

About cors-security-headers

cors-security-headers is a reference skill for developers securing web applications. It shows configuration patterns for CORS, security headers, CSRF protection, and secure cookies in Express and Spring Boot.

Key features

  1. 01Express CORS configuration
  2. 02Helmet.js security headers
  3. 03CSRF protection patterns
  4. 04Secure session cookie settings
  5. 05Spring Boot header configuration

Use cases

  1. 01Restricting cross-origin requests to trusted origins
  2. 02Setting content security and transport headers
  3. 03Protecting session-based forms against CSRF
  4. 04Configuring secure cookies for web sessions

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/claude-dev-suite/claude-dev-suite --skill cors-security-headers

After installing, type/cors-security-headersin Claude Code to confirm it loaded.

Details

From the project README

CORS & Security Headers

CORS (Express)

import cors from 'cors';

// Restrictive (recommended)
app.use(cors({
  origin: ['https://myapp.com', 'https://admin.myapp.com'],
  methods: ['GET', 'POST', 'PUT', 'DELETE'],
  allowedHeaders: ['Content-Type', 'Authorization'],
  credentials: true,
  maxAge: 86400, // Preflight cache: 24h
}));

// Dynamic origin
app.use(cors({
  origin: (origin, callback) => {
    const allowed = ALLOWED_ORIGINS.includes(origin!) || !origin; // !origin = same-origin
    callback(null, allowed ? origin : false);
  },
  credentials: true,
}));

Security Headers (Helmet.js)

import helmet from 'helmet';

app.use(helmet({
  contentSecurityPolicy: {
    directives: {
      defaultSrc: ["'self'"],
      scriptSrc: ["'self'", "'unsafe-inline'", 'https://cdn.example.com'],
      styleSrc: ["'self'", "'unsafe-inline'"],
      imgSrc: ["'self'", 'data:', 'https:'],
      connectSrc: ["'self'", 'https://api.example.com'],
      frameSrc: ["'none'"],
      objectSrc: ["'none'"],
    },
  },
  hsts: { maxAge: 31536000, includeSubDomains: true, preload: true },
  referrerPolicy: { policy: 'strict-origin-when-cross-origin' },
}));

CSRF Protection

import csrf from 'csurf';

// For server-rendered forms (session-based apps)
app.use(csrf({ cookie: true }));
app.get('/form', (req, res) => {
  res.render('form', { csrfToken: req.csrfToken() });
});

// For SPAs: use SameSite cookies + custom header
// No csrf library needed — rely on:
// 1. SameSite=Strict/Lax cookies
// 2. Check Origin/Referer header matches
// 3. Custom header requirement (X-Requested-With)

Secure Cookies

app.use(session({
  cookie: {
    httpOnly: true,        // No JS access
    secure: true,          // HTTPS only
    sameSite: 'lax',       // CSRF protection
    maxAge: 24 * 60 * 60 * 1000,
    domain: '.myapp.com',  // Shared across subdomains
  },
}));

Spring Boot Security Headers

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    return http
        .cors(cors -> cors.configurationSource(corsConfig()))
        .headers(headers -> headers
            .contentSecurityPolicy(csp -> csp.policyDirectives("default-src 'self'"))
            .referrerPolicy(ref -> ref.policy(ReferrerPolicyHeaderWriter.ReferrerPolicy.STRICT_ORIGIN))
            .frameOptions(frame -> frame.deny())
        )
        .csrf(csrf -> csrf.csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()))
        .build();
}

@Bean
CorsConfigurationSource corsConfig() {
    var config = new CorsConfiguration();
    config.setAllowedOrigins(List.of("https://myapp.com"));
    config.setAllowedMethods(List.of("GET", "POST", "PUT", "DELETE"));
    config.setAllowCredentials(true);
    var source = new UrlBasedCorsConfigurationSource();
    source.registerCorsConfiguration("/**", config);
    return source;
}

Key Headers Reference

HeaderPurposeRecommended Value
Strict-Transport-SecurityForce HTTPSmax-age=31536000; includeSubDomains
Content-Security-PolicyRestrict resource loadingdefault-src 'self' + specifics
X-Content-Type-OptionsPrevent MIME sniffingnosniff
X-Frame-OptionsPrevent clickjackingDENY
Referrer-PolicyControl referrer infostrict-origin-when-cross-origin
Permissions-PolicyRestrict browser featurescamera=(), microphone=()

Anti-Patterns

Anti-PatternFix
Access-Control-Allow-Origin: * with credentialsSpecify exact origins
No CSP headerAdd Content-Security-Policy
CSRF token in URL query paramsUse header or hidden form field
SameSite=None without SecureAlways pair SameSite=None with Secure
Missing HSTSEnable with long max-age and preload

Production Checklist

  • CORS: specific origins, no wildcard with credentials
  • Helmet.js (or equivalent) for all security headers
  • CSP configured and tested
  • HSTS with preload
  • Secure, HttpOnly, SameSite cookies
  • CSRF protection for state-changing requests

Comments

Markdown supported