🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.
TypeScript agentagent-collaborationagent-harnessloop-engineeringmcppersonal-assistantproactive-agentskillssuper-agenttask-management
⚡️ perf(desktop): check OTA core sizes instead of hashing every file at boot (#20211)
Inneie0ce126b6fbc
diff --git a/apps/desktop/scripts/core-ota-test/README.md b/apps/desktop/scripts/core-ota-test/README.mdindex 3ae2964f8f3..13289de9e99 100644--- a/apps/desktop/scripts/core-ota-test/README.md+++ b/apps/desktop/scripts/core-ota-test/README.md@@ -58,11 +58,11 @@ node scripts/core-ota-test/run.mjs tamper # 改 cores/1.0.0-core.2/dist/main/indnode scripts/core-ota-test/run.mjs launch```-期望:壳日志 `core 1.0.0-core.2 rejected: ... hash mismatch`,回退到 `previous`(1.0.0-core.1)。+期望:壳日志 `core 1.0.0-core.2 rejected: size mismatch dist/main/index.js`,回退到 `previous`(1.0.0-core.1)。## 5. boot 失败回滚-直接改 `cores/<v>/` 里的文件会被壳的 hash 校验拦下(等于第 4 步),所以发布一个签名有效但 renderer 永远不 mount 的 v4:+直接改 `cores/<v>/` 里的文件并改变大小会被壳的 size 校验拦下(等于第 4 步),所以发布一个签名有效但 renderer 永远不 mount 的 v4:```bashnode scripts/core-ota-test/run.mjs v4 # index.html 只引一个 js 资源并 throw,seq 3,reloaddiff --git a/apps/desktop/shell/__tests__/core-loader.test.mjs b/apps/desktop/shell/__tests__/core-loader.test.mjsindex 2b22b24de33..e6f4cd95553 100644--- a/apps/desktop/shell/__tests__/core-loader.test.mjs+++ b/apps/desktop/shell/__tests__/core-loader.test.mjs@@ -117,39 +117,49 @@ describe('resolveCore', () => {expect(core.log.join('\n')).toMatch(/signature/);});- it('rejects a core whose main file hash mismatches', () => {+ it('rejects a core whose main file size mismatches', () => {writeExternal('1.1.0', {mutate: (dir) => fs.writeFileSync(path.join(dir, 'dist/main/index.js'), 'tampered'),});writePointer({ current: '1.1.0' });const core = resolve();expect(core.source).toBe('builtin');- expect(core.log.join('\n')).toMatch(/dist\/main\/index\.js/);- });-- it('rejects a core whose renderer file hash mismatches', () => {- writeExternal('1.1.0', {- mutate: (dir) => fs.writeFileSync(path.join(dir, 'dist/renderer/index.html'), 'changed'),- });- writePointer({ current: '1.1.0' });- const core = resolve();- expect(core.source).toBe('builtin');- expect(core.log.join('\n')).toContain('hash mismatch dist/renderer/index.html');+ expect(core.log.join('\n')).toContain('size mismatch dist/main/index.js');});it.each(['node_modules/electron-log/main.js', 'cli/lobe-cli.js', 'package.json'])(- 'rejects a core whose %s hash mismatches',+ 'rejects a core whose %s size mismatches',(file) => {writeExternal('1.1.0', {- mutate: (dir) => fs.writeFileSync(path.join(dir, file), 'tampered'),+ mutate: (dir) => fs.writeFileSync(path.join(dir, file), 'tampered content'),});writePointer({ current: '1.1.0' });const core = resolve();expect(core.source).toBe('builtin');- expect(core.log.join('\n')).toContain(`hash mismatch ${file}`);+ expect(core.log.join('\n')).toContain(`size mismatch ${file}`);},);+ it('rejects a core with a missing tree file', () => {+ writeExternal('1.1.0', {+ mutate: (dir) => fs.rmSync(path.join(dir, 'dist/renderer/index.html')),+ });+ writePointer({ current: '1.1.0' });+ const core = resolve();+ expect(core.source).toBe('builtin');+ expect(core.log.join('\n')).toContain('missing dist/renderer/index.html');+ });++ it('accepts same-size local edits because content is hashed only while staging', () => {+ writeExternal('1.1.0', {+ mutate: (dir) => fs.writeFileSync(path.join(dir, 'dist/renderer/index.html'), 'changed'),+ });+ writePointer({ current: '1.1.0' });+ const core = resolve();+ expect(core.source).toBe('external');+ expect(core.manifest.version).toBe('1.1.0');+ });+it.each(['../x', 'dist/main/../x', './x', '/x', 'dist\\main\\x'])('rejects a signed manifest whose tree contains %s',(unsafe) => {diff --git a/apps/desktop/shell/core-loader.js b/apps/desktop/shell/core-loader.jsindex 1b230d3bf9c..72bb19bac8c 100644--- a/apps/desktop/shell/core-loader.js+++ b/apps/desktop/shell/core-loader.js@@ -1,4 +1,4 @@-const { createHash, verify } = require('node:crypto');+const { verify } = require('node:crypto');const fs = require('node:fs');const Module = require('node:module');const path = require('node:path');@@ -8,8 +8,6 @@ const VERSION_NAME = /^[\w.+-]{1,64}$/;const UNSAFE_SEGMENT = /^\.\.?$/;const MAIN_ENTRY = 'dist/main/index.js';-const sha256 = (buf) => createHash('sha256').update(buf).digest('hex');-const canonicalJson = (value) => {if (Array.isArray(value)) return `[${value.map(canonicalJson).join(',')}]`;if (value && typeof value === 'object') {@@ -64,9 +62,12 @@ const verifyCandidate = (dir, { abi, publicKey }) => {}if (!manifest.tree.some((entry) => entry.path === MAIN_ENTRY))throw new Error(`${MAIN_ENTRY} not in tree`);+ // Content hashes are verified once while staging; hashing ~110 MB here cost ~600 ms on a+ // cold Windows boot. Sizes still catch truncated, deleted or quarantined files.for (const file of manifest.tree) {- if (sha256(fs.readFileSync(path.join(dir, file.path))) !== file.sha256)- throw new Error(`hash mismatch ${file.path}`);+ const stat = fs.statSync(path.join(dir, file.path), { throwIfNoEntry: false });+ if (!stat) throw new Error(`missing ${file.path}`);+ if (stat.size !== file.size) throw new Error(`size mismatch ${file.path}`);}return manifest;};