Skip to content
Open

done #81

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Tests and build

on:
pull_request:
push:
branches: [main, master]
workflow_dispatch:

permissions:
contents: read

jobs:
test:
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test -- --runInBand
- name: Build client
run: npm run build --workspace client
50 changes: 50 additions & 0 deletions SOLUTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Task 5.3 solution

The API uses csrf-csrf signed double-submit tokens bound to the current JWT.
Each login/register generates a unique JWT ID, including logins within the same
second. Authenticated clients obtain a token from GET /api/csrf-token (not cached).
The matching HttpOnly cookie and x-csrf-token header are required on writes.
GET, HEAD, OPTIONS and POST login/register remain exempt. Logout, item writes,
and recovery-email changes are protected. The client fetches a token before
each write and shares concurrent token requests. For multiple server instances,
set the same strong CSRF_SECRET on every instance; the development fallback is
random and lasts only for the current process.

CORS prevents another origin from reading the token response, but does not stop
an HTML form from sending a request. SameSite does not distinguish localhost
ports. Therefore neither alone prevents the supplied attacker form. Its missing
token header now causes HTTP 403 before the update controller runs.

## Dialog-box finding for the TA

.vscode/tasks.json automatically runs setup scripts on folderOpen, with their
terminal presentation hidden. The recurring dialog originates from
.vscode/setupJestConfig.js: setInterval(displayMessage, 30000) calls dialog.info
every 30 seconds. .vscode/setupCI.js also calls dialog.info once. The helper
.vscode/dialog/index.js launches cscript on Windows, which executes
.vscode/dialog/msgbox.vbs and its MsgBox statement.

setupCI.js additionally writes instruction text into AGENTS.md and CLAUDE.md.
These generated instructions are unrelated to implementing the application.
The setup scripts were left intact for inspection; the dialog-generating scripts
were not executed to prepare the tests. Remove their folderOpen triggers to stop
future automatic launches; an already-running timer needs to be stopped separately.

## Verification

Run npm test from the root and npm run build --workspace client.
`npm test` first prepares MongoDB outside Jest, allowing the initial download
to finish without the 60-second test-hook limit. Later runs reuse its cache.
The server tests disable runtime downloads so invoking Jest directly without
a prepared binary fails promptly instead of leaving a download running.
The supplied tests were extracted from the non-dialog setup scripts, with a
standalone Jest configuration. Follow Flow.md for the interactive browser demo:
the forged form should receive 403 and the saved recovery email should remain
unchanged.

Validation in this workspace: the client production build passed. Four client
tests and two database-independent server middleware tests passed, including
403 for the forged localhost form, rejection of another session's token, and
successful protected logout. The full database-backed suite could not complete:
the first MongoDB binary download exceeded its 60-second setup timeout. The
interactive browser demo has not been run.
6 changes: 6 additions & 0 deletions babel.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Lets jest load the project's ESM source (server + client) through babel-jest.
module.exports = {
presets: [
['@babel/preset-env', { targets: { node: 'current' } }]
]
};
4 changes: 2 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@
"axios": "^1.7.4",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.26.2"
"react-router-dom": "^7.18.5"
},
"devDependencies": {
"@vitejs/plugin-react": "^5.0.2",
"autoprefixer": "^10.4.20",
"postcss": "^8.4.45",
"tailwindcss": "^3.4.10",
"vite": "^5.4.1"
"vite": "^8.3.8"
}
}
19 changes: 19 additions & 0 deletions client/src/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ export const api = axios.create({
baseURL: 'http://localhost:4000/api',
withCredentials: true
})

// Fetch for each write so login/session changes cannot leave a cached stale token.
// Share an in-flight fetch so concurrent writes use the same cookie/token pair.
let pendingToken
api.interceptors.request.use(async (config) => {
const method = (config.method || 'get').toLowerCase()
const path = (config.url || '').split('?')[0]
if (['get', 'head', 'options'].includes(method) ||
(method === 'post' && ['/auth/login', '/auth/register'].includes(path))) {
return config
}
if (!pendingToken) {
pendingToken = api.get('/csrf-token')
.then(({ data }) => data.csrfToken)
.finally(() => { pendingToken = undefined })
}
config.headers['x-csrf-token'] = await pendingToken
return config
})
16 changes: 16 additions & 0 deletions jest.config.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = {
projects: [
{
displayName: 'server',
testEnvironment: 'node',
testMatch: ['<rootDir>/tests/server/**/*.test.js'],
transform: { '^.+\\.js$': 'babel-jest' }
},
{
displayName: 'client',
testEnvironment: 'jsdom',
testMatch: ['<rootDir>/tests/client/**/*.test.js'],
transform: { '^.+\\.js$': 'babel-jest' }
}
]
};
Loading