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(remote-desktop): 统一 iOS 剪贴板字符计数

Dashd648b662b6e5
diff --git a/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift b/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
index 9e78befb9a..7e23db8cb4 100644
--- a/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
+++ b/apps/mobile/modules/cindy-remote-presentation/ios/CindyRemotePresentationModule.swift
@@ -50,7 +50,7 @@ public class CindyRemotePresentationModule: Module {
/** One atomic portable item; never dereference clipboard file URLs. */
enum RemoteClipboard {
- static let limit = 32 * 1024 * 1024
+ static let limit = RemoteClipboardSize.limit
static func failure(_ code: String) -> NSError {
NSError(domain: "CindyClipboard", code: 1, userInfo: [NSLocalizedDescriptionKey: code])
}
@@ -74,10 +74,14 @@ enum RemoteClipboard {
}
var result: [String: String] = [:]
func string(_ type: String) throws -> String? {
- if let string = item[type] as? String { return string.isEmpty ? nil : string }
+ if let string = item[type] as? String {
+ guard RemoteClipboardSize.accepts(string) else { throw failure("CLIPBOARD_TOO_LONG") }
+ return string.isEmpty ? nil : string
+ }
if let data = item[type] as? Data {
- guard data.count <= limit else { throw failure("CLIPBOARD_TOO_LONG") }
+ guard RemoteClipboardSize.acceptsUTF8Bytes(data) else { throw failure("CLIPBOARD_TOO_LONG") }
guard let string = String(data: data, encoding: .utf8) else { throw failure("CLIPBOARD_UNSUPPORTED") }
+ guard RemoteClipboardSize.accepts(string) else { throw failure("CLIPBOARD_TOO_LONG") }
return string.isEmpty ? nil : string
}
return nil
@@ -108,13 +112,15 @@ enum RemoteClipboard {
try foreground()
guard version == board.changeCount else { throw failure("CLIPBOARD_CHANGED") }
let data = try JSONSerialization.data(withJSONObject: result)
- guard data.count <= limit else { throw failure("CLIPBOARD_TOO_LONG") }
- return String(data: data, encoding: .utf8)!
+ let json = String(data: data, encoding: .utf8)!
+ guard RemoteClipboardSize.accepts(json) else { throw failure("CLIPBOARD_TOO_LONG") }
+ return json
}
static func write(_ json: String) throws {
try foreground()
- guard json.utf8.count <= limit, let data = json.data(using: .utf8),
+ guard RemoteClipboardSize.accepts(json) else { throw failure("CLIPBOARD_TOO_LONG") }
+ guard let data = json.data(using: .utf8),
let content = try JSONSerialization.jsonObject(with: data) as? [String: String],
!content.isEmpty,
content.keys.allSatisfy({ ["text", "html", "rtf", "url", "png"].contains($0) })
diff --git a/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift b/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift
new file mode 100644
index 0000000000..b3a846054a
--- /dev/null
+++ b/apps/mobile/modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift
@@ -0,0 +1,17 @@
+import Foundation
+
+enum RemoteClipboardSize {
+ // CLIPBOARD_MAX_CHARS in packages/device-link/src/remoteClipboard.ts:
+ // JavaScript string.length counts UTF-16 code units, not UTF-8 bytes or graphemes.
+ static let limit = 32 * 1024 * 1024
+
+ static func accepts(_ string: String) -> Bool {
+ string.utf16.count <= limit
+ }
+
+ static func acceptsUTF8Bytes(_ data: Data) -> Bool {
+ // A UTF-16 unit needs at most three UTF-8 bytes. Only a pre-decode bound;
+ // the decoded string must still pass accepts(_:).
+ data.count <= limit * 3
+ }
+}
diff --git a/apps/mobile/scripts/fixtures/remote-clipboard-size.swift b/apps/mobile/scripts/fixtures/remote-clipboard-size.swift
new file mode 100644
index 0000000000..7793041ead
--- /dev/null
+++ b/apps/mobile/scripts/fixtures/remote-clipboard-size.swift
@@ -0,0 +1,17 @@
+import Foundation
+import Darwin
+
+@main
+struct ClipboardSizeTests {
+ static func main() throws {
+ let data = try Data(contentsOf: URL(fileURLWithPath: CommandLine.arguments[1]))
+ let string = String(data: data, encoding: .utf8)!
+ let expected = CommandLine.arguments[2] == "true"
+ guard RemoteClipboardSize.accepts(string) == expected else { exit(1) }
+ if expected {
+ guard RemoteClipboardSize.acceptsUTF8Bytes(data) else { exit(2) }
+ let decoded = String(data: data, encoding: .utf8)!
+ guard RemoteClipboardSize.accepts(decoded) else { exit(3) }
+ }
+ }
+}
diff --git a/apps/mobile/scripts/test-remote-clipboard-native.mjs b/apps/mobile/scripts/test-remote-clipboard-native.mjs
new file mode 100644
index 0000000000..43c12d2df0
--- /dev/null
+++ b/apps/mobile/scripts/test-remote-clipboard-native.mjs
@@ -0,0 +1,41 @@
+#!/usr/bin/env node
+import assert from 'node:assert/strict';
+import { execFileSync } from 'node:child_process';
+import { mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
+import { tmpdir } from 'node:os';
+import { dirname, join, resolve } from 'node:path';
+import { fileURLToPath } from 'node:url';
+import { CLIPBOARD_MAX_CHARS, parseClipboardContent } from '../../../packages/device-link/src/remoteClipboard.ts';
+
+const mobile = resolve(dirname(fileURLToPath(import.meta.url)), '..');
+const directory = mkdtempSync(join(tmpdir(), 'cindy-clipboard-native-'));
+try {
+ const source = join(mobile, 'modules/cindy-remote-presentation/ios/RemoteClipboardSize.swift');
+ const fixture = join(mobile, 'scripts/fixtures/remote-clipboard-size.swift');
+ const executable = join(directory, 'clipboard-tests');
+ execFileSync('swiftc', [source, fixture, '-o', executable], { stdio: 'inherit' });
+ const file = join(directory, 'payload.json');
+ function check(json, expected) {
+ if (expected) assert.doesNotThrow(() => parseClipboardContent(json));
+ else assert.throws(() => parseClipboardContent(json), /CLIPBOARD_TOO_LONG/);
+ writeFileSync(file, json);
+ execFileSync(executable, [file, String(expected)], { stdio: 'inherit' });
+ }
+ // Same wire payload and boundary decisions in JavaScript and compiled Swift.
+ for (const unit of ['a', '中', 'あ', '😀', 'e\u0301']) {
+ const length = CLIPBOARD_MAX_CHARS - JSON.stringify({ text: '' }).length;
+ const text = unit.repeat(Math.floor(length / unit.length)) + 'x'.repeat(length % unit.length);
+ check(JSON.stringify({ text }), true);
+ check(JSON.stringify({ text: text + 'x' }), false);
+ }
+ // Prove this catches the original byte-count guard, not just constant drift.
+ const oldSource = join(directory, 'OldClipboardSize.swift');
+ writeFileSync(oldSource, readFileSync(source, 'utf8').replace('string.utf16.count', 'string.utf8.count'));
+ const oldExecutable = join(directory, 'old-clipboard-tests');
+ execFileSync('swiftc', [oldSource, fixture, '-o', oldExecutable]);
+ writeFileSync(file, JSON.stringify({ text: '中'.repeat(Math.floor(CLIPBOARD_MAX_CHARS / 2)) }));
+ assert.throws(() => execFileSync(oldExecutable, [file, 'true'], { stdio: 'pipe' }));
+ console.log('PASS: 10 UTF-16 boundaries agree with shared protocol; old byte guard fails');
+} finally {
+ rmSync(directory, { recursive: true, force: true });
+}