diff --git a/src/node/db/AuthorManager.ts b/src/node/db/AuthorManager.ts index 57270adbe44..4783a35e54b 100644 --- a/src/node/db/AuthorManager.ts +++ b/src/node/db/AuthorManager.ts @@ -24,6 +24,32 @@ const CustomError = require('../utils/customError'); const hooks = require('../../static/js/pluginfw/hooks'); import padutils, {randomString} from "../../static/js/pad_utils"; +/** + * Maximum length (in Unicode code points) of an author display name. Names come from + * unauthenticated socket.io messages and are rebroadcast to every client that opens a pad, so + * they must be bounded by the application rather than by the transport's packet size limit + * (GHSA-w4w9-j4c3-hjf6). The pad's name input uses the same value as its maxlength. + */ +const MAX_AUTHOR_NAME_LENGTH = 128; +exports.MAX_AUTHOR_NAME_LENGTH = MAX_AUTHOR_NAME_LENGTH; + +/** + * Normalizes an author display name: non-strings and blank strings become null, everything else + * is trimmed and truncated to MAX_AUTHOR_NAME_LENGTH code points. Safe to call on values read + * back from the database, so records stored before the cap existed are not rebroadcast at their + * full size. + */ +const sanitizeAuthorName = (name: unknown): string | null => { + if (typeof name !== 'string') return null; + // Slice by UTF-16 units first so Array.from() never walks a huge string, then cut on code + // points so a surrogate pair is never split. + let s = name.trimStart().slice(0, MAX_AUTHOR_NAME_LENGTH * 2 + 2).trim(); + const cps = Array.from(s); + if (cps.length > MAX_AUTHOR_NAME_LENGTH) s = cps.slice(0, MAX_AUTHOR_NAME_LENGTH).join('').trim(); + return s === '' ? null : s; +}; +exports.sanitizeAuthorName = sanitizeAuthorName; + exports.getColorPalette = () => [ '#ffc7c7', '#fff1c7', @@ -207,7 +233,7 @@ exports.createAuthor = async (name: string) => { const now = Date.now(); const authorObj = { colorId: Math.floor(Math.random() * (exports.getColorPalette().length)), - name, + name: sanitizeAuthorName(name), timestamp: now, lastSeen: now, }; @@ -241,15 +267,16 @@ exports.setAuthorColorId = async (author: string, colorId: string) => { * Returns the name of the author * @param {String} author The id of the author */ -exports.getAuthorName = async (author: string) => await db.getSub(`globalAuthor:${author}`, ['name']); +exports.getAuthorName = async (author: string) => + sanitizeAuthorName(await db.getSub(`globalAuthor:${author}`, ['name'])); /** * Sets the name of the author * @param {String} author The id of the author * @param {String} name The name of the author */ -exports.setAuthorName = async (author: string, name: string) => { - await db.setSub(`globalAuthor:${author}`, ['name'], name); +exports.setAuthorName = async (author: string, name: unknown) => { + await db.setSub(`globalAuthor:${author}`, ['name'], sanitizeAuthorName(name)); await db.setSub(`globalAuthor:${author}`, ['lastSeen'], Date.now()); }; @@ -511,9 +538,12 @@ exports.searchAuthors = async (query: { if (erased && !query.includeErased) continue; const authorID = key.substring('globalAuthor:'.length); const mappers = mapperByAuthor.get(authorID) || []; + // Records written before names were type-checked (GHSA-w4w9) can hold a non-string name; + // treat those as unnamed instead of letting one bad record break the whole admin search. + const name = typeof rec.name === 'string' ? rec.name : null; if (pattern) { const nameMatch = - (rec.name || '').toLowerCase().includes(pattern); + (name || '').toLowerCase().includes(pattern); const mapperMatch = mappers.some((m) => m.toLowerCase().includes(pattern)); const idMatch = @@ -522,7 +552,7 @@ exports.searchAuthors = async (query: { } rows.push({ authorID, - name: rec.name ?? null, + name, colorId: rec.colorId ?? null, mapper: mappers, // Prefer lastSeen; fall back to timestamp for legacy records that diff --git a/src/node/handler/PadMessageHandler.ts b/src/node/handler/PadMessageHandler.ts index bfdf81b71c4..e71be429efd 100644 --- a/src/node/handler/PadMessageHandler.ts +++ b/src/node/handler/PadMessageHandler.ts @@ -842,8 +842,10 @@ const handleGetChatMessages = async ( */ const handleSuggestUserName = ( socket:any, message: ClientSuggestUserName, ctx: MessageContext) => { - const {newName, unnamedId} = message.data.payload; + const {unnamedId} = message.data.payload; + const newName = authorManager.sanitizeAuthorName(message.data.payload.newName); if (newName == null) throw new Error('missing newName'); + message.data.payload.newName = newName; if (unnamedId == null) throw new Error('missing unnamedId'); const padId = ctx.padId; // search the author and send him this message @@ -864,7 +866,8 @@ const handleSuggestUserName = ( const handleUserInfoUpdate = async ( socket:any, {data: {userInfo: {name, colorId}}}: UserNewInfoMessage, ctx: MessageContext) => { if (colorId == null) throw new Error('missing colorId'); - if (!name) name = null; + // Unauthenticated input that is stored and rebroadcast: type-check and cap it (GHSA-w4w9). + name = authorManager.sanitizeAuthorName(name); if (!ctx.author || !ctx.padId) throw new Error('session not ready'); const author = ctx.author; if (!/(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(colorId)) { @@ -1220,6 +1223,8 @@ const handleClientReady = async ( await hooks.aCallAll('clientReady', message); // Deprecated due to awkward context. let {colorId: authorColorId, name: authorName} = message.userInfo || {}; + // Unauthenticated input that is stored and rebroadcast: type-check and cap it (GHSA-w4w9). + authorName = authorManager.sanitizeAuthorName(authorName); if (authorColorId && !/^#(?:[0-9A-F]{3}){1,2}$/i.test(authorColorId as string)) { messageLogger.warn(`Ignoring invalid colorId in CLIENT_READY message: ${authorColorId}`); // @ts-ignore @@ -1230,6 +1235,8 @@ const handleClientReady = async ( authorColorId && authorManager.setAuthorColorId(sessionInfo.author, authorColorId), ]); ({colorId: authorColorId, name: authorName} = await authorManager.getAuthor(sessionInfo.author)); + // The stored record may predate the name cap; never send an oversized name back out. + authorName = authorManager.sanitizeAuthorName(authorName); // Pad retention: an overdue pad is deleted here, before it loads, so the // client gets a new pad rather than content that should already be gone. @@ -1267,7 +1274,12 @@ const handleClientReady = async ( 'This is possibly related to https://github.com/ether/etherpad-lite/issues/2802'); } else { // Filter author attribs (e.g. don't send author's pads to all clients) - historicalAuthorData[authorId] = {name: author.name, colorId: author.colorId}; + // Re-sanitize on read so records stored before the name cap are not rebroadcast at full + // size to every client that opens the pad (GHSA-w4w9). + historicalAuthorData[authorId] = { + name: authorManager.sanitizeAuthorName(author.name), + colorId: author.colorId, + }; } })); @@ -1614,7 +1626,7 @@ const handleClientReady = async ( type: 'USER_NEWINFO', userInfo: { colorId: authorInfo.colorId, - name: authorInfo.name, + name: authorManager.sanitizeAuthorName(authorInfo.name), userId: authorId, }, }, diff --git a/src/static/js/pad_userlist.ts b/src/static/js/pad_userlist.ts index 7366348a1f0..c226a1fbf5c 100644 --- a/src/static/js/pad_userlist.ts +++ b/src/static/js/pad_userlist.ts @@ -142,6 +142,7 @@ const paduserlist = (() => { name = $('') .attr('data-l10n-id', 'pad.userlist.unnamed') .attr('type', 'text') + .attr('maxlength', '128') .addClass('editempty') .addClass('newinput') .attr('value', html10n.get('pad.userlist.unnamed')); diff --git a/src/templates/pad.html b/src/templates/pad.html index 0d5072cdc01..4ed24c9c89a 100644 --- a/src/templates/pad.html +++ b/src/templates/pad.html @@ -632,7 +632,7 @@

- +
diff --git a/src/tests/backend/specs/authorNameCap.ts b/src/tests/backend/specs/authorNameCap.ts new file mode 100644 index 00000000000..803cb786a55 --- /dev/null +++ b/src/tests/backend/specs/authorNameCap.ts @@ -0,0 +1,226 @@ +'use strict'; + +// GHSA-w4w9-j4c3-hjf6: an unauthenticated client could store an author name of up to ~1 MB +// (bounded only by engine.io's maxHttpBufferSize) via CLIENT_READY or USERINFO_UPDATE, and the +// server rebroadcast it to every client that opened the pad. + +const assert = require('assert').strict; +const common = require('../common'); +const padManager = require('../../../node/db/PadManager'); +const authorManager = require('../../../node/db/AuthorManager'); +const DB = require('../../../node/db/DB'); +const padutils = require('../../../static/js/pad_utils').default; + +const {MAX_AUTHOR_NAME_LENGTH, sanitizeAuthorName} = authorManager; +// Large enough to prove the cap, small enough to stay well under the 1 MB transport limit. +const HUGE = 200000; + +const waitForMessage = (socket: any, pred: (msg: any) => boolean, timeoutMs = 5000) => + new Promise((resolve, reject) => { + const timer = setTimeout(() => { + socket.off('message', onMsg); + reject(new Error('timed out waiting for matching message')); + }, timeoutMs); + const onMsg = (msg: any) => { + if (!pred(msg)) return; + clearTimeout(timer); + socket.off('message', onMsg); + resolve(msg); + }; + socket.on('message', onMsg); + }); + +describe(__filename, function () { + let agent: any; + let padId: string; + const sockets: any[] = []; + + const join = async (userInfo?: any) => { + const res = await agent.get(`/p/${padId}`).expect(200); + const socket = await common.connect(res); + sockets.push(socket); + const msgP = waitForMessage(socket, (m) => m.type === 'CLIENT_VARS' || m.accessStatus); + socket.emit('message', { + component: 'pad', + type: 'CLIENT_READY', + padId, + sessionID: null, + token: padutils.generateAuthorToken(), + ...(userInfo === undefined ? {} : {userInfo}), + }); + const msg = await msgP; + assert.equal(msg.type, 'CLIENT_VARS'); + return {socket, clientVars: msg.data}; + }; + + before(async function () { + agent = await common.init(); + }); + + beforeEach(async function () { + padId = common.randomString(); + assert(!await padManager.doesPadExist(padId)); + }); + + afterEach(async function () { + while (sockets.length) sockets.pop().close(); + if (await padManager.doesPadExist(padId)) await padManager.removePad(padId); + }); + + describe('sanitizeAuthorName', function () { + it('keeps a normal name', function () { + assert.equal(sanitizeAuthorName('Alice'), 'Alice'); + }); + it('trims whitespace', function () { + assert.equal(sanitizeAuthorName(' Alice '), 'Alice'); + }); + it('caps an oversized name', function () { + assert.equal(sanitizeAuthorName('A'.repeat(HUGE)), 'A'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + it('caps after leading whitespace', function () { + assert.equal(sanitizeAuthorName(`${' '.repeat(1000)}${'B'.repeat(HUGE)}`), + 'B'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + it('does not split a surrogate pair', function () { + const out = sanitizeAuthorName('\u{1F600}'.repeat(500)); + assert.equal(Array.from(out).length, MAX_AUTHOR_NAME_LENGTH); + assert.equal(out, '\u{1F600}'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + for (const v of [null, undefined, 42, true, {}, ['a'], {toString: () => 'x'}, '', ' ']) { + it(`returns null for ${JSON.stringify(v)}`, function () { + assert.equal(sanitizeAuthorName(v), null); + }); + } + }); + + describe('CLIENT_READY', function () { + it('stores a normal name unchanged', async function () { + const {clientVars} = await join({name: 'Alice', colorId: '#ff0000'}); + assert.equal(clientVars.userName, 'Alice'); + assert.equal(await authorManager.getAuthorName(clientVars.userId), 'Alice'); + }); + + it('truncates an oversized name before storing it', async function () { + const {clientVars} = await join({name: 'A'.repeat(HUGE), colorId: '#ff0000'}); + assert.equal(clientVars.userName, 'A'.repeat(MAX_AUTHOR_NAME_LENGTH)); + const record = await DB.db.get(`globalAuthor:${clientVars.userId}`); + assert.equal(record.name.length, MAX_AUTHOR_NAME_LENGTH); + }); + + it('does not store a non-string name', async function () { + const {clientVars} = await join({name: {x: 'A'.repeat(1000)}, colorId: '#ff0000'}); + assert.equal(clientVars.userName, undefined); + const record = await DB.db.get(`globalAuthor:${clientVars.userId}`); + assert(record.name == null, `expected no name, got ${JSON.stringify(record.name)}`); + }); + + it('a second client never receives an oversized name', async function () { + const first = await join({name: 'A'.repeat(HUGE), colorId: '#ff0000'}); + const second = await join(); + const hist = second.clientVars.collab_client_vars.historicalAuthorData; + // The first author has not edited yet, so it may not be in the pool; if it is, it is capped. + const entry = hist[first.clientVars.userId]; + if (entry) assert.equal(entry.name.length, MAX_AUTHOR_NAME_LENGTH); + assert(JSON.stringify(second.clientVars).length < HUGE); + }); + }); + + describe('USERINFO_UPDATE', function () { + const update = async (socket: any, name: any) => await common.sendMessage(socket, { + type: 'COLLABROOM', + component: 'pad', + data: {type: 'USERINFO_UPDATE', userInfo: {name, colorId: '#00ff00'}}, + }); + + it('caps an oversized name in storage and in USER_NEWINFO', async function () { + const a = await join(); + const b = await join(); + const newInfoP = waitForMessage(b.socket, (m) => m.type === 'COLLABROOM' && + m.data.type === 'USER_NEWINFO' && m.data.userInfo.userId === a.clientVars.userId && + m.data.userInfo.colorId === '#00ff00'); + await update(a.socket, 'B'.repeat(HUGE)); + const msg = await newInfoP; + assert.equal(msg.data.userInfo.name, 'B'.repeat(MAX_AUTHOR_NAME_LENGTH)); + const record = await DB.db.get(`globalAuthor:${a.clientVars.userId}`); + assert.equal(record.name, 'B'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + + it('drops a non-string name', async function () { + const a = await join({name: 'Alice', colorId: '#ff0000'}); + const b = await join(); + const newInfoP = waitForMessage(b.socket, (m) => m.type === 'COLLABROOM' && + m.data.type === 'USER_NEWINFO' && m.data.userInfo.userId === a.clientVars.userId && + m.data.userInfo.colorId === '#00ff00'); + await update(a.socket, ['B'.repeat(1000)]); + const msg = await newInfoP; + assert.equal(msg.data.userInfo.name, null); + assert((await DB.db.get(`globalAuthor:${a.clientVars.userId}`)).name == null); + }); + + it('keeps a normal name', async function () { + const a = await join(); + await update(a.socket, ' Bob '); + assert.equal(await authorManager.getAuthorName(a.clientVars.userId), 'Bob'); + }); + }); + + describe('existing oversized DB record', function () { + it('is capped in historicalAuthorData, USER_NEWINFO and getAuthorName', async function () { + // Simulate a record stored before the cap existed, for an author in the pad's pool. + const {authorID} = await authorManager.createAuthor(null); + const record = await DB.db.get(`globalAuthor:${authorID}`); + await DB.db.set(`globalAuthor:${authorID}`, {...record, name: 'C'.repeat(HUGE)}); + const pad = await padManager.getPad(padId, 'hello\n', authorID); + assert(pad.getAllAuthors().includes(authorID)); + + const {clientVars} = await join(); + const entry = clientVars.collab_client_vars.historicalAuthorData[authorID]; + assert(entry != null); + assert.equal(entry.name, 'C'.repeat(MAX_AUTHOR_NAME_LENGTH)); + assert(JSON.stringify(clientVars).length < HUGE); + assert.equal(await authorManager.getAuthorName(authorID), 'C'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + + it('a non-string name does not break admin author search', async function () { + const {authorID} = await authorManager.createAuthor(null); + const record = await DB.db.get(`globalAuthor:${authorID}`); + await DB.db.set(`globalAuthor:${authorID}`, {...record, name: {x: 'y'}}); + const res = await authorManager.searchAuthors({ + pattern: authorID.slice(2), offset: 0, limit: 12, sortBy: 'name', ascending: true, + includeErased: false, + }); + const row = res.results.find((r: any) => r.authorID === authorID); + assert(row != null); + assert.equal(row.name, null); + }); + + it('is capped in the joining client\'s own clientVars and USER_NEWINFO', async function () { + const observer = await join(); + // Join once to get an author/token, then inflate the stored name and rejoin with the + // same token (no userInfo, so the stored name is what gets read back). + const token = padutils.generateAuthorToken(); + const res = await agent.get(`/p/${padId}`).expect(200); + const s1 = await common.connect(res); + sockets.push(s1); + const cv1P = waitForMessage(s1, (m) => m.type === 'CLIENT_VARS'); + s1.emit('message', {component: 'pad', type: 'CLIENT_READY', padId, sessionID: null, token}); + const authorID = (await cv1P).data.userId; + s1.close(); + const record = await DB.db.get(`globalAuthor:${authorID}`); + await DB.db.set(`globalAuthor:${authorID}`, {...record, name: 'D'.repeat(HUGE)}); + + const newInfoP = waitForMessage(observer.socket, (m) => m.type === 'COLLABROOM' && + m.data.type === 'USER_NEWINFO' && m.data.userInfo.userId === authorID && + m.data.userInfo.name != null); + const s2 = await common.connect(res); + sockets.push(s2); + const cv2P = waitForMessage(s2, (m) => m.type === 'CLIENT_VARS'); + s2.emit('message', {component: 'pad', type: 'CLIENT_READY', padId, sessionID: null, token}); + const cv2 = (await cv2P).data; + assert.equal(cv2.userId, authorID); + assert.equal(cv2.userName, 'D'.repeat(MAX_AUTHOR_NAME_LENGTH)); + const msg = await newInfoP; + assert.equal(msg.data.userInfo.name, 'D'.repeat(MAX_AUTHOR_NAME_LENGTH)); + }); + }); +});