Skip to content

Commit e599b7e

Browse files
committed
Merge remote-tracking branch 'origin/main' into HEAD
2 parents 1ade03b + c542b78 commit e599b7e

10 files changed

Lines changed: 294 additions & 75 deletions

File tree

‎.pnpm-store/v11/index.db‎

-8 KB
Binary file not shown.

‎apps/desktop/src/preview/BrowserSession.test.ts‎

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const { fromPartition, sessions } = vi.hoisted(() => ({
1313
{
1414
readonly clearCache: ReturnType<typeof vi.fn>;
1515
readonly clearStorageData: ReturnType<typeof vi.fn>;
16-
readonly getUserAgent: ReturnType<typeof vi.fn>;
16+
readonly getUserAgent: ReturnType<typeof vi.fn<() => string>>;
1717
readonly setPermissionRequestHandler: ReturnType<typeof vi.fn>;
1818
readonly setPermissionCheckHandler: ReturnType<typeof vi.fn>;
1919
readonly setUserAgent: ReturnType<typeof vi.fn>;
@@ -102,6 +102,44 @@ describe("BrowserSession", () => {
102102
}).pipe(Effect.provide(layer)),
103103
);
104104

105+
// A rewritten session UA — any variant, even ones that keep the Electron
106+
// token — makes Cloudflare Turnstile loop with error 600010 (#5002), so
107+
// the guest must end up with Electron's native User-Agent. The mock applies
108+
// setUserAgent calls, so this fails on any reintroduced rewrite while still
109+
// permitting a harmless re-set of the unchanged native string.
110+
it.effect("keeps the guest's effective User-Agent equal to Electron's native one", () =>
111+
Effect.gen(function* () {
112+
// Electron's real UA shape: app token, then Chrome, then Electron, then
113+
// Safari — the token order and casing matter to any strip regex.
114+
const nativeUserAgent =
115+
"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) T3Code(Alpha)/0.0.33 Chrome/146.0.7680.216 Electron/41.5.0 Safari/537.36";
116+
fromPartition.mockReset();
117+
fromPartition.mockImplementation((partition: string) => {
118+
let userAgent = nativeUserAgent;
119+
const browserSession = {
120+
clearCache: vi.fn(() => Promise.resolve()),
121+
clearStorageData: vi.fn(() => Promise.resolve()),
122+
getUserAgent: vi.fn(() => userAgent),
123+
setPermissionRequestHandler: vi.fn(),
124+
setPermissionCheckHandler: vi.fn(),
125+
setUserAgent: vi.fn((next: string) => {
126+
userAgent = next;
127+
}),
128+
};
129+
sessions.set(partition, browserSession);
130+
return browserSession;
131+
});
132+
133+
const browserSessions = yield* BrowserSession.BrowserSession;
134+
const partition = yield* browserSessions.getPartition("scope-a");
135+
yield* browserSessions.getSession("scope-a");
136+
137+
const browserSession = sessions.get(partition);
138+
assert.isDefined(browserSession);
139+
assert.strictEqual(browserSession.getUserAgent(), nativeUserAgent);
140+
}).pipe(Effect.provide(layer)),
141+
);
142+
105143
it.effect("grants clipboard-sanitized-write through both the request and check handlers", () =>
106144
Effect.gen(function* () {
107145
const browserSessions = yield* BrowserSession.BrowserSession;

‎apps/desktop/src/preview/BrowserSession.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,11 +197,12 @@ export const make = Effect.gen(function* BrowserSessionMake() {
197197
return Effect.try({
198198
try: () => {
199199
const browserSession = session.fromPartition(partition);
200-
const userAgent = browserSession
201-
.getUserAgent()
202-
.replace(/Electron\/[\d.]+ /, "")
203-
.replace(/\s*t3code\/[\d.]+/, "");
204-
browserSession.setUserAgent(userAgent);
200+
// The guest keeps Electron's native User-Agent. Rewriting it in any
201+
// form — even variants that keep the Electron token — makes Cloudflare
202+
// Turnstile fail its integrity check with error 600010 and recreate
203+
// the challenge every few seconds, so logins behind it never complete
204+
// (#5002). Re-setting the unchanged native string is harmless, so it
205+
// is the rewritten string itself that trips the check.
205206
browserSession.setPermissionRequestHandler((_webContents, permission, callback) => {
206207
callback(ALLOWED_PREVIEW_PERMISSIONS.has(permission));
207208
});

‎apps/mobile/package.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"effect": "catalog:",
7676
"expo": "~57.0.18",
7777
"expo-asset": "~57.0.15",
78-
"expo-audio": "~57.0.4",
78+
"expo-audio": "57.0.4",
7979
"expo-auth-session": "~57.0.10",
8080
"expo-blur": "~57.0.2",
8181
"expo-build-properties": "~57.0.15",
@@ -91,6 +91,7 @@
9191
"expo-glass-effect": "~57.0.1",
9292
"expo-haptics": "~57.0.2",
9393
"expo-image": "~57.0.3",
94+
"expo-image-manipulator": "~57.0.17",
9495
"expo-image-picker": "~57.0.14",
9596
"expo-linking": "~57.0.8",
9697
"expo-network": "~57.0.1",

‎apps/mobile/src/lib/composerFiles.test.ts‎

Lines changed: 137 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const mocks = vi.hoisted(() => ({
1111
open: vi.fn(),
1212
size: vi.fn(),
1313
readBase64: vi.fn(),
14+
manipulate: vi.fn(),
15+
release: vi.fn(),
1416
}));
1517

1618
vi.mock("expo-file-system", () => {
@@ -80,6 +82,10 @@ vi.mock("expo-file-system", () => {
8082

8183
vi.mock("expo-image-picker", () => ({ launchImageLibraryAsync: mocks.pickMedia }));
8284
vi.mock("expo-document-picker", () => ({ getDocumentAsync: mocks.pickFile }));
85+
vi.mock("expo-image-manipulator", () => ({
86+
SaveFormat: { JPEG: "jpeg", PNG: "png", WEBP: "webp" },
87+
ImageManipulator: { manipulate: mocks.manipulate },
88+
}));
8389
vi.mock("./uuid", () => ({ uuidv4: () => "attachment-id" }));
8490

8591
import {
@@ -106,20 +112,50 @@ describe("composer file attachments", () => {
106112
});
107113

108114
describe("photo library image conversion", () => {
109-
const jpeg = "/9j/2Q==";
115+
const rendered = { uri: "file:///cache/ImageManipulator/photo.jpg", base64: "/9j/2Q==" };
110116
const photo: ImagePickerAsset = {
111117
uri: "file:///picker/photo.heic",
112118
type: "image",
113119
fileName: "photo.HEIC",
114120
mimeType: "image/heic",
115-
fileSize: 20 * 1024 * 1024,
116-
base64: jpeg,
117-
width: 1,
118-
height: 1,
121+
fileSize: 4 * 1024 * 1024,
122+
width: 4032,
123+
height: 3024,
124+
};
125+
/**
126+
* Stands in for the native manipulator: `size` is what decoding the source yields,
127+
* `resizes` records every requested resize, and `saved` is what saving returns.
128+
*/
129+
const native = {
130+
size: { width: 1, height: 1 },
131+
resizes: [] as Array<{ width?: number | null; height?: number | null }>,
132+
saved: rendered as { uri: string; base64?: string },
119133
};
120134

135+
beforeEach(() => {
136+
native.size = { width: 1, height: 1 };
137+
native.resizes = [];
138+
native.saved = rendered;
139+
mocks.manipulate.mockReset();
140+
mocks.release.mockReset();
141+
mocks.manipulate.mockImplementation(() => {
142+
const context = {
143+
resize(size: { width?: number | null; height?: number | null }) {
144+
native.resizes.push(size);
145+
return context;
146+
},
147+
renderAsync: async () => ({
148+
...native.size,
149+
release: mocks.release,
150+
saveAsync: async () => native.saved,
151+
}),
152+
};
153+
return context;
154+
});
155+
});
156+
121157
it.each(["image/heic", "image/heif", undefined])(
122-
"attaches the native JPEG conversion with matching metadata when the source MIME is %s",
158+
"renders a %s photo to JPEG natively and previews the rendered file",
123159
async (mimeType) => {
124160
mocks.pickMedia.mockResolvedValue({
125161
canceled: false,
@@ -128,6 +164,9 @@ describe("composer file attachments", () => {
128164

129165
const result = await pickComposerImages({ existingCount: 0 });
130166

167+
expect(mocks.pickMedia).toHaveBeenCalledWith(expect.objectContaining({ base64: false }));
168+
expect(mocks.manipulate).toHaveBeenCalledWith(photo.uri);
169+
expect(mocks.readBase64).not.toHaveBeenCalled();
131170
expect(result).toEqual({
132171
images: [
133172
{
@@ -136,20 +175,35 @@ describe("composer file attachments", () => {
136175
name: "photo.jpg",
137176
mimeType: "image/jpeg",
138177
sizeBytes: 4,
139-
dataUrl: `data:image/jpeg;base64,${jpeg}`,
140-
previewUri: `data:image/jpeg;base64,${jpeg}`,
178+
dataUrl: `data:image/jpeg;base64,${rendered.base64}`,
179+
previewUri: rendered.uri,
141180
},
142181
],
143182
error: null,
144183
});
145184
},
146185
);
147186

187+
it.each([
188+
{ size: { width: 4032, height: 3024 }, resizes: [{ width: 2048 }] },
189+
{ size: { width: 3024, height: 4032 }, resizes: [{ height: 2048 }] },
190+
{ size: { width: 2048, height: 1536 }, resizes: [] },
191+
])("bounds a $size.width x $size.height photo to a 2048 px longest edge", async (input) => {
192+
native.size = input.size;
193+
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [photo] });
194+
195+
await pickComposerImages({ existingCount: 0 });
196+
197+
expect(native.resizes).toEqual(input.resizes);
198+
// Every decoded bitmap is released, including the full-size one a resize replaces.
199+
expect(mocks.release).toHaveBeenCalledTimes(input.resizes.length + 1);
200+
});
201+
148202
it.each([
149203
{ extension: "png", mimeType: "image/png", base64: "iVBORw0KGgo=" },
150204
{ extension: "gif", mimeType: "image/gif", base64: "R0lGODlh" },
151205
{ extension: "webp", mimeType: "image/webp", base64: "UklGRgQAAABXRUJQ" },
152-
])("preserves original $extension bytes instead of the picker's JPEG", async (original) => {
206+
])("keeps original $extension bytes from the picker file", async (original) => {
153207
const name = `photo.${original.extension}`;
154208
mocks.pickMedia.mockResolvedValue({
155209
canceled: false,
@@ -159,44 +213,83 @@ describe("composer file attachments", () => {
159213

160214
const result = await pickComposerImages({ existingCount: 0 });
161215

216+
expect(mocks.manipulate).not.toHaveBeenCalled();
217+
expect(mocks.readBase64).toHaveBeenCalledWith(photo.uri);
162218
expect(result.error).toBeNull();
163219
expect(result.images).toEqual([
164220
expect.objectContaining({
165221
name,
166222
mimeType: original.mimeType,
167223
dataUrl: `data:${original.mimeType};base64,${original.base64}`,
168224
sizeBytes: Buffer.from(original.base64, "base64").byteLength,
225+
previewUri: photo.uri,
169226
}),
170227
]);
171228
});
172229

173-
it("checks the converted JPEG size even when the HEIC source was smaller", async () => {
174-
const oversized =
175-
jpeg.slice(0, 4) + "A".repeat(Math.ceil(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3) * 4);
230+
it("renders a supported original that exceeds the image limit instead of rejecting it", async () => {
176231
mocks.pickMedia.mockResolvedValue({
177232
canceled: false,
178-
assets: [{ ...photo, fileSize: 42, base64: oversized }],
233+
assets: [{ ...photo, fileName: "photo.jpg", mimeType: "image/jpeg" }],
179234
});
235+
mocks.size.mockReturnValue(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
180236

181-
await expect(pickComposerImages({ existingCount: 0 })).resolves.toEqual({
182-
images: [],
183-
error: "'photo.HEIC' exceeds the 10 MB attachment limit.",
237+
const result = await pickComposerImages({ existingCount: 0 });
238+
239+
expect(mocks.manipulate).toHaveBeenCalledWith(photo.uri);
240+
expect(result.error).toBeNull();
241+
expect(result.images).toEqual([
242+
expect.objectContaining({ name: "photo.jpg", mimeType: "image/jpeg", sizeBytes: 4 }),
243+
]);
244+
});
245+
246+
it("measures the picker file instead of trusting the reported size", async () => {
247+
// A content stream can deliver more bytes than the picker advertises; a supported
248+
// original only skips rendering when the file itself measures within the limit.
249+
mocks.pickMedia.mockResolvedValue({
250+
canceled: false,
251+
assets: [{ ...photo, fileName: "photo.png", mimeType: "image/png", fileSize: 42 }],
184252
});
253+
mocks.size.mockReturnValue(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES + 1);
254+
255+
const result = await pickComposerImages({ existingCount: 0 });
256+
257+
expect(mocks.readBase64).not.toHaveBeenCalled();
258+
expect(mocks.manipulate).toHaveBeenCalledWith(photo.uri);
259+
expect(result.images).toEqual([expect.objectContaining({ mimeType: "image/jpeg" })]);
185260
});
186261

187-
it("does not relabel unconverted HEIC bytes as JPEG", async () => {
262+
it("renders a supported original whose size cannot be measured", async () => {
188263
mocks.pickMedia.mockResolvedValue({
189264
canceled: false,
190-
assets: [{ ...photo, base64: "AAAAGGZ0eXBoZWlj" }],
265+
assets: [
266+
{ ...photo, uri: "content://media/1", fileName: "photo.png", mimeType: "image/png" },
267+
],
191268
});
192269

193270
const result = await pickComposerImages({ existingCount: 0 });
194271

195-
expect(result.images).toEqual([]);
196-
expect(result.error).toContain("not a supported image type");
272+
expect(mocks.readBase64).not.toHaveBeenCalled();
273+
expect(mocks.manipulate).toHaveBeenCalledWith("content://media/1");
274+
expect(result.images).toEqual([expect.objectContaining({ mimeType: "image/jpeg" })]);
197275
});
198276

199-
it("retains a converted photo when another original cannot be read", async () => {
277+
it("checks the rendered JPEG against the image limit", async () => {
278+
native.saved = {
279+
uri: rendered.uri,
280+
base64:
281+
rendered.base64.slice(0, 4) +
282+
"A".repeat(Math.ceil(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3) * 4),
283+
};
284+
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [photo] });
285+
286+
await expect(pickComposerImages({ existingCount: 0 })).resolves.toEqual({
287+
images: [],
288+
error: "'photo.HEIC' exceeds the 10 MB attachment limit.",
289+
});
290+
});
291+
292+
it("retains a rendered photo when another original cannot be read", async () => {
200293
mocks.pickMedia.mockResolvedValue({
201294
canceled: false,
202295
assets: [{ ...photo, fileName: "missing.gif", mimeType: "image/gif" }, photo],
@@ -208,6 +301,21 @@ describe("composer file attachments", () => {
208301
expect(result.images).toEqual([expect.objectContaining({ name: "photo.jpg" })]);
209302
expect(result.error).toBe("Failed to read 'missing.gif'.");
210303
});
304+
305+
it("reports a photo the native renderer cannot decode", async () => {
306+
mocks.manipulate.mockImplementation(() => ({
307+
resize: () => {
308+
throw new Error("unreachable");
309+
},
310+
renderAsync: () => Promise.reject(new Error("corrupt")),
311+
}));
312+
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [photo] });
313+
314+
await expect(pickComposerImages({ existingCount: 0 })).resolves.toEqual({
315+
images: [],
316+
error: "Failed to read 'photo.HEIC'.",
317+
});
318+
});
211319
});
212320

213321
describe("photo library videos", () => {
@@ -217,10 +325,13 @@ describe("composer file attachments", () => {
217325
fileName: "photo.png",
218326
mimeType: "image/png",
219327
fileSize: 3,
220-
base64: "YWJj",
221328
width: 1,
222329
height: 1,
223330
};
331+
332+
beforeEach(() => {
333+
mocks.readBase64.mockResolvedValue("YWJj");
334+
});
224335
const video: ImagePickerAsset = {
225336
uri: "file:///picker/clip.mov",
226337
type: "video",
@@ -234,7 +345,9 @@ describe("composer file attachments", () => {
234345

235346
it("retains mixed photos and videos, keeping video bytes in durable file storage", async () => {
236347
mocks.pickMedia.mockResolvedValue({ canceled: false, assets: [image, video] });
237-
mocks.size.mockReturnValue(video.fileSize);
348+
mocks.size.mockImplementation((uri: string) =>
349+
uri.endsWith("clip.mov") ? video.fileSize : 3,
350+
);
238351

239352
const result = await pickComposerMedia({ existingCount: 0, maxVideoBytes: 50 * 1024 * 1024 });
240353

@@ -345,7 +458,7 @@ describe("composer file attachments", () => {
345458
canceled: false,
346459
assets: [{ ...video, fileSize: reported }, image],
347460
});
348-
mocks.size.mockReturnValue(stored);
461+
mocks.size.mockImplementation((uri: string) => (uri.endsWith("clip.mov") ? stored : 3));
349462

350463
const result = await pickComposerMedia({ existingCount: 0, maxVideoBytes: limit });
351464

0 commit comments

Comments
 (0)