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): 404 视为撤回、runtimeVersion 缺失维持阻断,修 CodeQL 主机名正则告警

guyongfc833cef78c9
diff --git a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
index 13eba8e3de..47cb213488 100644
--- a/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
+++ b/apps/mobile/src/__tests__/forcedUpdateGate.test.ts
@@ -63,7 +63,9 @@ describe('强更阻断闸门', () => {
expect(layout).toContain('useForcedUpdateRecheck();');
// 解除方向 fail-closed:只有成功拉到 /latest 且判定不再强更才解除,
// 拉取失败 / 记录解析不出 / 拿不到本机 version 一律维持阻断,不能靠断网绕过。
- expect(recheck).toContain("if (!record || !currentVersion) return 'error';");
+ expect(recheck).toContain(
+ "if (!record || !currentRuntimeVersion || !currentVersion) return 'error';",
+ );
expect(recheck).toContain("return 'error'");
// 仍强更时必须刷新 target:服务端可能只修正了坏掉的安装地址。
expect(recheck).toContain('deps.onStillForced(evaluation.target, startRevision)');
diff --git a/apps/mobile/src/update/fetchLatestRelease.test.ts b/apps/mobile/src/update/fetchLatestRelease.test.ts
index f02259592b..3b72e7e49d 100644
--- a/apps/mobile/src/update/fetchLatestRelease.test.ts
+++ b/apps/mobile/src/update/fetchLatestRelease.test.ts
@@ -12,6 +12,18 @@ afterEach(() => {
vi.unstubAllGlobals();
});
+/**
+ * 断言请求 URL = 期望前缀 + `&t=<时间戳>`。
+ * 刻意不用 `new RegExp` 拼含 host 的字符串:URL 里的 `.` 在正则里是通配,
+ * 会匹配到别的主机(CodeQL js/incomplete-hostname-regexp)。
+ */
+function expectCacheBustedUrl(url: unknown, prefix: string): void {
+ expect(typeof url).toBe('string');
+ const bustPrefix = `${prefix}&t=`;
+ expect(String(url).startsWith(bustPrefix)).toBe(true);
+ expect(String(url).slice(bustPrefix.length)).toMatch(/^\d+$/);
+}
+
describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
it('非自建变体(baseUrl 为空)→ null,不发请求', async () => {
const fetchMock = vi.fn();
@@ -24,22 +36,16 @@ describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1', version: '1.2.0' }));
vi.stubGlobal('fetch', fetchMock);
await expect(fetchLatestRelease('ios', 8000, BASE)).resolves.toEqual({ runtimeVersion: 'rtv1', version: '1.2.0' });
- expect(fetchMock).toHaveBeenCalledWith(
- expect.stringMatching(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`)),
- expect.any(Object),
- );
+ const [url] = fetchMock.mock.calls[0] as unknown as [string];
+ expectCacheBustedUrl(url, `${BASE}/latest?platform=ios`);
});
it('canary 显式追加 channel,stable URL 保持旧契约', async () => {
const fetchMock = vi.fn(async () => resp(200, { runtimeVersion: 'rtv1' }));
vi.stubGlobal('fetch', fetchMock);
await fetchLatestRelease('android', 8000, BASE, true);
- expect(fetchMock).toHaveBeenCalledWith(
- expect.stringMatching(
- new RegExp(`^${BASE}/latest\\?platform=android&channel=canary&t=\\d+$`),
- ),
- expect.any(Object),
- );
+ const [url] = fetchMock.mock.calls[0] as unknown as [string];
+ expectCacheBustedUrl(url, `${BASE}/latest?platform=android&channel=canary`);
});
it('一律绕缓存:每次请求都带 cache-buster + no-cache 头', async () => {
@@ -49,7 +55,7 @@ describe('fetchLatestRelease —— 区分"无更新"与"连不上"', () => {
vi.stubGlobal('fetch', fetchMock);
await fetchLatestRelease('ios', 8000, BASE);
const [url, init] = fetchMock.mock.calls[0] as unknown as [string, RequestInit];
- expect(url).toMatch(new RegExp(`^${BASE}/latest\\?platform=ios&t=\\d+$`));
+ expectCacheBustedUrl(url, `${BASE}/latest?platform=ios`);
expect(init.headers).toEqual({ accept: 'application/json', 'cache-control': 'no-cache' });
});
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.test.ts b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
index 47c45be4b2..c8c1b20a8b 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.test.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.test.ts
@@ -220,6 +220,24 @@ describe('createForcedUpdateRechecker 解除判定', () => {
await expect(runOnce(deps)).resolves.toBe('cleared');
});
+ it('服务端 404(整包记录被撤下)→ 解除阻断(冷启动同样不会阻断,运行中不该更严)', async () => {
+ // fetchLatestRelease 只在 404 时返回 null(网络 / 5xx 都抛错),这是服务端明确声明
+ // "该平台当前没有整包记录" —— 记录不存在,门槛也不存在。
+ const deps = makeDeps({ fetchLatest: vi.fn(async () => null) });
+ await expect(runOnce(deps)).resolves.toBe('cleared');
+ expect(deps.onCleared).toHaveBeenCalledWith(7);
+ });
+
+ it('拿不到本机 runtimeVersion → 维持阻断(不能靠 expo-updates 异常绕过)', async () => {
+ const deps = makeDeps({
+ fetchLatest: vi.fn(async () => latestRecord({ minVersion: undefined })),
+ getCurrentRuntimeVersion: () => null,
+ });
+ await expect(runOnce(deps)).resolves.toBe('error');
+ expect(deps.onCleared).not.toHaveBeenCalled();
+ expect(deps.onStillForced).not.toHaveBeenCalled();
+ });
+
it('/latest 拉取失败 → 维持阻断(不能靠断网绕过强更)', async () => {
const deps = makeDeps({ fetchLatest: vi.fn(async () => { throw new Error('offline'); }) });
await expect(runOnce(deps)).resolves.toBe('error');
diff --git a/apps/mobile/src/update/forcedUpdateRecheck.ts b/apps/mobile/src/update/forcedUpdateRecheck.ts
index 8860345f94..60e2cda652 100644
--- a/apps/mobile/src/update/forcedUpdateRecheck.ts
+++ b/apps/mobile/src/update/forcedUpdateRecheck.ts
@@ -111,13 +111,25 @@ export function createForcedUpdateRechecker(
try {
const latest = await withTimeout(deps.fetchLatest(), latestTimeoutMs);
if (deps.isCurrent && !deps.isCurrent()) return 'still-forced';
+ // null 只在服务端 404 时出现(网络 / 5xx 都抛错),是服务端**明确声明**该平台当前
+ // 没有整包记录 —— 记录不存在,门槛也就不存在。必须解除:同样的 404 在冷启动路径上
+ // 根本不会进入阻断(evaluateBundleUpdate 对 null 返回无更新),运行中的进程不该比
+ // 冷启动更严,否则用户被卡到杀进程为止。
+ if (latest === null) {
+ deps.onCleared(startRevision);
+ return 'cleared';
+ }
// 解除必须建立在"真的比出来了不再低于门槛"之上。record 解析不出(指针损坏 /
// 被中间层改坏)或拿不到本机 version 时,evaluateBundleUpdate 会 fail-open 报
// 无更新 —— 那是**进入**方向的正确取向,拿到解除方向就成了漏洞:一条坏记录
// 就能放行所有被强更的装机。这里显式挡掉,按拉取失败处理。
+ // currentRuntimeVersion 同样是必需条件:拿不到它(expo-updates 未启用 / 返回空)时
+ // evaluateBundleUpdate 会 fail-open 报无更新 —— 那是进入方向的取向,用在解除方向
+ // 就成了绕过强更的口子。
const record = parseLatestRelease(latest);
+ const currentRuntimeVersion = String(deps.getCurrentRuntimeVersion() ?? '').trim();
const currentVersion = String(deps.getCurrentVersion() ?? '').trim();
- if (!record || !currentVersion) return 'error';
+ if (!record || !currentRuntimeVersion || !currentVersion) return 'error';
// 新鲜度门:读到的记录不得比正在阻断的目标更旧。可变指针 + 无 cache-buster 的请求
// 会撞上 CDN 边缘的旧记录,那条记录没有 minVersion → 会把仍需强更的用户放出去。
// 记录缺 version(parseLatestRelease 容许空串)同样证明不了新鲜度,一并挡掉。
@@ -128,7 +140,7 @@ export function createForcedUpdateRechecker(
return 'error';
}
const evaluation = evaluateBundleUpdate({
- currentRuntimeVersion: deps.getCurrentRuntimeVersion(),
+ currentRuntimeVersion,
currentVersion,
latest,
});