From e07c29940e76e289352fbdc766840837d6712ca0 Mon Sep 17 00:00:00 2001 From: Eric Bailey Date: Thu, 28 Sep 2023 11:45:29 -0500 Subject: [PATCH] improve tag validation (#1684) * check faceted tags for slurs * format * fix baby bug --- packages/pds/src/repo/prepare.ts | 13 +++++++++- packages/pds/tests/create-post.test.ts | 35 +++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/pds/src/repo/prepare.ts b/packages/pds/src/repo/prepare.ts index 2147ef552b6..88201455300 100644 --- a/packages/pds/src/repo/prepare.ts +++ b/packages/pds/src/repo/prepare.ts @@ -31,6 +31,7 @@ import { Record as PostRecord, isRecord as isPost, } from '../lexicon/types/app/bsky/feed/post' +import { isTag } from '../lexicon/types/app/bsky/richtext/facet' import { isRecord as isList } from '../lexicon/types/app/bsky/graph/list' import { isRecord as isProfile } from '../lexicon/types/app/bsky/actor/profile' import { hasExplicitSlur } from '../handle/explicit-slurs' @@ -300,7 +301,17 @@ function assertNoExplicitSlurs(rkey: string, record: RepoRecord) { toCheck += ' ' + rkey toCheck += ' ' + record.displayName } else if (isPost(record)) { - toCheck += record.tags?.join(' ') + if (record.tags) { + toCheck += record.tags.join(' ') + } + + for (const facet of record.facets || []) { + for (const feat of facet.features) { + if (isTag(feat)) { + toCheck += ' ' + feat.tag + } + } + } } if (hasExplicitSlur(toCheck)) { throw new InvalidRecordError('Unacceptable slur in record') diff --git a/packages/pds/tests/create-post.test.ts b/packages/pds/tests/create-post.test.ts index e2763981fb0..3a96d0f15a7 100644 --- a/packages/pds/tests/create-post.test.ts +++ b/packages/pds/tests/create-post.test.ts @@ -1,4 +1,9 @@ -import AtpAgent, { AppBskyFeedPost, AtUri } from '@atproto/api' +import AtpAgent, { + AppBskyFeedPost, + AtUri, + RichText, + AppBskyRichtextFacet, +} from '@atproto/api' import { runTestServer, TestServerInfo } from './_util' import { SeedClient } from './seeds/client' import basicSeed from './seeds/basic' @@ -42,4 +47,32 @@ describe('pds posts record creation', () => { expect(record).toBeTruthy() expect(record.tags).toEqual(['javascript', 'hehe']) }) + + it('handles RichText tag facets as well', async () => { + const rt = new RichText({ text: 'hello #world' }) + await rt.detectFacets(agent) + + const post: AppBskyFeedPost.Record = { + text: rt.text, + facets: rt.facets, + createdAt: new Date().toISOString(), + } + + const res = await agent.api.app.bsky.feed.post.create( + { repo: sc.dids.alice }, + post, + sc.getHeaders(sc.dids.alice), + ) + const { value: record } = await agent.api.app.bsky.feed.post.get({ + repo: sc.dids.alice, + rkey: new AtUri(res.uri).rkey, + }) + + expect(record).toBeTruthy() + expect( + record.facets?.every((f) => { + return AppBskyRichtextFacet.isTag(f.features[0]) + }), + ).toBeTruthy() + }) })