Skip to content
Open
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ A change is ready to merge when:
- [ ] New behaviour has a test (where practical)
- [ ] Docs/README updated if the public API or props changed
- [ ] UI changes include a screenshot
- [ ] Security checks updated and validated with audit test suite

---

Expand Down
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ function App() {
}
```

### Security Posture Auditing (`audit`)

Dimsee provides an automated configuration auditing function (`audit`) to evaluate your authentication setup:

```javascript
const { audit, createAuthBackend } = require("dimsee/backend");

const app = createAuthBackend({ /* ... */ });
const report = audit(app);

if (!report.passed) {
console.error("Security posture findings:", report.findings);
}
```

- **JWT Secrets Audit**: Checks that `jwtSecret` (for access tokens) and `jwtRefreshSecret` (for refresh tokens) are distinct, configured, and not left at default placeholders.


## Framework Guides

Learn how to integrate Dimsee with popular React frameworks.
Expand Down
5 changes: 5 additions & 0 deletions SECURITY.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ Dimsee aims to give a MERN app a secure-by-default auth baseline:
- **Brute force:** rate limiting on the auth endpoints.
- **Transport:** designed to run behind HTTPS in production.

## Automated Security Posture Checks

Use `audit()` to automatically detect configuration drift or weak secrets:
- Access and refresh token secrets must be distinct and non-default.

## Known limitations & your responsibilities

Dimsee is a building block, not a turnkey security guarantee. You are responsible
Expand Down
65 changes: 65 additions & 0 deletions __tests__/auditJwtSecrets.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const { checkJwtSecrets } = require('../backend/audit/checks/jwtSecrets');
const { audit } = require('../backend/audit');

describe('audit check: distinct access and refresh JWT secrets (Issue #73)', () => {
test('flags when access and refresh secrets are identical', () => {
const config = {
jwtSecret: 'super-secret-key-12345',
jwtRefreshSecret: 'super-secret-key-12345'
};

const findings = checkJwtSecrets(config, { env: 'development' });
const identicalFinding = findings.find(f => f.id === 'jwt-secrets-identical');

expect(identicalFinding).toBeDefined();
expect(identicalFinding.severity).toBe('warn');
expect(identicalFinding.message).toContain('share the same secret key');
});

test('escalates to error severity in production environment', () => {
const config = {
jwtSecret: 'super-secret-key-12345',
jwtRefreshSecret: 'super-secret-key-12345'
};

const findings = checkJwtSecrets(config, { env: 'production' });
const identicalFinding = findings.find(f => f.id === 'jwt-secrets-identical');

expect(identicalFinding.severity).toBe('error');
});

test('flags default/placeholder secrets', () => {
const config = {
jwtSecret: 'your-secret-key-change-in-production',
jwtRefreshSecret: 'different-custom-secret'
};

const findings = checkJwtSecrets(config, { env: 'production' });
const defaultFinding = findings.find(f => f.id === 'jwt-secret-default');

expect(defaultFinding).toBeDefined();
expect(defaultFinding.severity).toBe('error');
expect(defaultFinding.remediation).toBeDefined();
});

test('flags missing secrets', () => {
const findings = checkJwtSecrets({}, { env: 'production' });

expect(findings.some(f => f.id === 'jwt-secret-missing')).toBe(true);
expect(findings.some(f => f.id === 'jwt-refresh-secret-missing')).toBe(true);
});

test('passes cleanly when distinct, non-default secrets are provided', () => {
const config = {
jwtSecret: 'random-access-secret-32-chars-long-abc',
jwtRefreshSecret: 'random-refresh-secret-32-chars-long-xyz'
};

const findings = checkJwtSecrets(config, { env: 'production' });
expect(findings.length).toBe(0);

const report = audit(config, { env: 'production' });
expect(report.passed).toBe(true);
expect(report.counts.error).toBe(0);
});
});
90 changes: 90 additions & 0 deletions backend/audit/checks/jwtSecrets.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// backend/audit/checks/jwtSecrets.js
'use strict';

const KNOWN_INSECURE_SECRETS = new Set([
'your-secret-key-change-in-production',
'your-jwt-secret-key-change-in-production',
'your-jwt-refresh-secret-key-change-in-production',
'your-secret-key',
'your-jwt-secret',
'secret',
'jwtsecret',
'changeme',
'123456'
]);

/**
* Check: distinct access and refresh JWT secrets (Issue #73).
* Flags when jwtSecret and jwtRefreshSecret are equal or left at their defaults.
*/
function checkJwtSecrets(config = {}, context = {}) {
const findings = [];
const jwtSecret = config.jwtSecret || process.env.JWT_SECRET;
const jwtRefreshSecret = config.jwtRefreshSecret || process.env.JWT_REFRESH_SECRET;
const isProduction = (context.env === 'production' || process.env.NODE_ENV === 'production');

// Check 1: Missing secrets
if (!jwtSecret) {
findings.push({
id: 'jwt-secret-missing',
rule: 'distinct-jwt-secrets',
severity: isProduction ? 'error' : 'warn',
title: 'Missing access token JWT secret',
message: 'jwtSecret is not configured. A high-entropy random secret must be provided.',
remediation: 'Provide a strong random string via process.env.JWT_SECRET or config.jwtSecret.'
});
}

if (!jwtRefreshSecret) {
findings.push({
id: 'jwt-refresh-secret-missing',
rule: 'distinct-jwt-secrets',
severity: isProduction ? 'error' : 'warn',
title: 'Missing refresh token JWT secret',
message: 'jwtRefreshSecret is not configured. Separate secrets must be used for access and refresh tokens.',
remediation: 'Provide a strong random string via process.env.JWT_REFRESH_SECRET or config.jwtRefreshSecret.'
});
}

// Check 2: Default or placeholder secrets
if (jwtSecret && KNOWN_INSECURE_SECRETS.has(jwtSecret.trim().toLowerCase())) {
findings.push({
id: 'jwt-secret-default',
rule: 'distinct-jwt-secrets',
severity: isProduction ? 'error' : 'warn',
title: 'Default/insecure JWT secret in use',
message: `jwtSecret is using a known default placeholder ("${jwtSecret}").`,
remediation: 'Generate a high-entropy secret (e.g. node -e "console.log(crypto.randomBytes(32).toString(\'hex\'))").'
});
}

if (jwtRefreshSecret && KNOWN_INSECURE_SECRETS.has(jwtRefreshSecret.trim().toLowerCase())) {
findings.push({
id: 'jwt-refresh-secret-default',
rule: 'distinct-jwt-secrets',
severity: isProduction ? 'error' : 'warn',
title: 'Default/insecure JWT refresh secret in use',
message: `jwtRefreshSecret is using a known default placeholder ("${jwtRefreshSecret}").`,
remediation: 'Generate a distinct high-entropy secret for refresh tokens.'
});
}

// Check 3: Reused / identical access and refresh secrets
if (jwtSecret && jwtRefreshSecret && jwtSecret === jwtRefreshSecret) {
findings.push({
id: 'jwt-secrets-identical',
rule: 'distinct-jwt-secrets',
severity: isProduction ? 'error' : 'warn',
title: 'Access and refresh token JWT secrets are identical',
message: 'jwtSecret and jwtRefreshSecret share the same secret key. Compromising one compromises both token types.',
remediation: 'Use distinct, independent secret keys for access tokens and refresh tokens.'
});
}

return findings;
}

module.exports = {
checkJwtSecrets,
KNOWN_INSECURE_SECRETS
};
59 changes: 59 additions & 0 deletions backend/audit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// backend/audit/index.js
'use strict';

const { checkJwtSecrets } = require('./checks/jwtSecrets');

/**
* Registry of available audit checks.
*/
const CHECKS = [
checkJwtSecrets
];

/**
* Runs security audit on a Dimsee configuration or running app instance.
* @param {Object} input - Dimsee configuration object or Express app with app.locals.authConfig
* @param {Object} [options] - Audit options
* @returns {Object} Audit report containing findings and summary
*/
function audit(input = {}, options = {}) {
// Support passing app instance or raw config
let config = input;
if (input && input.locals && input.locals.authConfig) {
config = input.locals.authConfig;
}

const env = options.env || process.env.NODE_ENV || 'development';
const findings = [];

for (const check of CHECKS) {
const checkResult = check(config, { env, ...options });
if (checkResult) {
if (Array.isArray(checkResult)) {
findings.push(...checkResult);
} else {
findings.push(checkResult);
}
}
}

const passed = findings.every(f => f.severity !== 'error');
const counts = {
error: findings.filter(f => f.severity === 'error').length,
warn: findings.filter(f => f.severity === 'warn').length,
info: findings.filter(f => f.severity === 'info').length
};

return {
passed,
counts,
findings,
timestamp: new Date().toISOString()
};
}

module.exports = {
audit,
CHECKS,
checkJwtSecrets
};
4 changes: 3 additions & 1 deletion backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,6 @@ const createAuthBackend = (config = {}) => {
return app;
};

module.exports = { createAuthBackend, authMiddleware, User };
const { audit } = require("./audit");

module.exports = { createAuthBackend, authMiddleware, User, audit };
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const backend = require('./backend');

// Backend exports
const { createAuthBackend, authMiddleware, User } = backend;
const { createAuthBackend, authMiddleware, User, audit } = backend;

// Frontend exports
const AuthStatus = require('./frontend/src/components/AuthStatus');
Expand All @@ -16,6 +16,7 @@ module.exports = {
createAuthBackend,
authMiddleware,
User,
audit,
// Frontend
AuthStatus,
useAuth,
Expand Down
22 changes: 18 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,22 @@
},
"jest": {
"transform": {
"^.+\\.jsx?$": ["babel-jest", {
"presets": [["@babel/preset-env", { "targets": { "node": "current" } }], "@babel/preset-react"]
}]
"^.+\\.jsx?$": [
"babel-jest",
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-react"
]
}
]
},
"testPathIgnorePatterns": [
"/node_modules/",
Expand All @@ -62,7 +75,8 @@
"google-auth",
"github-auth",
"session",
"jwt"
"jwt",
"security-audit"
],
"author": "Siddharth <sidd190@gmail.com>",
"license": "MIT",
Expand Down