diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..efa1215 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,29 @@ +name: CI + +on: + push: + branches: [main] + pull_request: + +jobs: + verify: + runs-on: ubuntu-latest + + steps: + - name: Check out repository + uses: actions/checkout@v4 + + - name: Set up Node.js + uses: actions/setup-node@v4 + with: + node-version: 20 + cache: npm + + - name: Install dependencies + run: npm ci + + - name: Run tests + run: npm test -- --runInBand --passWithNoTests + + - name: Build client + run: npm run build --workspace client diff --git a/README.md b/README.md index e03fc86..92cfe7b 100644 --- a/README.md +++ b/README.md @@ -47,6 +47,7 @@ PORT=4000 MONGO_URI=mongodb://tasks:pass1234@ac-j3acrgb-shard-00-00.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-01.lueesfz.mongodb.net:27017,ac-j3acrgb-shard-00-02.lueesfz.mongodb.net:27017/?ssl=true&replicaSet=atlas-6to6iy-shard-0&authSource=admin&appName=Cluster0 JWT_SECRET=4a498d90a0f923ae4b3f1f65571ff25985d17e2f3286e8e90383e449f68a40a8 JWT_EXPIRES_IN=7d +CSRF_SECRET=******a-separate-long-random-secret ``` ## What was added on top of Task 3 diff --git a/client/src/api.js b/client/src/api.js index 6de0a0a..be17d6b 100644 --- a/client/src/api.js +++ b/client/src/api.js @@ -7,3 +7,31 @@ export const api = axios.create({ baseURL: 'http://localhost:4000/api', withCredentials: true }) + +let csrfToken +const csrfExemptUrls = ['/auth/login', '/auth/register', '/csrf-token'] + +api.interceptors.request.use(async config => { + const method = (config.method || 'get').toLowerCase() + const url = config.url || '' + if (['get', 'head', 'options'].includes(method) || + csrfExemptUrls.some(path => url.includes(path))) { + return config + } + + if (!csrfToken) { + const { data } = await api.get('/csrf-token') + csrfToken = data.csrfToken + } + + config.headers.set('x-csrf-token', csrfToken) + return config +}) + +api.interceptors.response.use(response => { + const url = response.config.url || '' + if (url.includes('/auth/login') || url.includes('/auth/register') || url.includes('/auth/logout')) { + csrfToken = undefined + } + return response +}) diff --git a/package-lock.json b/package-lock.json index 37d0442..34bdcb2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -101,6 +101,7 @@ "integrity": "sha512-j1nvOQCZYSgp7HLmisnCXFTpVvFODiijQ9QHkORfGKWH6YDtskTEeCJS7eV545GZ9UBzrq0e2xsOd5QQ5PjIqw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@babel/code-frame": "^7.29.7", "@babel/generator": "^7.29.13", @@ -1962,6 +1963,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" }, @@ -1985,6 +1987,7 @@ } ], "license": "MIT", + "peer": true, "engines": { "node": ">=18" } @@ -3158,9 +3161,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3182,9 +3182,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3206,9 +3203,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3230,9 +3224,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3254,9 +3245,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3278,9 +3266,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3482,9 +3467,6 @@ "arm" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3499,9 +3481,6 @@ "arm" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3516,9 +3495,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3533,9 +3509,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3550,9 +3523,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3567,9 +3537,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3584,9 +3551,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3601,9 +3565,6 @@ "ppc64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3618,9 +3579,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3635,9 +3593,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3652,9 +3607,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3669,9 +3621,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -3686,9 +3635,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -3912,6 +3858,7 @@ "integrity": "sha512-yOriVnggzrnQ3a9OKOCxaVuSug3w3/SbOj5i7VwXWZEyUNl3bLF9V3MfxGbZKuwqJOQyRfqXyROBB1CoZLFWzA==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "@types/node": "*", "@types/tough-cookie": "*", @@ -4088,9 +4035,6 @@ "arm64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4105,9 +4049,6 @@ "arm64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4122,9 +4063,6 @@ "loong64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4139,9 +4077,6 @@ "loong64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4156,9 +4091,6 @@ "ppc64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4173,9 +4105,6 @@ "riscv64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4190,9 +4119,6 @@ "riscv64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4207,9 +4133,6 @@ "s390x" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4224,9 +4147,6 @@ "x64" ], "dev": true, - "libc": [ - "glibc" - ], "license": "MIT", "optional": true, "os": [ @@ -4241,9 +4161,6 @@ "x64" ], "dev": true, - "libc": [ - "musl" - ], "license": "MIT", "optional": true, "os": [ @@ -4718,6 +4635,7 @@ "integrity": "sha512-cIjZnSO+y89ykVX9x96OVnA6tGORBn7vGJ/e3PCbBHMEyfMNG/ixzOS0lLa6saMlWcs69bSKjxFyDwjK152OMg==", "dev": true, "license": "Apache-2.0", + "peer": true, "engines": { "bare": ">=1.20.0" } @@ -4728,6 +4646,7 @@ "integrity": "sha512-AIPKioV7/Y/8KfZ3AAhjPJxLLbY49S64Ym5DakZlUg75qQiTgUq9hEJoEwa4eUezPUlXRy/i5NpsKvo9jgKmoA==", "dev": true, "license": "Apache-2.0", + "peer": true, "peerDependencies": { "bare-abort-controller": "*" }, @@ -4971,6 +4890,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "baseline-browser-mapping": "^2.11.33", "caniuse-lite": "^1.0.30001819", @@ -5444,6 +5364,15 @@ "node": ">= 8" } }, + "node_modules/csrf-csrf": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/csrf-csrf/-/csrf-csrf-4.1.0.tgz", + "integrity": "sha512-hqnuSC2OEHXdFA9cbtRD2arM8VfjUGn4gnlsOWOhbUCkhhBS0PJTvGBQN9OCbVCvW6tLsshmjL9yovAP+hJXmg==", + "license": "ISC", + "dependencies": { + "http-errors": "^2.0.0" + } + }, "node_modules/cssstyle": { "version": "4.6.0", "resolved": "https://registry.npmjs.org/cssstyle/-/cssstyle-4.6.0.tgz", @@ -7489,6 +7418,7 @@ "integrity": "sha512-/imKNG4EbWNrVjoNC/1H5/9GFy+tqjGBHCaSsN+P2RnPqjsLmv6UD3Ej+Kj8nBWaRAwyk7kK5ZUc+OEatnTR3A==", "dev": true, "license": "MIT", + "peer": true, "bin": { "jiti": "bin/jiti.js" } @@ -7532,6 +7462,7 @@ "integrity": "sha512-Cvc9WUhxSMEo4McES3P7oK3QaXldCfNWp7pl2NNeiIFlCoLr3kfq9kb1fxftiwk1FLV7CvpvDfonxtzUDeSOPg==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "cssstyle": "^4.2.1", "data-urls": "^5.0.0", @@ -8774,6 +8705,7 @@ } ], "license": "MIT", + "peer": true, "dependencies": { "nanoid": "^3.3.20", "picocolors": "^1.1.1", @@ -9083,6 +9015,7 @@ "resolved": "https://registry.npmjs.org/react/-/react-18.3.1.tgz", "integrity": "sha512-wS+hAgJShR0KhEvPJArfuPVN1+Hz1t0Y6n5jLrGQbkb4urgPE/0Rve+1kMB1v/oWgHgm4WIcV+i7F2pTVj+2iQ==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0" }, @@ -9095,6 +9028,7 @@ "resolved": "https://registry.npmjs.org/react-dom/-/react-dom-18.3.1.tgz", "integrity": "sha512-5m4nQKp+rZRb09LNH59GM4BxTh9251/ylbKIbpe7TpGxfJ+9kv6BLkLBXIjjspbgbnIBNqlI23tRnTWT0snUIw==", "license": "MIT", + "peer": true, "dependencies": { "loose-envify": "^1.1.0", "scheduler": "^0.23.2" @@ -10494,6 +10428,7 @@ "integrity": "sha512-o5a9xKjbtuhY6Bi5S3+HvbRERmouabWbyUcpXXUA1u+GNUKoROi9byOJ8M0nHbHYHkYICiMlqxkg1KkYmm25Sw==", "dev": true, "license": "MIT", + "peer": true, "dependencies": { "esbuild": "^0.21.3", "postcss": "^8.4.43", @@ -10811,6 +10746,7 @@ "bcryptjs": "^2.4.3", "cookie-parser": "^1.4.7", "cors": "^2.8.5", + "csrf-csrf": "^4.1.0", "dotenv": "^16.4.5", "express": "^4.19.2", "joi": "^17.12.3", diff --git a/server/package.json b/server/package.json index db19c7f..a1b4048 100644 --- a/server/package.json +++ b/server/package.json @@ -10,6 +10,7 @@ "bcryptjs": "^2.4.3", "cookie-parser": "^1.4.7", "cors": "^2.8.5", + "csrf-csrf": "^4.1.0", "dotenv": "^16.4.5", "express": "^4.19.2", "joi": "^17.12.3", diff --git a/server/src/app.js b/server/src/app.js index 8920599..0d72054 100644 --- a/server/src/app.js +++ b/server/src/app.js @@ -6,6 +6,8 @@ import authRoutes from './routes/auth.js'; import itemRoutes from './routes/items.js'; import userRoutes from './routes/users.js'; import settingRoutes from './routes/settings.js'; +import { doubleCsrfProtection, generateCsrfToken } from './middleware/csrf.js'; +import { requireAuth } from './middleware/auth.js'; const app = express(); @@ -22,6 +24,10 @@ app.use(cookieParser()); app.get('/api/health', (req, res) => res.json({ ok: true })); app.use('/api/auth', authRoutes); +app.get('/api/csrf-token', requireAuth, (req, res) => { + res.json({ csrfToken: generateCsrfToken(req, res) }); +}); +app.use(doubleCsrfProtection); app.use('/api/items', itemRoutes); app.use('/api/users', userRoutes); app.use('/api/settings', settingRoutes); diff --git a/server/src/controllers/settingController.js b/server/src/controllers/settingController.js index e30276f..e1c4477 100644 --- a/server/src/controllers/settingController.js +++ b/server/src/controllers/settingController.js @@ -21,10 +21,7 @@ export async function getSettings(req, res, next) { } // POST /api/settings/recovery-email — change the recovery email. -// -// This is a state-changing request authenticated ONLY by the auth cookie the -// browser sends automatically. There is no anti-CSRF token, so any page the -// victim visits can forge this request on their behalf. That is the vulnerability. +// CSRF validation is applied by the route middleware before this controller. export async function updateRecoveryEmail(req, res, next) { try { const { value, error } = recoveryEmailSchema.validate({ recoveryEmail: req.body.recoveryEmail }); diff --git a/server/src/middleware/csrf.js b/server/src/middleware/csrf.js new file mode 100644 index 0000000..9b7f882 --- /dev/null +++ b/server/src/middleware/csrf.js @@ -0,0 +1,14 @@ +import { doubleCsrf } from 'csrf-csrf'; + +const csrf = doubleCsrf({ + getSecret: () => process.env.CSRF_SECRET || process.env.JWT_SECRET, + getSessionIdentifier: req => req.cookies?.token || '', + cookieOptions: { + httpOnly: true, + sameSite: 'lax', + secure: false, + path: '/' + } +}); + +export const { generateCsrfToken, doubleCsrfProtection } = csrf; diff --git a/server/src/routes/auth.js b/server/src/routes/auth.js index c80d6a0..eb8d714 100644 --- a/server/src/routes/auth.js +++ b/server/src/routes/auth.js @@ -1,12 +1,13 @@ import { Router } from 'express'; import { register, login, logout, me } from '../controllers/authController.js'; import { requireAuth } from '../middleware/auth.js'; +import { doubleCsrfProtection } from '../middleware/csrf.js'; const router = Router(); router.post('/register', register); router.post('/login', login); -router.post('/logout', logout); +router.post('/logout', requireAuth, doubleCsrfProtection, logout); router.get('/me', requireAuth, me); export default router; diff --git a/server/src/routes/settings.js b/server/src/routes/settings.js index 2e8f894..9a88aa5 100644 --- a/server/src/routes/settings.js +++ b/server/src/routes/settings.js @@ -4,9 +4,8 @@ import { getSettings, updateRecoveryEmail } from '../controllers/settingControll const router = Router(); -// Reading and writing both require a logged-in user. Nothing here checks that -// the request actually originated from our own app — so the write below is -// vulnerable to CSRF. The solution project adds that check with `csrf-csrf`. +// Reading and writing both require a logged-in user. State-changing requests +// are protected by the csrf-csrf middleware mounted in app.js. router.get('/', requireAuth, getSettings); router.post('/recovery-email', requireAuth, updateRecoveryEmail);