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): 插件验证码只在确认是自己写入时清理

Dashcc61f2281cd4
diff --git a/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts b/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts
new file mode 100644
index 0000000000..03deceba75
--- /dev/null
+++ b/apps/mobile/src/__tests__/copiedCodeClipboard.test.ts
@@ -0,0 +1,75 @@
+import { beforeEach, expect, it, vi } from "vitest";
+
+const h = vi.hoisted(() => ({
+ os: "ios",
+ text: "",
+ count: 0,
+ // Simulates another app writing between our write and the next version read.
+ interleave: false,
+ getString: vi.fn(async () => h.text),
+ setString: vi.fn(async (value: string) => {
+ h.text = value;
+ h.count++;
+ if (h.interleave) {
+ h.interleave = false;
+ h.text = "other";
+ h.count++;
+ }
+ return true;
+ }),
+}));
+vi.mock("react-native", () => ({
+ Platform: {
+ get OS() {
+ return h.os;
+ },
+ },
+}));
+vi.mock("expo-clipboard", () => ({
+ getStringAsync: h.getString,
+ setStringAsync: h.setString,
+}));
+vi.mock("../../modules/cindy-remote-presentation/src", () => ({
+ remotePresentation: { clipboardVersion: async () => String(h.count) },
+}));
+
+import { clearCopiedCode, copyCode } from "@/plugins/copiedCodeClipboard";
+
+beforeEach(() => {
+ vi.clearAllMocks();
+ Object.assign(h, { os: "ios", text: "before", count: 5, interleave: false });
+});
+
+it("clears its own iOS write by change count without reading the clipboard", async () => {
+ const copied = await copyCode("CODE-1");
+ expect(copied).toEqual({ code: "CODE-1", version: "6" });
+ await clearCopiedCode(copied);
+ expect(h.text).toBe("");
+ expect(h.getString).not.toHaveBeenCalled();
+});
+
+it("keeps a later iOS copy, including one that lands before the version read", async () => {
+ const later = await copyCode("CODE-1");
+ await h.setString("mine");
+ await clearCopiedCode(later);
+ expect(h.text).toBe("mine");
+
+ h.interleave = true;
+ const raced = await copyCode("CODE-2");
+ expect(raced.version).toBeUndefined();
+ await clearCopiedCode(raced);
+ expect(h.text).toBe("other");
+ expect(h.getString).not.toHaveBeenCalled();
+});
+
+it("compares contents on Android, so returning from the browser still clears the code", async () => {
+ h.os = "android";
+ const copied = await copyCode("CODE-1");
+ await clearCopiedCode(copied);
+ expect(h.text).toBe("");
+
+ const replaced = await copyCode("CODE-2");
+ await h.setString("mine");
+ await clearCopiedCode(replaced);
+ expect(h.text).toBe("mine");
+});
diff --git a/apps/mobile/src/plugins/PluginSetupNativeForm.tsx b/apps/mobile/src/plugins/PluginSetupNativeForm.tsx
index 8fcf5c31e7..1f3a83acb7 100644
--- a/apps/mobile/src/plugins/PluginSetupNativeForm.tsx
+++ b/apps/mobile/src/plugins/PluginSetupNativeForm.tsx
@@ -10,7 +10,6 @@ import {
} from "react-native";
import { useIsFocused } from "expo-router";
import * as WebBrowser from "expo-web-browser";
-import * as Clipboard from "expo-clipboard";
import { PLUGIN_OAUTH_CHANNEL } from "@cindy/device-link";
import { useTranslation } from "react-i18next";
import { Text, TextInput } from "@/components/AppText";
@@ -35,7 +34,11 @@ import { type MobilePluginSetupAction } from "./pluginSetupActions";
import type { authenticateMobilePluginOauth } from "./pluginOauthClient";
import { openMobilePluginOauthSession } from "./pluginOauthSession";
import { pluginOauthPlatformCrypto } from "./pluginOauthPlatformCrypto";
-import { remotePresentation } from "../../modules/cindy-remote-presentation/src";
+import {
+ clearCopiedCode,
+ copyCode,
+ type CopiedCode,
+} from "./copiedCodeClipboard";
import { runMobilePluginAuthorization } from "./pluginAuthorizationController";
export default function PluginSetupNativeForm({
@@ -64,7 +67,7 @@ export default function PluginSetupNativeForm({
alive = useRef(true),
lock = useRef(false),
operation = useRef(0),
- copiedVersion = useRef<string | undefined>(undefined);
+ copiedCode = useRef<CopiedCode | undefined>(undefined);
const live = useRef({ focused, epoch: link.connectionEpoch });
live.current = { focused, epoch: link.connectionEpoch };
const [selected, setSelected] = useState<MobilePluginSetupAction | undefined>(
@@ -78,22 +81,14 @@ export default function PluginSetupNativeForm({
() => () => {
alive.current = false;
operation.current++;
- void clearCopiedCode();
+ void releaseCopiedCode();
},
[],
);
- const clearCopiedCode = async () => {
- const version = copiedVersion.current;
- copiedVersion.current = undefined;
- // Compare the change counter instead of reading the clipboard, which would
- // make iOS ask the user for paste permission.
- if (
- version &&
- (await remotePresentation
- ?.clipboardVersion?.()
- .catch(() => undefined)) === version
- )
- await Clipboard.setStringAsync("").catch(() => {});
+ const releaseCopiedCode = async () => {
+ const copied = copiedCode.current;
+ copiedCode.current = undefined;
+ if (copied) await clearCopiedCode(copied);
};
const clear = () => {
setSecret("");
@@ -104,7 +99,7 @@ export default function PluginSetupNativeForm({
clear();
setSelected(undefined);
setBrowser(undefined);
- void clearCopiedCode();
+ void releaseCopiedCode();
onClosed();
};
useEffect(() => {
@@ -243,7 +238,7 @@ export default function PluginSetupNativeForm({
}
} finally {
client?.dispose();
- void clearCopiedCode();
+ void releaseCopiedCode();
lock.current = false;
if (alive.current) {
setBusy(false);
@@ -343,11 +338,10 @@ export default function PluginSetupNativeForm({
accessibilityRole="button"
onPress={() => {
const code = browser.code!;
- void Clipboard.setStringAsync(code)
- .then(() => remotePresentation?.clipboardVersion?.())
- .then((version) => {
- copiedVersion.current = version;
- if (!alive.current) void clearCopiedCode();
+ void copyCode(code)
+ .then((copied) => {
+ copiedCode.current = copied;
+ if (!alive.current) void releaseCopiedCode();
})
.catch(() => {});
}}
diff --git a/apps/mobile/src/plugins/copiedCodeClipboard.ts b/apps/mobile/src/plugins/copiedCodeClipboard.ts
new file mode 100644
index 0000000000..e5559d4a00
--- /dev/null
+++ b/apps/mobile/src/plugins/copiedCodeClipboard.ts
@@ -0,0 +1,41 @@
+import { Platform } from "react-native";
+import * as Clipboard from "expo-clipboard";
+import { remotePresentation } from "../../modules/cindy-remote-presentation/src";
+
+export interface CopiedCode {
+ code: string;
+ /** iOS change count right after our write; absent when ownership is unproven. */
+ version?: string;
+}
+
+const iosVersion = () =>
+ remotePresentation?.clipboardVersion?.().catch(() => undefined) ??
+ Promise.resolve(undefined);
+
+/** Copy a device code and remember enough to clear it later only if it is still ours. */
+export async function copyCode(code: string): Promise<CopiedCode> {
+ if (Platform.OS !== "ios") {
+ await Clipboard.setStringAsync(code);
+ return { code };
+ }
+ const before = await iosVersion();
+ await Clipboard.setStringAsync(code);
+ const after = await iosVersion();
+ // A single increment proves no other write landed between ours and the read.
+ const owned =
+ before !== undefined &&
+ after !== undefined &&
+ Number(after) === Number(before) + 1;
+ return owned ? { code, version: after } : { code };
+}
+
+/** Clear the clipboard only while it still holds the copied code. */
+export async function clearCopiedCode(copied: CopiedCode): Promise<void> {
+ // iOS asks for paste permission when the contents are read, so compare the
+ // change count there. Android has no such prompt; compare the contents.
+ const owned =
+ Platform.OS === "ios"
+ ? copied.version !== undefined && (await iosVersion()) === copied.version
+ : (await Clipboard.getStringAsync().catch(() => "")) === copied.code;
+ if (owned) await Clipboard.setStringAsync("").catch(() => {});
+}