Claude skill API versioning security — SA-SKL-APIVERSION
Maintaining multiple API versions simultaneously creates security debt: every security improvement applied to the current version must be explicitly backported to all deprecated versions still in service, or the deprecated versions remain at the security level of the original launch. MCP skills with both /api/v1/ and /api/v2/ routes frequently have security improvements applied to the v2 router and forgotten on v1 — authentication middleware, rate limiting, error handling, and CORS policy are the four most common gaps. SA-SKL-APIVERSION-001 through APIVERSION-004 cover each gap with the structural cause, the vulnerable pattern, and the architectural fix that eliminates the per-version inconsistency entirely.
The root cause: per-version middleware
All four SA-SKL-APIVERSION patterns share the same root cause: security middleware is applied at the version-router level rather than at the base /api/ router level. When middleware is applied per-version, every future security change requires a human to remember to apply it to all versions. This is a process guarantee, not a code guarantee — it fails eventually. The correct architecture applies security-critical middleware at the highest possible level: at the app level for middleware that applies to all routes, or at the /api/ base router level for middleware that applies to all API endpoints regardless of version. Per-version middleware should be reserved for functionality that genuinely differs between versions — not for security controls that should be universal.
SA-SKL-APIVERSION in scope: APIVERSION-001 (authentication middleware only on v2 router), APIVERSION-002 (rate limiting only on v2 router), APIVERSION-003 (verbose error handler only on v2 router or placed after version-specific handlers), APIVERSION-004 (CORS configuration per-version with different allowlists). All four are detected by SkillAudit's static analysis of Express router mounting patterns and middleware attachment points.
APIVERSION-001 — Authentication middleware missing from v1 router
When a team adds authentication to an API — typically when moving from an open beta to a paid product — they mount the authMiddleware on the new v2 router. The v1 router, originally designed for unauthenticated access, is left without the middleware because "it's being deprecated anyway." But deprecation without deletion means the v1 endpoints remain live and unauthenticated for as long as the router is mounted. An attacker who reads the changelog, diffs the git history, or fuzzes version prefixes will find the unauthenticated v1 endpoint and use it to bypass authentication entirely for any resource that v1 exposes.
// VULNERABLE: authMiddleware applied to v2 router only
const v1Router = require('./routes/v1'); // No auth
const v2Router = require('./routes/v2');
app.use('/api/v2', authMiddleware, v2Router); // ← Auth on v2
app.use('/api/v1', v1Router); // ← VULNERABLE: v1 has no auth
// routes/v1/users.js — returns user data without auth check
router.get('/users/:id', async (req, res) => {
const user = await db.users.findById(req.params.id);
res.json(user); // ← Accessible without authentication via /api/v1/users/:id
});
// FIXED: authMiddleware applied at the base /api/ router level — all versions covered
const apiRouter = express.Router();
const v1Router = require('./routes/v1');
const v2Router = require('./routes/v2');
// FIXED: auth at the base router — applies to ALL versions automatically
apiRouter.use(authMiddleware);
apiRouter.use('/v1', v1Router);
apiRouter.use('/v2', v2Router);
app.use('/api', apiRouter);
// Any future /api/v3 added here also gets authMiddleware automatically
authMiddleware, verifyToken, or equivalent) on the /api/v2 router but not on the /api/v1 router; deprecated v1 endpoints remain fully unauthenticated and return user data, trigger side effects, or expose admin functionality to unauthenticated requests; an attacker who discovers the v1 prefix bypasses authentication for all resources v1 exposes. Fix: apply authMiddleware at the /api base router level, not per-version router; ensure any new version router mounted under /api inherits the base router's middleware automatically. Detection: SkillAudit flags app.use('/api/v2', authMiddleware, v2Router) paired with app.use('/api/v1', v1Router) without authMiddleware, and similar patterns.APIVERSION-002 — Rate limiting missing from deprecated v1 router
Rate limiting applied at the per-version router level creates a bypass path: the deprecated v1 router, which has no rate limit middleware, accepts unlimited requests. An attacker can use the v1 router for credential stuffing, API abuse, or resource exhaustion. The practical impact is higher than it sounds: many MCP skills have v1 endpoints for backward compatibility with clients that have not updated their integrations — these clients may pass API keys, and an attacker with a list of leaked API keys can validate them at high speed against the unprotected v1 endpoint. The fix is the same as APIVERSION-001: apply rate limiting at the /api/ base router level.
// VULNERABLE: rate limiting applied to v2 router only
const rateLimit = require('express-rate-limit');
const apiLimiter = rateLimit({ windowMs: 60000, max: 100 });
app.use('/api/v2', apiLimiter, v2Router); // ← Rate limited
app.use('/api/v1', v1Router); // ← VULNERABLE: unlimited requests
// Attacker uses /api/v1/auth/token for unlimited credential stuffing
// or /api/v1/scan for unlimited free scans bypassing the paid tier
// FIXED: rate limiting at the base /api/ router
const apiRouter = express.Router();
const rateLimit = require('express-rate-limit');
const apiLimiter = rateLimit({
windowMs: 60 * 1000, // 1 minute
max: 100,
standardHeaders: true,
legacyHeaders: false,
message: { error: 'Too many requests, please try again later.' },
});
// FIXED: rate limit applied before version routing — all versions covered
apiRouter.use(apiLimiter);
apiRouter.use(authMiddleware);
apiRouter.use('/v1', v1Router);
apiRouter.use('/v2', v2Router);
app.use('/api', apiRouter);
/api/v2 router but not to the /api/v1 router; the deprecated v1 router accepts unlimited requests per time window, enabling credential stuffing against v1 auth endpoints, unlimited free-tier API calls via v1 bypass, or resource exhaustion against unprotected v1 operations. Fix: apply rate limiting at the /api base router level before version routing, so all current and future version routers inherit the same rate limit without per-version configuration. Detection: SkillAudit flags express-rate-limit or equivalent rate limit middleware attached to a version-specific router (e.g., app.use('/api/v2', rateLimit, ...)) without equivalent middleware on other version routers.APIVERSION-003 — Verbose error details in v1 500 responses
Error handler middleware in Express is applied in registration order: the first matching error handler catches a thrown error and can pass it to the next handler with next(err) or return a response. When a v2-era refactor adds a hardened global error handler that strips stack traces from 500 responses, it is typically placed after the application routes in the middleware chain. If version-specific error handling was added to the v1 router earlier in the chain — or if the v1 router's error propagation reaches a default Express error handler before the hardened global one — v1 endpoints still return verbose error objects including file paths, stack traces, and database error messages. These details help an attacker understand the internal architecture and find exploitable code paths.
// VULNERABLE: v1-era verbose error handler registered before global hardened handler
// routes/v1/index.js (registered early)
v1Router.use((err, req, res, next) => {
console.error(err); // Log full error
// VULNERABLE: returns full error details to client
res.status(500).json({
error: err.message,
stack: err.stack, // ← File paths, line numbers
details: err.toString(), // ← Full error chain
});
});
// app.js — hardened handler registered AFTER, never reached for v1 errors
app.use('/api/v1', v1Router); // v1 router has its own verbose handler
app.use('/api/v2', v2Router);
app.use((err, req, res, next) => {
res.status(500).json({ error: 'Internal server error' }); // ← Never reached for v1
});
// FIXED: single global error handler registered after all routes — no per-version handlers
// routes/v1/index.js — no error handler; errors propagate to global handler
// routes/v2/index.js — no error handler; errors propagate to global handler
// app.js
app.use('/api', apiRouter); // apiRouter mounts v1 and v2 without error handlers
// FIXED: global error handler AFTER all routes — catches errors from all versions
app.use((err, req, res, next) => {
// Log internally (full detail)
console.error({ message: err.message, stack: err.stack, url: req.url });
// FIXED: return sanitized error to client — no stack, no internal paths
const statusCode = err.statusCode || err.status || 500;
res.status(statusCode).json({
error: statusCode < 500 ? err.message : 'Internal server error',
});
});
err.stack, err.message in raw form, or other verbose error details in HTTP 500 responses; v1 error responses reveal internal file paths, stack traces, database error messages, and dependency version strings; an attacker who triggers an error on a v1 endpoint receives a detailed map of the application's internal architecture. Fix: remove per-version error handlers entirely; use a single global error handler registered after all routes that logs the full error internally but returns only a sanitized { error: 'Internal server error' } response to clients. Detection: SkillAudit flags version-specific Express error middleware ((err, req, res, next) signature) that includes err.stack or err.message in the response body.APIVERSION-004 — Permissive CORS on deprecated v1 endpoints
CORS configuration controls which origins can make cross-origin requests to the API. When a v2-era security review removes retired domains from the CORS allowlist — perhaps a previous partner integration, a retired marketing subdomain, or a decommissioned frontend — the allowlist update is applied to the v2 CORS configuration only. The v1 CORS configuration retains the original permissive allowlist including origins the application no longer controls. If any of those retired origins are now taken over by a third party (subdomain takeover, expired domain registration), that third party can make authenticated cross-origin requests to the v1 API on behalf of any user whose browser sends cookies to those origins. The fix is a single CORS configuration applied at the base /api/ router level.
// VULNERABLE: per-version CORS — v1 retains legacy permissive allowlist
const corsV1 = cors({
origin: [
'https://skillaudit.dev',
'https://old-beta.skillaudit.io', // ← Retired domain — no longer controlled
'https://partner-app.example.com', // ← Retired partner integration
'https://staging.skillaudit.dev', // ← May be claimable via subdomain takeover
],
credentials: true,
});
const corsV2 = cors({
origin: ['https://skillaudit.dev'], // ← Hardened: only production origin
credentials: true,
});
app.use('/api/v1', corsV1, v1Router); // ← Permissive legacy CORS
app.use('/api/v2', corsV2, v2Router); // ← Hardened CORS
// FIXED: single CORS configuration at the base /api/ router
const allowedOrigins = [
'https://skillaudit.dev',
// Add staging only via environment variable — never hardcode it
...(process.env.CORS_EXTRA_ORIGINS ? process.env.CORS_EXTRA_ORIGINS.split(',') : []),
];
const apiCors = cors({
origin: (origin, callback) => {
// Allow server-to-server requests (no Origin header) only in test environments
if (!origin && process.env.NODE_ENV !== 'production') return callback(null, true);
if (!origin || allowedOrigins.includes(origin)) return callback(null, true);
callback(new Error(`CORS: origin ${origin} not allowed`));
},
credentials: true,
});
// FIXED: single CORS config at base router — all versions use the same policy
apiRouter.use(apiCors);
apiRouter.use(authMiddleware);
apiRouter.use('/v1', v1Router);
apiRouter.use('/v2', v2Router);
app.use('/api', apiRouter);
/api/v1 and /api/v2 routers with different origin allowlists; the v1 allowlist includes retired domains (old partner integrations, decommissioned frontends, staging subdomains) that are no longer controlled by the application; if any retired origin is claimable via subdomain takeover or expired domain registration, the new owner can make credentialed cross-origin requests to v1 endpoints on behalf of any user. Fix: use a single CORS configuration applied at the base /api/ router level with the current minimal allowlist; never configure CORS per-version; manage environment-specific origins via environment variables rather than hardcoded arrays. Detection: SkillAudit flags multiple cors() middleware instances with different origin arrays applied to different version routers of the same application.SkillAudit detection summary — SA-SKL-APIVERSION
| Pattern ID | Severity | What SkillAudit flags | Fix |
|---|---|---|---|
| APIVERSION-001 | CRITICAL | authMiddleware on /api/v2 router without equivalent on /api/v1 router | Apply authMiddleware at /api base router level before version routing |
| APIVERSION-002 | HIGH | Rate limit middleware on version-specific router without coverage of all version routers | Apply rate limiting at /api base router level before version routing |
| APIVERSION-003 | HIGH | Version-specific Express error middleware ((err, req, res, next)) including err.stack or err.message in response body | Single global error handler after all routes; sanitize response to { error: 'Internal server error' } |
| APIVERSION-004 | MEDIUM | Multiple cors() instances with different origin arrays on different version routers | Single CORS config at base /api/ router; manage extra origins via env variable |
Related patterns: For subdomain takeover enabling CORS origin claim on retired domains, see SA-SKL-SUBDOM. For Content Security Policy bypass patterns, see SA-SKL-CSPVIO. For rate limit implementation gaps beyond versioning, see SA-SKL-RATELIMIT2.