Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
fix(sync-service): let comment grants change only comment marks (#7853)
Wolf Mermelstein430f2e0d54ae
diff --git a/apps/web/src/features/block-md/component/MarkdownEditor.tsx b/apps/web/src/features/block-md/component/MarkdownEditor.tsxindex 630df0b43c..877be9da8a 100644--- a/apps/web/src/features/block-md/component/MarkdownEditor.tsx+++ b/apps/web/src/features/block-md/component/MarkdownEditor.tsx@@ -146,7 +146,7 @@ export function MarkdownEditor(props: {permissions,state: documentState,} = useMarkdownDocument();- const { canEdit, canComment } = permissions;+ const { canEdit } = permissions;const blockId = documentId();const documentKind = kind();const sourceBlockName = documentKind === 'document' ? 'md' : documentKind;@@ -227,12 +227,6 @@ export function MarkdownEditor(props: {const [highlightLayerRef, setHighlightLayerRef] =createSignal<HTMLDivElement>();- createEffect(() => {- // We still want the editor to be locked down (for certain things like click events on check- // lists) when the user does not have editor access.- editor.setEditable(editorReady() && (canEdit() || canComment()));- });-const isContentEditable = createMemo(() => {return (editorReady() &&diff --git a/docker/sync-service.Dockerfile b/docker/sync-service.Dockerfileindex 254d93bea9..223bf41993 100644--- a/docker/sync-service.Dockerfile+++ b/docker/sync-service.Dockerfile@@ -68,4 +68,4 @@ EXPOSE 8787# Generate .dev.vars from environment variables so wrangler can resolve secret bindings,# apply local D1 migrations, then start.-CMD ["sh", "-c", "printf \"LOCAL_API_KEY=%s\\nDOCUMENT_PERMISSIONS_SECRET=%s\\nSPS_API_SECRET_KEY=%s\\nDSS_INTERNAL_AUTH_KEY=%s\\n\" \"$INTERNAL_API_SECRET_KEY\" \"$DOCUMENT_PERMISSIONS_SECRET\" \"$INTERNAL_API_SECRET_KEY\" \"$DSS_INTERNAL_AUTH_KEY\" > .dev.vars && CI=true npx wrangler d1 migrations apply USER_PEER_MAPPING --local --config wrangler.docker.toml && exec npx wrangler dev --local --ip 0.0.0.0 --port 8787 --config wrangler.docker.toml"]+CMD ["sh", "-c", "printf \"LOCAL_API_KEY=%s\\nDOCUMENT_PERMISSIONS_SECRET=%s\\nSPS_API_SECRET_KEY=%s\\nDSS_INTERNAL_AUTH_KEY=%s\\nLEXICAL_SERVICE_AUTH_KEY=%s\\n\" \"$INTERNAL_API_SECRET_KEY\" \"$DOCUMENT_PERMISSIONS_SECRET\" \"$INTERNAL_API_SECRET_KEY\" \"$DSS_INTERNAL_AUTH_KEY\" \"$INTERNAL_API_SECRET_KEY\" > .dev.vars && CI=true npx wrangler d1 migrations apply USER_PEER_MAPPING --local --config wrangler.docker.toml && exec npx wrangler dev --local --ip 0.0.0.0 --port 8787 --config wrangler.docker.toml"]diff --git a/services/lexical-service/src/endpoints/comment-only-change.ts b/services/lexical-service/src/endpoints/comment-only-change.tsnew file mode 100644index 0000000000..7fd4bb4d13--- /dev/null+++ b/services/lexical-service/src/endpoints/comment-only-change.ts@@ -0,0 +1,56 @@+import { OpenAPIRoute } from 'chanfana';+import type { Context } from 'hono';+import type { SerializedEditorState } from 'lexical';+import { z } from 'zod';+import { isCommentOnlyChange } from '../lib/comment-only-change';+import { handleEndpointError } from '../lib/error-handler';+import { standardErrorResponses } from '../lib/schemas';++const editorState = z.object({ root: z.object({}).passthrough() });++const commentOnlyChangeRequest = z.object({+ before: editorState,+ after: editorState,+});++export class CommentOnlyChangeEndpoint extends OpenAPIRoute {+ schema = {+ summary: 'Check that a document change only touches comment marks',+ description:+ 'Compares two Lexical editor states of one document and reports whether they differ only in comment marks. The sync service asks this before accepting an update from a session that may comment but not edit.',+ request: {+ body: {+ content: {+ 'application/json': {+ schema: commentOnlyChangeRequest,+ },+ },+ },+ },+ responses: {+ 200: {+ description: 'Whether the change only touches comment marks',+ content: {+ 'application/json': {+ schema: z.object({ commentOnly: z.boolean() }),+ },+ },+ },+ ...standardErrorResponses,+ },+ };++ async handle(c: Context) {+ try {+ const { body } = await this.getValidatedData<typeof this.schema>();+ return c.json({+ commentOnly: isCommentOnlyChange(+ body.before as unknown as SerializedEditorState,+ body.after as unknown as SerializedEditorState+ ),+ });+ } catch (error) {+ return handleEndpointError(error, c);+ }+ }+}diff --git a/services/lexical-service/src/index.ts b/services/lexical-service/src/index.tsindex 26470e08f9..fef17b1723 100644--- a/services/lexical-service/src/index.ts+++ b/services/lexical-service/src/index.ts@@ -8,6 +8,7 @@ import { CognitionPresignedEndpoint } from './endpoints/cognition-presigned';import { CognitionTextEndpoint } from './endpoints/cognition-text';import { CognitionV2Endpoint } from './endpoints/cognition-v2';import { CommentMarkEndpoint } from './endpoints/comment-mark';+import { CommentOnlyChangeEndpoint } from './endpoints/comment-only-change';import { ExtractReplyEndpoint } from './endpoints/extract-reply';import { HtmlEndpoint } from './endpoints/html';import { ImageMarkdownEndpoint } from './endpoints/image-markdown';@@ -46,6 +47,7 @@ app.use('/search/*', internalAuth);app.use('/xml/*', internalAuth);app.use('/markdown/*', internalAuth);app.use('/comment-mark/*', internalAuth);+app.use('/comment-only-change', internalAuth);app.use('/snapshot/*', internalAuth);app.use('/mentions', internalAuth);app.use('/extract-reply', internalAuth);@@ -73,6 +75,7 @@ openapi.get('/cognitionv2/:docId', CognitionV2Endpoint);openapi.get('/search/:docId', SearchTextEndpoint);openapi.get('/markdown/:docId', MarkdownEndpoint);openapi.get('/comment-mark/:docId/:markId', CommentMarkEndpoint);+openapi.post('/comment-only-change', CommentOnlyChangeEndpoint);openapi.get('/xml/:docId', XmlEndpoint);openapi.post('/snapshot/markdown', MarkdownSnapshotEndpoint);openapi.post('/html', HtmlEndpoint);diff --git a/services/lexical-service/src/lib/comment-only-change.test.ts b/services/lexical-service/src/lib/comment-only-change.test.tsnew file mode 100644index 0000000000..9be88e648a--- /dev/null+++ b/services/lexical-service/src/lib/comment-only-change.test.ts@@ -0,0 +1,283 @@+import '../polyfills/prism';+import { describe, expect, it } from 'bun:test';+import { $wrapSelectionInMarkNode } from '@lexical/mark';+import { CommentNode } from '@macro-inc/lexical-core';+import {+ $createRangeSelection,+ $getRoot,+ $isElementNode,+ $isTextNode,+ type SerializedEditorState,+} from 'lexical';+import { isCommentOnlyChange } from './comment-only-change';+import { createEditor } from './editor';++const before = {+ root: {+ $: { id: 'root-1' },+ type: 'root',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ children: [+ {+ $: { id: 'para-1', peerId: 'author' },+ type: 'paragraph',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ textFormat: 0,+ textStyle: '',+ children: [+ {+ $: { id: 'text-1', peerId: 'author' },+ type: 'text',+ version: 1,+ text: 'The second stage backfills the ledger from the archive.',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ ],+ },+ {+ $: { id: 'para-2', peerId: 'author' },+ type: 'paragraph',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ textFormat: 0,+ textStyle: '',+ children: [+ {+ $: { id: 'text-2', peerId: 'author' },+ type: 'text',+ version: 1,+ text: 'Run it twice.',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ ],+ },+ ],+ },+} as unknown as SerializedEditorState;++/** `before` with "backfills the ledger" under a comment mark, as a commenter's client writes it. */+const commented = {+ root: {+ $: { id: 'root-1' },+ type: 'root',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ children: [+ {+ $: { id: 'para-1', peerId: 'author' },+ type: 'paragraph',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ textFormat: 0,+ textStyle: '',+ children: [+ {+ $: { id: 'text-1', peerId: 'author' },+ type: 'text',+ version: 1,+ text: 'The second stage ',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ {+ $: { id: 'mark-node-1', peerId: 'commenter' },+ type: 'comment-mark',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ ids: ['mark-1'],+ isDraft: false,+ children: [+ {+ $: { id: 'text-3', peerId: 'commenter' },+ type: 'text',+ version: 1,+ text: 'backfills the ledger',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ ],+ },+ {+ $: { id: 'text-4', peerId: 'commenter' },+ type: 'text',+ version: 1,+ text: ' from the archive.',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ ],+ },+ {+ $: { id: 'para-2', peerId: 'author' },+ type: 'paragraph',+ version: 1,+ format: '',+ indent: 0,+ direction: null,+ textFormat: 0,+ textStyle: '',+ children: [+ {+ $: { id: 'text-2', peerId: 'author' },+ type: 'text',+ version: 1,+ text: 'Run it twice.',+ format: 0,+ detail: 0,+ mode: 'normal',+ style: '',+ },+ ],+ },+ ],+ },+} as unknown as SerializedEditorState;++/** `commented` changed by `edit`, so each case states only what it alters. */+function changed(+ edit: (root: any) => void,+ state: SerializedEditorState = commented+): SerializedEditorState {+ const copy = structuredClone(state);+ edit(copy.root);+ return copy;+}++describe('isCommentOnlyChange', () => {+ it('accepts wrapping text in a comment mark', () => {+ expect(isCommentOnlyChange(before, commented)).toBe(true);+ });++ it('accepts removing a comment mark', () => {+ expect(isCommentOnlyChange(commented, before)).toBe(true);+ });++ it('accepts changing a mark itself', () => {+ const resolved = changed((root) => {+ const mark = root.children[0].children[1];+ mark.ids = ['mark-1', 'mark-2'];+ mark.$.sharedPeerIds = ['someone-else'];+ });+ expect(isCommentOnlyChange(commented, resolved)).toBe(true);+ });++ it('accepts a mark next to text already stored in pieces', () => {+ const storedInPieces = changed((root) => {+ root.children[0].children = [+ { ...root.children[0].children[0], text: 'The second ' },+ {+ ...root.children[0].children[0],+ $: { id: 'text-5' },+ text: 'stage ',+ },+ ...root.children[0].children.slice(1),+ ];+ });+ const unmarked = changed((root) => {+ const [first, second, mark, last] = root.children[0].children;+ root.children[0].children = [first, second, mark.children[0], last];+ }, storedInPieces);+ expect(isCommentOnlyChange(unmarked, storedInPieces)).toBe(true);+ });++ it('accepts a mark made by Lexical itself', () => {+ const editor = createEditor();+ editor.setEditorState(editor.parseEditorState(before));+ editor.update(+ () => {+ const text = $getRoot().getFirstChildOrThrow();+ if (!$isElementNode(text)) throw new Error('expected a paragraph');+ const node = text.getFirstChildOrThrow();+ if (!$isTextNode(node)) throw new Error('expected text');+ const selection = $createRangeSelection();+ selection.anchor.set(node.getKey(), 17, 'text');+ selection.focus.set(node.getKey(), 37, 'text');+ $wrapSelectionInMarkNode(+ selection,+ false,+ 'mark-1',+ (ids) => new CommentNode(ids, undefined, -1, true)+ );+ },+ { discrete: true }+ );+ const after = editor.getEditorState().toJSON();+ expect(JSON.stringify(after)).toContain('comment-mark');+ expect(isCommentOnlyChange(before, after)).toBe(true);+ });++ it('rejects inserting text inside a mark', () => {+ const edited = changed((root) => {+ root.children[0].children[1].children[0].text = 'deletes the ledger';+ });+ expect(isCommentOnlyChange(commented, edited)).toBe(false);+ });++ it('rejects editing text while adding a mark', () => {+ const edited = changed((root) => {+ root.children[1].children[0].text = 'Run it once.';+ });+ expect(isCommentOnlyChange(before, edited)).toBe(false);+ });++ it('rejects formatting text', () => {+ const bold = changed((root) => {+ root.children[0].children[1].children[0].format = 1;+ });+ expect(isCommentOnlyChange(commented, bold)).toBe(false);+ });++ it('rejects reordering blocks', () => {+ const reordered = changed((root) => {+ root.children.reverse();+ }, before);+ expect(isCommentOnlyChange(before, reordered)).toBe(false);+ });++ it('rejects removing a block', () => {+ const removed = changed((root) => {+ root.children.pop();+ }, before);+ expect(isCommentOnlyChange(before, removed)).toBe(false);+ });++ it('rejects changing node state that is not identity', () => {+ const pinned = changed((root) => {+ root.$.pinnedPropertyIds = ['property-1'];+ }, before);+ expect(isCommentOnlyChange(before, pinned)).toBe(false);+ });++ it('throws on a state that does not parse', () => {+ const unknown = changed((root) => {+ root.children[0].type = 'not-a-node';+ }, before);+ expect(() => isCommentOnlyChange(before, unknown)).toThrow();+ });+});diff --git a/services/lexical-service/src/lib/comment-only-change.ts b/services/lexical-service/src/lib/comment-only-change.tsnew file mode 100644index 0000000000..4a7b3e2d6d--- /dev/null+++ b/services/lexical-service/src/lib/comment-only-change.ts@@ -0,0 +1,76 @@+import { isDeepStrictEqual } from 'node:util';+import { $unwrapMarkNode } from '@lexical/mark';+import { $dfsIterator } from '@lexical/utils';+import { $isCommentNode } from '@macro-inc/lexical-core';+import {+ $getRoot,+ $isTextNode,+ NODE_STATE_KEY,+ type SerializedEditorState,+ type SerializedLexicalNode,+} from 'lexical';+import { createEditor } from './editor';++/**+ * Node state that wrapping text in a comment mark rewrites: the split text+ * nodes are new nodes with their own id and author, so they carry none of+ * the original's identity.+ */+const IDENTITY_STATE_KEYS = ['id', 'peerId', 'sharedPeerIds', 'local'];++/**+ * Whether `after` differs from `before` only in its comment marks: adding,+ * removing or changing a mark is allowed, any other change to the document is+ * not. Throws when either state does not parse.+ */+export function isCommentOnlyChange(+ before: SerializedEditorState,+ after: SerializedEditorState+): boolean {+ return isDeepStrictEqual(+ contentWithoutComments(before),+ contentWithoutComments(after)+ );+}++function contentWithoutComments(state: SerializedEditorState) {+ const editor = createEditor();+ editor.setEditorState(+ editor.parseEditorState({+ ...state,+ root: withoutIdentityState(state.root),+ })+ );+ editor.update(+ () => {+ for (const { node } of [...$dfsIterator($getRoot())]) {+ if ($isCommentNode(node)) $unwrapMarkNode(node);+ // Text normalization only visits dirty nodes; dirtying every text node+ // merges the pieces a mark split off, and any split already stored.+ else if ($isTextNode(node)) node.markDirty();+ }+ },+ { discrete: true }+ );+ return editor.getEditorState().toJSON();+}++function withoutIdentityState<Node extends SerializedLexicalNode>(+ node: Node+): Node {+ const copy: Record<string, unknown> = { ...node };+ const nodeState = node[NODE_STATE_KEY];+ if (nodeState) {+ const kept = Object.fromEntries(+ Object.entries(nodeState).filter(+ ([key]) => !IDENTITY_STATE_KEYS.includes(key)+ )+ );+ if (Object.keys(kept).length > 0) copy[NODE_STATE_KEY] = kept;+ else delete copy[NODE_STATE_KEY];+ }+ if ('children' in node && Array.isArray(node.children)) {+ copy.children = node.children.map(withoutIdentityState);+ }+ return copy as Node;+}diff --git a/services/sync-service/src/auth.rs b/services/sync-service/src/auth.rsindex 5939610e76..ebf651df0c 100644--- a/services/sync-service/src/auth.rs+++ b/services/sync-service/src/auth.rs@@ -12,20 +12,30 @@ use crate::{constants::header_names, error::ResultExt, secrets::Secrets};pub enum AccessLevel {#[default]View = 0,- /// Legacy document sockets permit Comment writes; surfaces do not.+ /// Document sockets may write comment marks; surfaces may not write.Comment = 1,Edit = 2,Owner = 3,Admin = 4,}+/// What a session may change in the document it is connected to.+#[derive(Debug, Clone, Copy, PartialEq, Eq)]+pub enum WriteAccess {+ None,+ /// Comment anchors live in the document body, so commenting writes it;+ /// every other change must be rejected.+ CommentMarks,+ Full,+}+impl AccessLevel {- pub fn can_edit_for(&self, kind: SessionKind) -> bool {- let minimum = match kind {- SessionKind::Document => Self::Comment,- SessionKind::Surface => Self::Edit,- };- self >= &minimum+ pub fn write_access_for(&self, kind: SessionKind) -> WriteAccess {+ match (self, kind) {+ (Self::View, _) | (Self::Comment, SessionKind::Surface) => WriteAccess::None,+ (Self::Comment, SessionKind::Document) => WriteAccess::CommentMarks,+ (Self::Edit | Self::Owner | Self::Admin, _) => WriteAccess::Full,+ }}}diff --git a/services/sync-service/src/auth/test.rs b/services/sync-service/src/auth/test.rsindex dce4a11e5f..2b066372a5 100644--- a/services/sync-service/src/auth/test.rs+++ b/services/sync-service/src/auth/test.rs@@ -62,13 +62,24 @@ fn dual_kind_document_claims_are_rejected() {}#[test]-fn surface_writes_require_edit_without_changing_document_comment_behavior() {- assert!(AccessLevel::Comment.can_edit_for(SessionKind::Document));- assert!(!AccessLevel::View.can_edit_for(SessionKind::Document));+fn document_comment_grants_write_only_comment_marks_and_surfaces_require_edit() {+ assert_eq!(+ AccessLevel::Comment.write_access_for(SessionKind::Document),+ WriteAccess::CommentMarks+ );+ assert_eq!(+ AccessLevel::View.write_access_for(SessionKind::Document),+ WriteAccess::None+ );for level in [AccessLevel::View, AccessLevel::Comment] {- assert!(!level.can_edit_for(SessionKind::Surface));+ assert_eq!(+ level.write_access_for(SessionKind::Surface),+ WriteAccess::None+ );}- for level in [AccessLevel::Edit, AccessLevel::Owner, AccessLevel::Admin] {- assert!(level.can_edit_for(SessionKind::Surface));+ for kind in [SessionKind::Document, SessionKind::Surface] {+ for level in [AccessLevel::Edit, AccessLevel::Owner, AccessLevel::Admin] {+ assert_eq!(level.write_access_for(kind), WriteAccess::Full);+ }}}diff --git a/services/sync-service/src/durable_object.rs b/services/sync-service/src/durable_object.rsindex d649e0aac0..33ad674117 100644--- a/services/sync-service/src/durable_object.rs+++ b/services/sync-service/src/durable_object.rs@@ -19,7 +19,7 @@ use worker::{use crate::{ai_peer::is_ai_peer,- auth::{AccessLevel, TokenFrom, decode_jwt, socket_access},+ auth::{AccessLevel, TokenFrom, WriteAccess, decode_jwt, socket_access},constants::USER_PEER_D1_BINDING,d1::{PeerWithUserId, get_user_id_from_peer_id, insert_user_mapping},domain::document::DocumentAttribution,@@ -258,16 +258,16 @@ impl<'a> Wsm<'a> {.context("invalid signed websocket attribution")}- pub async fn can_edit(&mut self) -> Result<bool> {+ pub async fn write_access(&mut self) -> Result<WriteAccess> {self.maybe_update_ws_meta_map().await?;let ws_id = self.get_ws_id()?.to_string();Ok(self.dss.ws_meta_map- .lock("Wsm::can_edit get")+ .lock("Wsm::write_access get").get(&ws_id).ok_or(Error::from("missing ws metadata"))?- .can_edit())+ .write_access())}pub async fn add_new_peerid(&mut self, peerid: u64, document_id: &str) -> Result<()> {@@ -325,6 +325,10 @@ fn take_editors(editors: &Mutex<BTreeSet<DocumentAttribution>>) -> Vec<DocumentA}impl DocumentSyncSession {+ pub(crate) fn env(&self) -> &Env {+ &self.env+ }+pub(crate) fn socket_for(&self, ws: &WebSocket) -> Socket {Socket::new(ws.clone(), self.inbound.clone())}diff --git a/services/sync-service/src/durable_object/surface_api.rs b/services/sync-service/src/durable_object/surface_api.rsindex 54936ac1b0..c1e08cfaba 100644--- a/services/sync-service/src/durable_object/surface_api.rs+++ b/services/sync-service/src/durable_object/surface_api.rs@@ -7,7 +7,7 @@ use worker::{Date, Method, Request, Response, Result, WebSocket};use super::{DocumentSyncSession, WebSocketMetadata, Wsm, response, status_codes};use crate::{- auth::{TokenFrom, is_internal, surface_access},+ auth::{TokenFrom, WriteAccess, is_internal, surface_access},error::ResultExt,generated::schema::InitializeFromSnapshotRequest,state::DocumentState,@@ -53,9 +53,11 @@ impl WebSocketMetadata {}}- pub(super) fn can_edit(&self) -> bool {- self.grant_active(self.session_kind, now_seconds())- && self.access_level.can_edit_for(self.session_kind)+ pub(super) fn write_access(&self) -> WriteAccess {+ if !self.grant_active(self.session_kind, now_seconds()) {+ return WriteAccess::None;+ }+ self.access_level.write_access_for(self.session_kind)}}diff --git a/services/sync-service/src/lexical_service.rs b/services/sync-service/src/lexical_service.rsnew file mode 100644index 0000000000..2ea7ed059d--- /dev/null+++ b/services/sync-service/src/lexical_service.rs@@ -0,0 +1,76 @@+use serde::Deserialize;+use worker::{Env, Fetch, Method, Request, RequestInit};++use crate::{+ constants::header_names::MACRO_INTERNAL_AUTH_KEY_HEADER_KEY,+ error::ResultExt,+ state::RootChange,+ timeout::{DEFAULT_TIMEOUT_MS, timeout},+};++#[derive(Deserialize)]+#[serde(rename_all = "camelCase")]+struct CommentOnlyChangeResponse {+ comment_only: bool,+}++/// Client for lexical-service, which owns what Lexical node types mean.+pub struct LexicalServiceClient<'a> {+ env: &'a Env,+}++impl<'a> LexicalServiceClient<'a> {+ pub fn new(env: &'a Env) -> Self {+ Self { env }+ }++ fn url(&self) -> worker::Result<String> {+ Ok(self.env.var("LEXICAL_SERVICE_URL")?.to_string())+ }++ fn internal_auth_key(&self) -> worker::Result<String> {+ self.env+ .secret("LEXICAL_SERVICE_AUTH_KEY")+ .map(|value| value.to_string())+ .or_else(|_| {+ self.env+ .var("LEXICAL_SERVICE_AUTH_KEY")+ .map(|value| value.to_string())+ })+ }++ /// Whether `change` adds, removes or alters only comment marks.+ pub async fn is_comment_only_change(&self, change: &RootChange) -> worker::Result<bool> {+ let body = serde_json::json!({+ "before": { "root": change.before },+ "after": { "root": change.after },+ });+ let mut request = Request::new_with_init(+ &format!("{}/comment-only-change", self.url()?),+ RequestInit::new()+ .with_method(Method::Post)+ .with_body(Some(body.to_string().into())),+ )?;+ request.headers_mut()?.set(+ MACRO_INTERNAL_AUTH_KEY_HEADER_KEY,+ &self.internal_auth_key()?,+ )?;+ request+ .headers_mut()?+ .set("Content-Type", "application/json")?;+ let mut response = timeout(Fetch::Request(request).send(), DEFAULT_TIMEOUT_MS)+ .await+ .into_result()??;+ let status = response.status_code();+ if status != 200 {+ return Err(worker::Error::from(format!(+ "lexical-service comment-only-change returned {status}"+ )));+ }+ let parsed: CommentOnlyChangeResponse = response+ .json()+ .await+ .context("failed to parse comment-only-change response")?;+ Ok(parsed.comment_only)+ }+}diff --git a/services/sync-service/src/lib.rs b/services/sync-service/src/lib.rsindex 2bc13987d9..bd088d98d4 100644--- a/services/sync-service/src/lib.rs+++ b/services/sync-service/src/lib.rs@@ -9,6 +9,7 @@ mod durable_object;mod error;mod generated;pub mod keepalive;+mod lexical_service;mod metrics;mod mutex;mod secrets;diff --git a/services/sync-service/src/socket/protocol.rs b/services/sync-service/src/socket/protocol.rsindex cd204b77fb..167121d5cd 100644--- a/services/sync-service/src/socket/protocol.rs+++ b/services/sync-service/src/socket/protocol.rs@@ -4,9 +4,11 @@ use tracing::trace;use worker::Result;use crate::{+ auth::WriteAccess,durable_object::{DocumentSyncSession, Wsm},error::ResultExt,generated::schema::{FromPeer, FromRemote},+ lexical_service::LexicalServiceClient,socket::Socket,state::DocumentState,storage::SessionStorage,@@ -197,9 +199,17 @@ pub async fn process_message(FromPeer::PeerUpdate { updates, id } => {telemetry.op_id = Some(id.to_string());telemetry.update_bytes = Some(updates.iter().map(|u| u.len()).sum());- if !Wsm::new(dss, sender.websocket()).can_edit().await? {- tracing::warn!("received update from peer without edit permission");- return Ok(());+ match Wsm::new(dss, sender.websocket()).write_access().await? {+ WriteAccess::None => {+ tracing::warn!("received update from peer without edit permission");+ return Ok(());+ }+ WriteAccess::CommentMarks => {+ if !changes_only_comment_marks(dss, document_state, &updates).await {+ return Ok(());+ }+ }+ WriteAccess::Full => {}}let attribution = match Wsm::new(dss, sender.websocket()).edit_attribution().await {@@ -323,3 +333,38 @@ pub async fn process_message(Ok(())}++/// A commenter's updates are accepted only when they change nothing but+/// comment marks. Anything that cannot be checked is refused.+async fn changes_only_comment_marks(+ dss: &DocumentSyncSession,+ document_state: &DocumentState,+ updates: &[SliceWrapper<'_, u8>],+) -> bool {+ let updates: Vec<&[u8]> = updates.iter().map(|update| &update[..]).collect();+ let change = match document_state.root_change(&updates) {+ Ok(Some(change)) => change,+ Ok(None) => {+ tracing::warn!("refused commenter update outside the document body or its history");+ return false;+ }+ Err(error) => {+ tracing::warn!(error = ?error, "refused commenter update that failed to import");+ return false;+ }+ };+ match LexicalServiceClient::new(dss.env())+ .is_comment_only_change(&change)+ .await+ {+ Ok(true) => true,+ Ok(false) => {+ tracing::warn!("refused commenter update that changes more than comment marks");+ false+ }+ Err(error) => {+ tracing::error!(error = ?error, "refused commenter update: comment check failed");+ false+ }+ }+}diff --git a/services/sync-service/src/state.rs b/services/sync-service/src/state.rsindex da75cc86f3..1af0c60299 100644--- a/services/sync-service/src/state.rs+++ b/services/sync-service/src/state.rs@@ -24,6 +24,13 @@ pub struct ImportedUpdate {pub touched_nodes: Vec<String>,}+/// The Lexical root of a document before and after a client's updates.+#[derive(Debug, PartialEq)]+pub struct RootChange {+ pub before: serde_json::Value,+ pub after: serde_json::Value,+}+#[derive(Debug)]pub struct DocumentState {pub loro_doc: LoroDoc,@@ -117,6 +124,35 @@ impl DocumentState {})}+ /// The Lexical root before and after `updates`, leaving this document+ /// unchanged. `None` when the updates write outside the root, or depend on+ /// operations this document lacks and so could not be checked.+ pub fn root_change(&self, updates: &[&[u8]]) -> Result<Option<RootChange>> {+ let preview = self.loro_doc.fork();+ for update in updates {+ let status = preview+ .import_with(update, FROM_CLIENT_TAG)+ .context("failed to import update")?;+ if status.pending.is_some() {+ return Ok(None);+ }+ }+ let mut before = deep_json(&self.loro_doc)?;+ let mut after = deep_json(&preview)?;+ let (Some(before_root), Some(after_root)) = (+ before+ .as_object_mut()+ .and_then(|value| value.remove("root")),+ after.as_object_mut().and_then(|value| value.remove("root")),+ ) else {+ return Ok(None);+ };+ Ok((before == after).then_some(RootChange {+ before: before_root,+ after: after_root,+ }))+ }+/// Diff the two frontiers and return the Lexical node IDs whose backing/// containers were modified, deduplicated. Empty `Vec` on diff failure.fn touched_lexical_ids(&self, before: &Frontiers, after: &Frontiers) -> Vec<String> {@@ -214,6 +250,10 @@ impl DocumentState {}}+fn deep_json(doc: &LoroDoc) -> Result<serde_json::Value> {+ serde_json::from_str(&doc.get_deep_value().to_json()).context("failed to read document json")+}+#[cfg(test)]mod tests {use super::*;diff --git a/services/sync-service/src/state/test.rs b/services/sync-service/src/state/test.rsindex 9c7872d1d9..d3a65d1baf 100644--- a/services/sync-service/src/state/test.rs+++ b/services/sync-service/src/state/test.rs@@ -95,3 +95,77 @@ fn shared_history_still_compacts_with_concurrent_heads() {assert!(compact.len() < full.len() / 2);}}++#[test]+fn root_change_previews_an_update_without_applying_it() {+ let state = DocumentState::new();+ state+ .loro_doc+ .get_map("root")+ .insert("type", "root")+ .unwrap();+ let client = LoroDoc::new();+ client+ .import(&state.loro_doc.export(ExportMode::Snapshot).unwrap())+ .unwrap();+ client.get_map("root").insert("indent", 1).unwrap();+ let update = client+ .export(ExportMode::updates(&state.loro_doc.oplog_vv()))+ .unwrap();++ assert_eq!(+ state.root_change(&[&update]).unwrap(),+ Some(RootChange {+ before: serde_json::json!({"type": "root"}),+ after: serde_json::json!({"type": "root", "indent": 1}),+ })+ );+ assert_eq!(+ state.loro_doc.get_deep_value().to_json(),+ r#"{"root":{"type":"root"}}"#+ );+}++#[test]+fn root_change_refuses_writes_outside_the_root() {+ let state = DocumentState::new();+ state+ .loro_doc+ .get_map("root")+ .insert("type", "root")+ .unwrap();+ let client = LoroDoc::new();+ client+ .import(&state.loro_doc.export(ExportMode::Snapshot).unwrap())+ .unwrap();+ client.get_map("elsewhere").insert("hidden", true).unwrap();+ let update = client+ .export(ExportMode::updates(&state.loro_doc.oplog_vv()))+ .unwrap();++ assert_eq!(state.root_change(&[&update]).unwrap(), None);+}++#[test]+fn root_change_refuses_updates_missing_their_history() {+ let state = DocumentState::new();+ state+ .loro_doc+ .get_map("root")+ .insert("type", "root")+ .unwrap();+ let client = LoroDoc::new();+ client+ .import(&state.loro_doc.export(ExportMode::Snapshot).unwrap())+ .unwrap();+ client.get_map("root").insert("indent", 1).unwrap();+ let unseen = client.oplog_vv();+ client.get_map("root").insert("indent", 2).unwrap();+ let depends_on_unseen = client.export(ExportMode::updates(&unseen)).unwrap();++ assert_eq!(state.root_change(&[&depends_on_unseen]).unwrap(), None);+ assert_eq!(+ state.loro_doc.get_deep_value().to_json(),+ r#"{"root":{"type":"root"}}"#+ );+}diff --git a/services/sync-service/tests/comment_marks.test.ts b/services/sync-service/tests/comment_marks.test.tsnew file mode 100644index 0000000000..1980d70945--- /dev/null+++ b/services/sync-service/tests/comment_marks.test.ts@@ -0,0 +1,218 @@+import { createServer, type Server } from "node:http";+import jwt from "jsonwebtoken";+import { LoroDoc } from "loro-crdt";+import type { Miniflare } from "miniflare";+import { afterAll, beforeAll, expect, test } from "vitest";+import {+ FromPeer,+ FromRemote,+ InitializeFromSnapshotRequest,+} from "../bebop/generated/schema";+import { createTestWebSocket, setupMiniflare } from "./utils";++type Verdict = "comment_only" | "not_comment_only" | "fail";+type Check = {+ authKey: string | undefined;+ body: { before: unknown; after: unknown };+};++let mf: Miniflare;+let server: Server;+/** What the stand-in lexical-service answers, per document `root.title`. */+const verdicts = new Map<string, Verdict>();+const checks: Check[] = [];++beforeAll(async () => {+ server = createServer(async (req, res) => {+ const chunks: Buffer[] = [];+ for await (const chunk of req) chunks.push(Buffer.from(chunk));+ const body = JSON.parse(Buffer.concat(chunks).toString());+ checks.push({+ authKey: req.headers["x-internal-auth-key"] as string,+ body,+ });+ const verdict = verdicts.get(body.before.root.title);+ if (+ req.url !== "/comment-only-change" ||+ verdict === "fail" ||+ verdict === undefined+ ) {+ res.writeHead(500).end();+ return;+ }+ res+ .writeHead(200, { "content-type": "application/json" })+ .end(JSON.stringify({ commentOnly: verdict === "comment_only" }));+ });+ await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));+ const address = server.address();+ if (!address || typeof address === "string")+ throw new Error("Missing stand-in address");+ mf = await setupMiniflare({+ lexicalServiceUrl: `http://127.0.0.1:${address.port}`,+ });+}, 60_000);++afterAll(async () => {+ await mf?.dispose();+ await new Promise<void>((resolve, reject) =>+ server?.close((error) => (error ? reject(error) : resolve())),+ );+});++async function seed(verdict: Verdict) {+ const id = crypto.randomUUID();+ verdicts.set(id, verdict);+ const doc = new LoroDoc();+ doc.getMap("root").set("title", id);+ const response = await mf.dispatchFetch(+ `http://localhost/document/${id}/initialize`,+ {+ method: "POST",+ headers: { "x-internal-auth-key": "local" },+ body: InitializeFromSnapshotRequest.encode({+ snapshot: doc.export({ mode: "snapshot" }),+ }),+ },+ );+ expect(response.status).toBe(200);+ return id;+}++async function connect(id: string, access: "comment" | "edit") {+ const token = jwt.sign(+ {+ document_id: id,+ user_id: `${access}-user`,+ access_level: access,+ exp: Math.floor(Date.now() / 1000) + 120,+ },+ "local",+ );+ const response = await mf.dispatchFetch(+ `http://localhost/document/${id}/connect?token=${token}`,+ {+ headers: { Upgrade: "websocket" },+ },+ );+ if (!response.webSocket) throw new Error("Expected a websocket");+ const socket = createTestWebSocket(response.webSocket);+ response.webSocket.accept();+ const initial = FromRemote.decode(+ new Uint8Array((await socket.waitForNextMessage()) as ArrayBuffer),+ );+ if (!initial.isRemoteInitialSync())+ throw new Error("Expected initial snapshot");+ const doc = new LoroDoc();+ doc.import(initial.value.snapshot);+ return { ...socket, doc };+}++type Client = Awaited<ReturnType<typeof connect>>;++/** Sends `write` as one update and resolves with whether the server acked it. */+async function send(+ client: Client,+ write: (doc: LoroDoc) => void,+): Promise<boolean> {+ const from = client.doc.oplogVersion();+ write(client.doc);+ client.doc.commit();+ const id = crypto.randomUUID();+ client.send(+ FromPeer.fromPeerUpdate({+ updates: [client.doc.export({ mode: "update", from })],+ id,+ }).encode(),+ );+ try {+ for (;;) {+ const message = FromRemote.decode(+ new Uint8Array((await client.waitForNextMessage(1500)) as ArrayBuffer),+ );+ if (message.isRemoteUpdateAck() && message.value.id === id) return true;+ }+ } catch {+ return false;+ }+}++async function stored(id: string) {+ const response = await mf.dispatchFetch(+ `http://localhost/document/${id}/raw`,+ {+ headers: { "x-internal-auth-key": "local" },+ },+ );+ expect(response.status).toBe(200);+ return response.json();+}++test("a commenter update that only changes comment marks is stored", async () => {+ const id = await seed("comment_only");+ const commenter = await connect(id, "comment");+ checks.length = 0;++ expect(+ await send(commenter, (doc) => doc.getMap("root").set("mark", "mark-1")),+ ).toBe(true);++ expect(checks).toEqual([+ {+ authKey: "lexical-key",+ body: {+ before: { root: { title: id } },+ after: { root: { title: id, mark: "mark-1" } },+ },+ },+ ]);+ expect(await stored(id)).toEqual({ root: { title: id, mark: "mark-1" } });+});++test("a commenter update that changes more than comment marks is refused", async () => {+ const id = await seed("not_comment_only");+ const commenter = await connect(id, "comment");++ expect(+ await send(commenter, (doc) => doc.getMap("root").set("body", "rewritten")),+ ).toBe(false);++ expect(await stored(id)).toEqual({ root: { title: id } });+});++test("a commenter update is refused when the check fails", async () => {+ const id = await seed("fail");+ const commenter = await connect(id, "comment");++ expect(+ await send(commenter, (doc) => doc.getMap("root").set("mark", "mark-1")),+ ).toBe(false);++ expect(await stored(id)).toEqual({ root: { title: id } });+});++test("a commenter update outside the document body is refused without a check", async () => {+ const id = await seed("comment_only");+ const commenter = await connect(id, "comment");+ checks.length = 0;++ expect(+ await send(commenter, (doc) => doc.getText("content").insert(0, "hidden")),+ ).toBe(false);++ expect(checks).toEqual([]);+ expect(await stored(id)).toEqual({ root: { title: id } });+});++test("an editor update is stored without a check", async () => {+ const id = await seed("fail");+ const editor = await connect(id, "edit");+ checks.length = 0;++ expect(+ await send(editor, (doc) => doc.getMap("root").set("body", "rewritten")),+ ).toBe(true);++ expect(checks).toEqual([]);+ expect(await stored(id)).toEqual({ root: { title: id, body: "rewritten" } });+});diff --git a/services/sync-service/tests/surface_isolation.test.ts b/services/sync-service/tests/surface_isolation.test.tsindex 6f5347231d..12ac4c8ea1 100644--- a/services/sync-service/tests/surface_isolation.test.ts+++ b/services/sync-service/tests/surface_isolation.test.ts@@ -227,7 +227,7 @@ test("initialization races cannot overwrite a winner or revive a tombstone", asyexpect(await initialize("surface", retiring, "revive")).toBe(403);});-test("Comment cannot write surfaces but retains document websocket behavior", async () => {+test("Comment cannot write surfaces or document text outside comment marks", async () => {const id = crypto.randomUUID();for (const kind of ["document", "surface"] as const)expect(await initialize(kind, id, "")).toBe(200);@@ -242,10 +242,13 @@ test("Comment cannot write surfaces but retains document websocket behavior", asid,getTokenForDocument(id, "commenter", "comment"),);- write(document, "allowed");- await document.socket.waitForNextMessage();+ write(document, "denied");+ // The owner's acked write lands after the commenter's, which is refused.+ const owner = await client("document", id);+ write(owner, "edited");+ await owner.socket.waitForNextMessage();expect(await content("surface", id)).toBe("");- expect(await content("document", id)).toBe("allowed");+ expect(await content("document", id)).toBe("edited");});test("pending, revoked and expired sessions fail closed; unsupported endpoints stay unavailable", async () => {diff --git a/services/sync-service/tests/utils.ts b/services/sync-service/tests/utils.tsindex 805908b87d..c42d6f6db8 100644--- a/services/sync-service/tests/utils.ts+++ b/services/sync-service/tests/utils.ts@@ -33,6 +33,7 @@ export async function setupMiniflare(persistPath?: string;migrate?: boolean;dssUrl?: string;+ lexicalServiceUrl?: string;fetchMock?: MiniflareOptions['fetchMock'];} = {},) {@@ -73,6 +74,9 @@ export async function setupMiniflare(SPS_URL: "http://localhost:8092",local:true,...(options.dssUrl ? { DSS_URL: options.dssUrl, DSS_INTERNAL_AUTH_KEY: "local" } : {}),+ ...(options.lexicalServiceUrl+ ? { LEXICAL_SERVICE_URL: options.lexicalServiceUrl, LEXICAL_SERVICE_AUTH_KEY: "lexical-key" }+ : {}),},compatibilityDate: '2025-03-05'});diff --git a/services/sync-service/wrangler.docker.toml b/services/sync-service/wrangler.docker.tomlindex 5dfe9393b2..4db7d5346a 100644--- a/services/sync-service/wrangler.docker.toml+++ b/services/sync-service/wrangler.docker.toml@@ -33,6 +33,7 @@ INTERNAL_API_SECRET_KEY = "LOCAL_API_KEY"SPS_URL = "http://search-processing-service:8080"DSS_URL = "http://document_storage_service:8080"DSS_INTERNAL_AUTH_KEY = "local"+LEXICAL_SERVICE_URL = "http://lexical-service:8096"# Tags exported spans with deployment.environment=local, matching the# Datadog agent's DD_ENV so the env:local trace/log views include them.ENVIRONMENT = "local"diff --git a/services/sync-service/wrangler.toml b/services/sync-service/wrangler.tomlindex 434468990e..81707a6fb3 100644--- a/services/sync-service/wrangler.toml+++ b/services/sync-service/wrangler.toml@@ -34,6 +34,8 @@ INTERNAL_API_SECRET_KEY = "LOCAL_API_KEY"SPS_URL = "http://localhost:8092"DSS_URL = "http://localhost:8086"DSS_INTERNAL_AUTH_KEY = "local"+LEXICAL_SERVICE_URL = "http://localhost:8096"+LEXICAL_SERVICE_AUTH_KEY = "local"# Tags exported spans with deployment.environment=local, matching the# Datadog agent's DD_ENV so the env:local trace/log views include them.ENVIRONMENT = "local"@@ -63,6 +65,7 @@ DOCUMENT_PERMISSIONS_SECRET = "local"ENVIRONMENT = "development"SPS_URL = "https://dev-gateway.macro.com/search-processing"DSS_URL = "https://dev-gateway.macro.com/dss"+LEXICAL_SERVICE_URL = "https://dev-gateway.macro.com/lexical"[env.playground.durable_objects]bindings = [@@ -103,6 +106,7 @@ ENVIRONMENT = "development"OTEL_EXPORTER_OTLP_ENDPOINT = "https://macro-prox-dev.macroverse.workers.dev/i/otlp"SPS_URL = "https://dev-gateway.macro.com/search-processing"DSS_URL = "https://dev-gateway.macro.com/dss"+LEXICAL_SERVICE_URL = "https://dev-gateway.macro.com/lexical"[env.dev.durable_objects]bindings = [@@ -143,6 +147,7 @@ ENVIRONMENT = "production"OTEL_EXPORTER_OTLP_ENDPOINT = "https://macro-prox-prod.macroverse.workers.dev/i/otlp"SPS_URL = "https://gateway.macro.com/search-processing"DSS_URL = "https://gateway.macro.com/dss"+LEXICAL_SERVICE_URL = "https://gateway.macro.com/lexical"[env.prod.durable_objects]bindings = [