fix(plugins): harden workspace.rename against twin folders and hidden characters

Folder renames require a unique folder id, so twins on two hosts are not both renamed,
and fail when no folder has the id. Names refuse C1, bidi and zero-width characters,
which could make a label read as another workspace's name.
This commit is contained in:
Neil
2026-11-09 00:22:04 -08:00
parent 436cbc6199
commit 4e33873967
4 changed files with 56 additions and 28 deletions
File diff suppressed because one or more lines are too long
@@ -57,6 +57,7 @@ const folderListAnswer = z.object({
})
)
})
const folderUpdateAnswer = z.object({ folderWorkspace: z.object({}).nullable() })
const terminalCreateAnswer = z.object({ terminal: z.object({ handle: z.string() }) })
const terminalReadAnswer = z.object({
terminal: z.object({
@@ -167,10 +168,15 @@ export const PLUGIN_WORKSPACE_ACTION_HANDLERS = {
const trimmed = name.trim()
const scope = parseWorkspaceKey(workspaceId)
if (scope?.type === 'folder') {
await effect(ctx, 'folderWorkspace.update', {
// Twins on two hosts can share a folder id; refuse rather than rename both.
const answer = await effect(ctx, 'folderWorkspace.update', {
folderWorkspaceId: scope.folderWorkspaceId,
requireUniqueId: true,
updates: { name: trimmed }
})
if (!folderUpdateAnswer.parse(answer).folderWorkspace) {
throw new PluginHostCallError('action_failed', 'no folder workspace has this id')
}
return { name: trimmed }
}
// The same update as a rename in the sidebar: the name is pinned, so a branch change keeps it.
@@ -1,5 +1,6 @@
import { describe, expect, it, vi } from 'vitest'
import { executePluginHostCall, type PluginHostServices } from './plugin-host-methods'
import type { PluginCapabilityKind } from '../../../shared/plugins/capabilities/plugin-capabilities'
function services(runtimeRpc: PluginHostServices['runtimeRpc']): PluginHostServices {
return {
@@ -32,14 +33,14 @@ const call = (
method: string,
params: unknown,
rpc: PluginHostServices['runtimeRpc'],
granted = ['workspace:write']
granted: PluginCapabilityKind[] = ['workspace:write']
) =>
executePluginHostCall({
pluginId: 'samples.demo',
method,
params,
surface: 'worker',
grantedCapabilities: granted as never,
grantedCapabilities: granted,
services: services(rpc),
audit: { record: vi.fn().mockResolvedValue(undefined) }
})
@@ -68,18 +69,27 @@ describe('workspace.rename', () => {
await call('workspace.rename', { workspaceId: 'folder:f1', name: 'Notes' }, rpc)
expect(rpc).toHaveBeenCalledWith(
'folderWorkspace.update',
{ folderWorkspaceId: 'f1', updates: { name: 'Notes' } },
{ folderWorkspaceId: 'f1', requireUniqueId: true, updates: { name: 'Notes' } },
expect.anything()
)
})
it('refuses blank or multi-line names and plugins without workspace:write', async () => {
it('fails when no folder workspace has the id', async () => {
const rpc = vi.fn().mockResolvedValue({ folderWorkspace: null })
expect(
await call('workspace.rename', { workspaceId: 'folder:gone', name: 'Notes' }, rpc)
).toMatchObject({ ok: false, code: 'action_failed' })
})
it('refuses blank, multi-line, bidi or invisible names and plugins without workspace:write', async () => {
const rpc = vi.fn()
for (const name of [' ', 'a\nb', 'x'.repeat(201)]) {
expect(await call('workspace.rename', { workspaceId: 'id-1', name }, rpc)).toMatchObject({
ok: false,
code: 'invalid_params'
})
const outcomes = await Promise.all(
[' ', 'a\nb', 'x'.repeat(201), 'main\u202egnp', 'a\u200bb', 'a\u0085b'].map((name) =>
call('workspace.rename', { workspaceId: 'id-1', name }, rpc)
)
)
for (const outcome of outcomes) {
expect(outcome).toMatchObject({ ok: false, code: 'invalid_params' })
}
expect(
await call('workspace.rename', { workspaceId: 'id-1', name: 'ok' }, rpc, ['workspace:read'])
@@ -46,6 +46,20 @@ const workspaceOpenParams = z.strictObject({ workspaceId })
const workspaceOpenResult = z.object({ opened: z.literal(true) })
export const PLUGIN_WORKSPACE_NAME_MAX = 200
// Cc controls, line/paragraph separators, bidi controls, zero-width characters and the BOM.
const HIDDEN_OR_CONTROL_RANGES: readonly (readonly [number, number])[] = [
[0x00, 0x1f],
[0x7f, 0x9f],
[0x61c, 0x61c],
[0x200b, 0x200f],
[0x2028, 0x202e],
[0x2060, 0x2069],
[0xfeff, 0xfeff]
]
const isHiddenOrControl = (char: string): boolean => {
const code = char.codePointAt(0) ?? 0
return HIDDEN_OR_CONTROL_RANGES.some(([low, high]) => code >= low && code <= high)
}
const workspaceRenameParams = z.strictObject({
workspaceId,
/** The name the sidebar shows. It replaces an automatic (branch) name or a person's rename. */
@@ -53,11 +67,9 @@ const workspaceRenameParams = z.strictObject({
.string()
.max(PLUGIN_WORKSPACE_NAME_MAX)
.refine((name) => name.trim().length > 0, 'name must not be blank')
// A sidebar label is one line of plain text: no newlines or other control characters.
.refine(
(name) => [...name].every((char) => char >= ' ' && char !== '\u007f'),
'name must be one line of plain text'
)
// A sidebar label is one line of plain text: no control, bidi or invisible characters,
// which could make it read as another workspace's name.
.refine((name) => ![...name].some(isHiddenOrControl), 'name must be one line of plain text')
})
const workspaceRenameResult = z.object({ name: z.string() })