-
-
Notifications
You must be signed in to change notification settings - Fork 3k
security(collab): apply queued USER_CHANGES to the enqueue-time pad (cross-pad write TOCTOU) #8075
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
JohnMcLear
wants to merge
2
commits into
develop
Choose a base branch
from
security/padmsg-toctou-crosspad
base: develop
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.
+125
−9
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| 'use strict'; | ||
|
|
||
| /** | ||
| * Regression for GHSA-6mcx-x5h6-rpw2 (PadMessageHandler cross-pad write TOCTOU). | ||
| * | ||
| * A USER_CHANGES message is authorized (access + read-only gate) against the pad | ||
| * the session points at when the message arrives, but the write ran later off | ||
| * mutable session state. A concurrent same-socket CLIENT_READY could swap | ||
| * sessioninfos[socket.id].padId during the awaits in handleMessage(), redirecting | ||
| * the queued write onto a read-only / unauthorized pad. | ||
| * | ||
| * This test drives a real USER_CHANGES over a socket and, via a | ||
| * handleMessageSecurity hook (which runs during those awaits), swaps the | ||
| * session's padId to a victim pad — exactly the concurrent-CLIENT_READY race. | ||
| * The change must still land on the pad the message was authorized against, and | ||
| * never on the victim. | ||
| */ | ||
|
|
||
| import {MapArrayType} from '../../../node/types/MapType'; | ||
|
|
||
| const assert = require('assert').strict; | ||
| const common = require('../common'); | ||
| const padManager = require('../../../node/db/PadManager'); | ||
| const plugins = require('../../../static/js/pluginfw/plugin_defs'); | ||
| const padMessageHandler = require('../../../node/handler/PadMessageHandler'); | ||
|
|
||
| describe(__filename, function () { | ||
| this.timeout(30000); | ||
| let agent: any; | ||
| const backups: MapArrayType<any> = {}; | ||
|
|
||
| before(async function () { agent = await common.init(); }); | ||
|
|
||
| beforeEach(function () { | ||
| backups.hooks = {handleMessageSecurity: plugins.hooks.handleMessageSecurity}; | ||
| plugins.hooks.handleMessageSecurity = []; | ||
| }); | ||
| afterEach(function () { | ||
| Object.assign(plugins.hooks, backups.hooks); | ||
| }); | ||
|
|
||
| it('a mid-message pad swap cannot redirect the write onto another pad', async function () { | ||
| // Authorized (writable) pad the client is editing. | ||
| const authorizedId = `toctou_ok_${common.randomString()}`; | ||
| const authorizedPad = await padManager.getPad(authorizedId, 'seed\n'); | ||
| await authorizedPad.setText('seed\n'); | ||
| // Victim pad with identical text, so the changeset is valid against either | ||
| // pad — the test distinguishes purely by which pad receives the write. | ||
| const victimId = `toctou_victim_${common.randomString()}`; | ||
| const victimPad = await padManager.getPad(victimId, 'seed\n'); | ||
| await victimPad.setText('seed\n'); | ||
|
|
||
| const res = await agent.get(`/p/${authorizedId}`).expect(200); | ||
| const socket = await common.connect(res); | ||
| try { | ||
| const {data: clientVars} = await common.handshake(socket, authorizedId); | ||
| const rev = clientVars.collab_client_vars.rev; | ||
| const authorId = clientVars.userId; | ||
|
|
||
| // Simulate the concurrent same-socket CLIENT_READY: while THIS USER_CHANGES | ||
| // is mid-flight (handleMessageSecurity runs during handleMessage's awaits), | ||
| // swap the session's padId to the victim pad in place. | ||
| plugins.hooks.handleMessageSecurity = [{ | ||
| hook_fn: (hookName: string, ctx: any) => { | ||
| if (ctx.message?.data?.type === 'USER_CHANGES') { | ||
| padMessageHandler.sessioninfos[ctx.socket.id].padId = victimId; | ||
| } | ||
| }, | ||
| hook_fn_name: 'toctou/handleMessageSecurity', | ||
| hook_name: 'handleMessageSecurity', | ||
| part: {plugin: 'toctou'}, | ||
| }]; | ||
|
|
||
| const accept = common.waitForSocketEvent(socket, 'message'); | ||
| await common.sendUserChanges(socket, { | ||
| baseRev: rev, | ||
| changeset: 'Z:5>2*0+2$ZZ', | ||
| apool: {numToAttrib: {0: ['author', authorId]}, nextNum: 1}, | ||
| }); | ||
| await accept; // wait for the server to finish applying | ||
|
Comment on lines
+74
to
+80
|
||
|
|
||
| assert.equal(victimPad.text(), 'seed\n', | ||
| 'the victim pad (swapped session padId) must NOT be modified'); | ||
| assert.equal(authorizedPad.text(), 'ZZseed\n', | ||
| 'the change must land on the pad the message was authorized against'); | ||
| } finally { | ||
| socket.close(); | ||
| await authorizedPad.remove(); | ||
| await victimPad.remove(); | ||
| } | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.