Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions pkgs/id/src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2596,6 +2596,38 @@ mod upload_router_tests {
body
}

#[tokio::test]
async fn collab_route_decodes_encoded_slash_as_one_doc_id_segment() {
let router = Router::new().route(
"/ws/collab/{doc_id}",
get(|Path(doc_id): Path<String>| async move { doc_id }),
);
let response = router
.clone()
.oneshot(
Request::builder()
.uri("/ws/collab/folder%2Fnote.md")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::OK);
let body = to_bytes(response.into_body(), usize::MAX).await.unwrap();
assert_eq!(String::from_utf8(body.to_vec()).unwrap(), "folder/note.md");

let response = router
.oneshot(
Request::builder()
.uri("/ws/collab/folder/note.md")
.body(Body::empty())
.unwrap(),
)
.await
.unwrap();
assert_eq!(response.status(), StatusCode::NOT_FOUND);
}

#[tokio::test]
async fn concurrent_uploads_keep_names_and_metadata_consistent() {
let (router, state, _dir, _endpoint, _docs) = router_with_state().await;
Expand Down
34 changes: 33 additions & 1 deletion pkgs/id/web/src/collab.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Packr, Unpackr } from "msgpackr";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { MAX_STEPS_MESSAGE_BYTES, MAX_STEPS_PER_MESSAGE, initCollab, stepBatch } from "./collab";
import {
collabWebSocketUrl,
MAX_STEPS_MESSAGE_BYTES,
MAX_STEPS_PER_MESSAGE,
initCollab,
stepBatch,
} from "./collab";

// Same MessagePack configuration as collab.ts
const packr = new Packr({ useRecords: false, structuredClone: true });
Expand Down Expand Up @@ -98,6 +104,32 @@ describe("initCollab token handling (#1463)", () => {
document.body.innerHTML = "";
});

it.each([
"plain.md",
"a b.md",
"résumé.md",
"folder/note.md",
"a#b?c.md",
"a%20b.md",
"100%.md",
])("encodes the collab path segment and preserves the filename query for %s", (docId) => {
const connection = track(initCollab(
collabWebSocketUrl("https:", "example.test", docId),
makeContainer(),
docId,
docId,
null,
));

const url = new URL(FakeWebSocket.instances[0].url);
const segment = url.pathname.slice("/ws/collab/".length);
expect(url.protocol).toBe("wss:");
expect(segment).toBe(encodeURIComponent(docId));
expect(decodeURIComponent(segment)).toBe(docId);
expect(url.searchParams.get("filename")).toBe(docId);
connection.disconnect();
});

it("reconnect AUTH sends the AUTH_OK refreshed token, not the init token", () => {
const onTokenRefresh = vi.fn();
const conn = track(initCollab(
Expand Down
6 changes: 6 additions & 0 deletions pkgs/id/web/src/collab.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ export interface CollabConnection {

export type StatusCallback = (status: "connecting" | "connected" | "disconnected" | "error") => void;

/** Build the collab WebSocket URL with docId kept in one encoded path segment. */
export function collabWebSocketUrl(protocol: string, host: string, docId: string): string {
const wsProtocol = protocol === "https:" ? "wss:" : "ws:";
return `${wsProtocol}//${host}/ws/collab/${encodeURIComponent(docId)}`;
}

/**
* Initialize a collaborative editing connection.
* This connects to the WebSocket first, receives the server version and document,
Expand Down
5 changes: 2 additions & 3 deletions pkgs/id/web/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { initHome } from "./home";
import "./world";
import "./world";
import { AutoSaveManager, type SaveResult } from "./auto-save";
import { type CollabConnection, initCollab } from "./collab";
import { type CollabConnection, collabWebSocketUrl, initCollab } from "./collab";
import { type EditorInstance, getEditorState } from "./editor";
import { updateHeaderSubtitle } from "./header-subtitle";
import { identityNeedsRefresh } from "./identity-refresh";
Expand Down Expand Up @@ -953,8 +953,7 @@ async function init(): Promise<void> {

// Connect to collab server - editor will be initialized after receiving server doc
updateStatus("connecting");
const wsProtocol = window.location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${wsProtocol}//${window.location.host}/ws/collab/${docId}`;
const wsUrl = collabWebSocketUrl(window.location.protocol, window.location.host, docId);
console.log("[id] Connecting to WebSocket:", wsUrl);

this.collab = initCollab(
Expand Down
Loading