Every analytics tool wants a subscription. And your data. I built Twiligent instead. YouTube stats. Instagram stats. One dashboard. Runs locally on your machine.
fix(security): confirm post changes from Claude and show every field in the confirmation
spacesdriveb6314148a8e6
diff --git a/backend/mcp/tools/scheduledPostTools.ts b/backend/mcp/tools/scheduledPostTools.tsindex 5f2dbce..4de6781 100644--- a/backend/mcp/tools/scheduledPostTools.ts+++ b/backend/mcp/tools/scheduledPostTools.ts@@ -32,6 +32,26 @@ const postFields: Record<string, JsonSchema> = {locationId: { type: 'string', description: 'A Facebook place id' },};+// the confirmation is the person's only look at a public post before it goes out, so it names every field in full+function describeFields(fields: Partial<ScheduledPostRequest>): string {+ const parts = [+ fields.mediaUrl && `Media: ${fields.mediaUrl}.`,+ fields.scheduledAt && `Time: ${fields.scheduledAt}.`,+ fields.caption !== undefined && `Caption: "${fields.caption}".`,+ fields.collaborators?.length && `Collaborators: ${fields.collaborators.join(', ')}.`,+ fields.altText !== undefined && `Alt text: "${fields.altText}".`,+ fields.locationId && `Location id: ${fields.locationId}.`,+ fields.shareToFeed !== undefined && `Share to feed: ${fields.shareToFeed ? 'yes' : 'no'}.`,+ ];+ return parts.filter(Boolean).join(' ');+}++// the validators accept more fields than these tools publish; anything a client was not told about is refused+function refuseUnlisted(args: Record<string, unknown>, allowed: string[]): void {+ const unlisted = Object.keys(args).filter(name => name !== 'confirmationToken' && !allowed.includes(name));+ if (unlisted.length > 0) throw new ValidationError(`These fields cannot be set from Claude: ${unlisted.join(', ')}. Use the Publish page for them.`);+}+export const listScheduledPosts = defineTool<{ status: PostStatus | undefined; page: Page }>({name: 'list_scheduled_posts',title: 'List scheduled posts',@@ -79,8 +99,9 @@ export const scheduleInstagramPost = defineTool<ScheduledPostRequest>({annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: true },permission: 'write',needsConfirmation: () => true,- describe: input => `Publish a ${input.mediaType ?? 'IMAGE'} post on Instagram account ${input.accountId} at ${input.scheduledAt}${input.caption ? ` with the caption "${input.caption.slice(0, 120)}"` : ''}.`,+ describe: input => `Publish a ${input.mediaType ?? 'IMAGE'} post on Instagram account ${input.accountId}. ${describeFields(input)}`,parse(args) {+ refuseUnlisted(args, ['accountId', 'mediaUrl', 'mediaType', ...Object.keys(postFields)]);const request = parseScheduledPost(args);if (request.mediaType === 'CAROUSEL') throw new ValidationError('Carousels can be scheduled from the Publish page; this tool takes one image or video.');return request;@@ -97,11 +118,14 @@ export const scheduleInstagramPost = defineTool<ScheduledPostRequest>({export const updateScheduledPost = defineTool<{ postId: string; changes: ScheduledPostUpdateRequest }>({name: 'update_scheduled_post',title: 'Change a scheduled post',- description: 'Changes the caption, time or other details of an Instagram post that has not started publishing. Fields left out stay as they are.',- inputSchema: objectSchema({ postId: idProperty('scheduled post'), ...postFields }, ['postId']),+ description: 'Changes the caption, time or other details of an Instagram post that has not started publishing. Fields left out stay as they are. Asks for a confirmation first, because the post goes public.',+ inputSchema: withConfirmation(objectSchema({ postId: idProperty('scheduled post'), ...postFields }, ['postId'])),annotations: { readOnlyHint: false, destructiveHint: false, idempotentHint: true, openWorldHint: false },permission: 'write',+ needsConfirmation: () => true,+ describe: ({ postId, changes }) => `Change scheduled post ${postId}. ${describeFields(changes)}`,parse(args) {+ refuseUnlisted(args, ['postId', ...Object.keys(postFields)]);const changes = parseScheduledPostUpdate(args);if (Object.keys(changes).length === 0) throw new ValidationError('Name at least one field to change, such as caption or scheduledAt.');return { postId: requiredId(args, 'postId', 'scheduled post'), changes };diff --git a/backend/services/scheduledPostService.ts b/backend/services/scheduledPostService.tsindex c5fb736..003bf40 100644--- a/backend/services/scheduledPostService.ts+++ b/backend/services/scheduledPostService.ts@@ -70,7 +70,8 @@ export function createScheduledPostService({ accounts, posts }: ScheduledPostDepconst post = await findOwned(userId, postId);if (post.status !== 'pending') throw new ValidationError('Can only edit pending posts');const updates: Partial<ScheduledPost> = { ...changes };- if (changes.scheduledAt) updates.scheduledAt = new Date(changes.scheduledAt).toISOString();+ // a new time in the past would make the next run publish it at once, so a change of time is held to the same rule+ if (changes.scheduledAt) updates.scheduledAt = futureDate(changes.scheduledAt);if (!(await posts.updatePendingForUser(postId, updates, userId))) throw new ValidationError('Can only edit pending posts');return posts.findForUser(postId, userId);},diff --git a/backend/tests/mcp/scheduledPostTools.test.ts b/backend/tests/mcp/scheduledPostTools.test.tsindex 0259d99..17cd25a 100644--- a/backend/tests/mcp/scheduledPostTools.test.ts+++ b/backend/tests/mcp/scheduledPostTools.test.ts@@ -73,3 +73,19 @@ test('an update needs at least one field, and cancelling needs confirmation', asawait cancelScheduledPost.run({ postId: 'a' }, ctx);assert.deepEqual(ctx.removed, ['a']);});++test('changing a queued post asks for a confirmation that shows every changed field in full', () => {+ const input = updateScheduledPost.parse({ postId: 'a', caption: 'A long caption '.repeat(20), collaborators: ['friend'] });+ assert.equal(updateScheduledPost.needsConfirmation?.(input), true);+ const text = updateScheduledPost.describe?.(input) ?? '';+ assert.match(text, /Change scheduled post a\./);+ assert.ok(text.includes('A long caption '.repeat(20).trim()));+ assert.match(text, /Collaborators: friend\./);+});++test('fields the tools do not publish are refused, so a client cannot set what it was never shown', () => {+ assert.throws(() => updateScheduledPost.parse({ postId: 'a', coverUrl: 'https://cdn.example/x.jpg' }), /cannot be set from Claude: coverUrl/);+ assert.throws(() => scheduleInstagramPost.parse({ accountId: 'ig-1', mediaUrl: 'https://cdn.example/a.jpg', scheduledAt: LATER, userTags: [] }), /userTags/);+ const withMedia = scheduleInstagramPost.parse({ accountId: 'ig-1', mediaUrl: 'https://cdn.example/a.jpg', scheduledAt: LATER, confirmationToken: 'x' });+ assert.match(scheduleInstagramPost.describe?.(withMedia) ?? '', /Media: https:\/\/cdn\.example\/a\.jpg\./);+});