Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(mobile): 共享访客正文预览先校验权限

Dash1622aef94856
diff --git a/apps/mobile/app/files/preview/[sessionId].tsx b/apps/mobile/app/files/preview/[sessionId].tsx
index 69464a10aa..24e384265f 100644
--- a/apps/mobile/app/files/preview/[sessionId].tsx
+++ b/apps/mobile/app/files/preview/[sessionId].tsx
@@ -718,6 +718,7 @@ function RemoteFilePreviewContent() {
renderItem={({ item, index }) => (
<View style={{ width: pageWidth }}>
<FilePreviewPage
+ deviceId={deviceId}
absolutePathOf={absolutePathOf}
active={Math.abs(index - pageIndex) <= 1}
// HTML 渲染态只在真正可见的当前页挂载可执行 WebView(review P1):
@@ -858,6 +859,7 @@ function PreviewNav({
}
function FilePreviewPage({
+ deviceId,
absolutePathOf,
active,
exportToUrl,
@@ -877,6 +879,7 @@ function FilePreviewPage({
visible,
workdir,
}: {
+ deviceId: string;
absolutePathOf(relPath: string): string;
active: boolean;
exportToUrl: ExportToUrl;
@@ -924,6 +927,7 @@ function FilePreviewPage({
<TextPreviewPage
key={maker.fileBrowser.cacheScope}
cacheScope={maker.fileBrowser.cacheScope}
+ deviceId={deviceId}
absolutePathOf={absolutePathOf}
active={active}
prepareHtmlPreview={prepareHtmlPreview}
@@ -1157,6 +1161,7 @@ function PdfPreviewPage({
*/
function TextPreviewPage({
cacheScope,
+ deviceId,
absolutePathOf,
active,
prepareHtmlPreview,
@@ -1173,6 +1178,7 @@ function TextPreviewPage({
workdir,
}: {
cacheScope: string;
+ deviceId: string;
/** item.relPath → 被控端绝对路径(HTML 资源透传要据此定位同目录)。 */
absolutePathOf(relPath: string): string;
active: boolean;
@@ -1213,7 +1219,7 @@ function TextPreviewPage({
const cacheable = !!workdir;
const [state, setState] = useState<TextPreviewState>(() => {
// Cache identity includes the account, device, workdir and path.
- const cached = cacheable ? getCachedPreviewText(cacheScope, workdir, item.relPath, item.mtimeMs) : null;
+ const cached = cacheable ? getCachedPreviewText(cacheScope, workdir, item.relPath, item.mtimeMs, deviceId) : null;
return cached
? {
status: 'ready',
@@ -1247,7 +1253,7 @@ function TextPreviewPage({
// Keep the visible snapshot, but don't seed future opens from text
// the host has confirmed is no longer previewable.
if (cacheable && (res.code === 'OVERSIZE' || res.code === 'BINARY_FILE')) {
- storeCachedPreviewText(cacheScope, workdir, item.relPath, item.mtimeMs, null);
+ storeCachedPreviewText(cacheScope, workdir, item.relPath, item.mtimeMs, null, deviceId);
}
// A failed refresh must not hide the text the user is already reading.
if (res.code === 'OVERSIZE') {
@@ -1276,7 +1282,7 @@ function TextPreviewPage({
// 原文只为渲染态(markdown / HTML)保留,普通代码文件不留大字符串。
content: richKind ? content : undefined,
};
- if (cacheable) storeCachedPreviewText(cacheScope, workdir, item.relPath, res.data.mtimeMs, ready);
+ if (cacheable) storeCachedPreviewText(cacheScope, workdir, item.relPath, res.data.mtimeMs, ready, deviceId);
setState({ status: 'ready', ...ready });
})
.catch((err) => {
@@ -1289,7 +1295,7 @@ function TextPreviewPage({
cancelled = true;
loadedRef.current = false;
};
- }, [active, wantsText, cacheable, cacheScope, item.relPath, richKind, readTextFile, t, workdir, textReloadEpoch]);
+ }, [active, wantsText, cacheable, cacheScope, deviceId, item.relPath, richKind, readTextFile, t, workdir, textReloadEpoch]);
const htmlSnapshot = useHtmlSnapshot(
absolutePathOf(item.relPath),
diff --git a/apps/mobile/src/__tests__/fileAccessCache.test.ts b/apps/mobile/src/__tests__/fileAccessCache.test.ts
index addae1c898..7623efa264 100644
--- a/apps/mobile/src/__tests__/fileAccessCache.test.ts
+++ b/apps/mobile/src/__tests__/fileAccessCache.test.ts
@@ -1,4 +1,5 @@
import { describe, expect, it, vi } from "vitest";
+import { sharedTaskHostPeer } from "@cindy/device-link";
vi.mock("@react-native-async-storage/async-storage", () => ({
default: {
getItem: vi.fn(async () => null),
@@ -29,15 +30,26 @@ describe("file cache identity", () => {
lines: ["private"],
totalLines: 1,
truncated: false,
- });
+ }, "device-a");
expect(
- getCachedPreviewText("account-a/device-b", "/same", "a", 1),
+ getCachedPreviewText("account-a/device-b", "/same", "a", 1, "device-b"),
).toBeNull();
expect(
- getCachedPreviewText("account-b/device-a", "/same", "a", 1),
+ getCachedPreviewText("account-b/device-a", "/same", "a", 1, "device-a"),
).toBeNull();
// A changed listing mtime doesn't block rendering the previous bounded text.
- expect(getCachedPreviewText("account-a/device-a", "/same", "a", 2)?.lines).toEqual(['private']);
+ expect(getCachedPreviewText("account-a/device-a", "/same", "a", 2, "device-a")?.lines).toEqual(['private']);
+ });
+ it("does not serve or retain shared-task preview bodies across opens", () => {
+ const guest = sharedTaskHostPeer("shared-task", "host");
+ const scope = JSON.stringify(["account", guest]);
+ const body = { lines: ["private"], totalLines: 1, truncated: false };
+ // An old entry must not bypass the next host authorization check.
+ storeCachedPreviewText(scope, "/shared", "old.md", 1, body, "host");
+ expect(getCachedPreviewText(scope, "/shared", "old.md", 2, guest)).toBeNull();
+ // Even an authorized guest read must not seed a future cache-first reopen.
+ storeCachedPreviewText(scope, "/shared", "new.md", 1, body, guest);
+ expect(getCachedPreviewText(scope, "/shared", "new.md", 1, "host")).toBeNull();
});
it("does not coalesce indexes on different devices and preserves the real RPC path", async () => {
const make = (scope: string) => ({
diff --git a/apps/mobile/src/session/fileBrowserCache.ts b/apps/mobile/src/session/fileBrowserCache.ts
index 5d6213ac4d..d205433c00 100644
--- a/apps/mobile/src/session/fileBrowserCache.ts
+++ b/apps/mobile/src/session/fileBrowserCache.ts
@@ -5,11 +5,13 @@
* 规则 7),后台静默刷新后原位更新;跨启动仍有上次快照可秒开。
* 2. 文本迷你缩略图片段:小文件(≤96KB)拉一次首块,渲染真实内容迷你页;
* 片段(≤2KB)内存 + AsyncStorage 双层缓存,key 含 mtime 天然失效。
- * 3. 预览正文:内存 LRU(账号/设备/路径),翻页/重进先展示旧内容再后台更新。
+ * 3. 同账号预览正文:内存 LRU(账号/设备/路径),翻页/重进先展示旧内容再后台更新。
+ * 共享访客不复用正文缓存,每次打开先由主机校验权限。
*
* AsyncStorage 侧统一带索引数组做 LRU 剪枝,防无限膨胀。
*/
import AsyncStorage from "@react-native-async-storage/async-storage";
+import { isSharedTaskPeer } from "@cindy/device-link";
import { useEffect, useState } from "react";
import type { MobileMakerTransport } from "@/device-link/mobileMakerTransport";
@@ -262,7 +264,10 @@ export function getCachedPreviewText(
workdir: string,
relPath: string,
_mtimeMs: number,
+ deviceId: string,
): CachedPreviewText | null {
+ // Shared guests must pass a fresh host authorization check before showing text.
+ if (isSharedTaskPeer(deviceId)) return null;
workdir = JSON.stringify([scope, workdir]);
const key = snippetKey(workdir, relPath, 0);
const value = previewTextMemory.get(key);
@@ -278,7 +283,9 @@ export function storeCachedPreviewText(
relPath: string,
_mtimeMs: number,
value: CachedPreviewText | null,
+ deviceId: string,
): void {
+ if (isSharedTaskPeer(deviceId)) return;
workdir = JSON.stringify([scope, workdir]);
const key = snippetKey(workdir, relPath, 0);
previewTextMemory.delete(key);