Repository navigation
bug: require login, ownership and CSRF token on todo routes (sfind-ff2cbbbc) #1104
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
devin-ai-integration
wants to merge
2
commits into
main
Choose a base branch
from
devin/sfind-ff2cbbbc-todo-routes-auth-ownership
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| const test = require('node:test'); | ||
| const assert = require('node:assert'); | ||
| const mongoose = require('mongoose'); | ||
|
|
||
| const Todo = mongoose.model('Todo', new mongoose.Schema({ content: Buffer, updated_at: Date, owner: String })); | ||
| mongoose.model('User', new mongoose.Schema({ username: String, password: String })); | ||
| const routes = require('../routes'); | ||
|
|
||
| const ALICE_ID = '5f0c3b2a9d1e8a0012345678'; | ||
| let store; | ||
|
|
||
| Todo.findOne = function (query, cb) { | ||
| const doc = store.find((d) => d._id === String(query._id) && d.owner === query.owner) || null; | ||
| setImmediate(cb, null, doc); | ||
| }; | ||
|
|
||
| function makeTodo(owner) { | ||
| const doc = { _id: ALICE_ID, owner, content: 'alice todo' }; | ||
| doc.remove = (cb) => { store = store.filter((d) => d !== doc); setImmediate(cb, null, doc); }; | ||
| doc.save = (cb) => setImmediate(cb, null, doc, 1); | ||
| return doc; | ||
| } | ||
|
|
||
| function call(handler, req) { | ||
| return new Promise((resolve) => { | ||
| const res = { | ||
| locals: {}, | ||
| status(code) { this.statusCode = code; return this; }, | ||
| send() { resolve({ status: this.statusCode }); }, | ||
| redirect(location) { resolve({ status: 302, location }); }, | ||
| }; | ||
| req.get = () => undefined; | ||
| handler(req, res, (err) => resolve({ next: true, err })); | ||
| }); | ||
| } | ||
|
|
||
| const session = (username) => ({ loggedIn: 1, username, csrfToken: 'a'.repeat(64) }); | ||
|
|
||
| test.beforeEach(() => { store = [makeTodo('alice@example.com')]; }); | ||
|
|
||
| test('anonymous requests are redirected by isLoggedIn', async () => { | ||
| const r = await call(routes.isLoggedIn, { session: {} }); | ||
| assert.deepStrictEqual(r, { status: 302, location: '/' }); | ||
| }); | ||
|
|
||
| test('verifyCsrf rejects missing and wrong tokens', async () => { | ||
| assert.strictEqual((await call(routes.verifyCsrf, { session: session('alice@example.com'), body: {} })).status, 403); | ||
| assert.strictEqual((await call(routes.verifyCsrf, { session: session('alice@example.com'), body: { _csrf: 'b'.repeat(64) } })).status, 403); | ||
| assert.strictEqual((await call(routes.verifyCsrf, { session: {}, body: { _csrf: 'x' } })).status, 403); | ||
| }); | ||
|
|
||
| test('verifyCsrf rejects same-length non-ASCII tokens with 403', async () => { | ||
| const r = await call(routes.verifyCsrf, { session: session('alice@example.com'), body: { _csrf: '\u00e9'.repeat(64) } }); | ||
| assert.strictEqual(r.status, 403); | ||
| }); | ||
|
|
||
| test('current_user does not create a CSRF token for anonymous sessions', async () => { | ||
| const anon = {}; | ||
| assert.strictEqual((await call(routes.current_user, { session: anon })).next, true); | ||
| assert.strictEqual(anon.csrfToken, undefined); | ||
| const alice = { loggedIn: 1, username: 'alice@example.com' }; | ||
| await call(routes.current_user, { session: alice }); | ||
| assert.match(alice.csrfToken, /^[0-9a-f]{64}$/); | ||
| }); | ||
|
|
||
| test('verifyCsrf accepts the session token', async () => { | ||
| const r = await call(routes.verifyCsrf, { session: session('alice@example.com'), body: { _csrf: 'a'.repeat(64) } }); | ||
| assert.strictEqual(r.next, true); | ||
| }); | ||
|
|
||
| test('destroy by a different user returns 404 and keeps the todo', async () => { | ||
| const r = await call(routes.destroy, { session: session('bob@example.com'), params: { id: ALICE_ID } }); | ||
| assert.strictEqual(r.status, 404); | ||
| assert.strictEqual(store.length, 1); | ||
| }); | ||
|
|
||
| test('destroy without a session username does not match ownerless lookups', async () => { | ||
| const r = await call(routes.destroy, { session: { loggedIn: 1 }, params: { id: ALICE_ID } }); | ||
| assert.strictEqual(r.status, 404); | ||
| assert.strictEqual(store.length, 1); | ||
| }); | ||
|
|
||
| test('destroy by the owner removes the todo', async () => { | ||
| const r = await call(routes.destroy, { session: session('alice@example.com'), params: { id: ALICE_ID } }); | ||
| assert.deepStrictEqual(r, { status: 302, location: '/' }); | ||
| assert.strictEqual(store.length, 0); | ||
| }); | ||
|
|
||
| test('update by a different user returns 404 and leaves content unchanged', async () => { | ||
| const r = await call(routes.update, { session: session('bob@example.com'), params: { id: ALICE_ID }, body: { content: 'defaced' } }); | ||
| assert.strictEqual(r.status, 404); | ||
| assert.strictEqual(store[0].content, 'alice todo'); | ||
| }); | ||
|
|
||
| test('update by the owner changes content', async () => { | ||
| const r = await call(routes.update, { session: session('alice@example.com'), params: { id: ALICE_ID }, body: { content: 'edited' } }); | ||
| assert.strictEqual(r.status, 302); | ||
| assert.strictEqual(store[0].content, 'edited'); | ||
| }); | ||
|
|
||
| test('malformed ids return 404', async () => { | ||
| const r = await call(routes.destroy, { session: session('alice@example.com'), params: { id: 'not-an-id' } }); | ||
| assert.strictEqual(r.status, 404); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 Existing sessions lose todo controls
After deployment,
currentUsernamerejects existing logged-in sessions without a username.isLoggedInstill accepts them, so todo controls disappear and edits return 404 until the user logs in again.Learn more
Sessions created before this change have
loggedInbut nousername, because login did not previously store it. isLoggedIn accepts these sessions, while the new username check fails. The index then hides the create and ownership controls, and the ownership lookup returns no todo. An API client that submits a valid token to create/import can also persist a todo withowner: null.Example: Alice logs in before deployment and keeps her session cookie. After deployment, she visits
/and sees a login prompt instead of her todo controls./edit/<her-id>returns 404 until she signs in again.Recommended fix: Handle the missing username as an expired session and require reauthentication before allowing todo writes; alternatively migrate sessions safely. Ensure the write routes reject a missing owner rather than saving null.
Was this helpful? React with 👍 or 👎 to provide feedback.