From c1018115a7eee4cdcb9042cce5e94afda41e48ba Mon Sep 17 00:00:00 2001 From: xream Date: Sun, 25 Oct 2026 20:12:47 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=20Gist=20=E9=80=BB?= =?UTF-8?q?=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/package.json | 2 +- backend/src/restful/miscs.js | 16 ++--- backend/src/restful/settings.js | 5 ++ .../src/test/restful/gist-backup-age.spec.js | 68 +++++++++++++++++++ backend/src/test/restful/settings.spec.js | 54 +++++++++++++++ 5 files changed, 135 insertions(+), 10 deletions(-) diff --git a/backend/package.json b/backend/package.json index 831b8438..684bec32 100644 --- a/backend/package.json +++ b/backend/package.json @@ -1,6 +1,6 @@ { "name": "sub-store", - "version": "2.44.1", + "version": "2.44.2", "description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.", "main": "src/main.js", "packageManager": "pnpm@11.0.9", diff --git a/backend/src/restful/miscs.js b/backend/src/restful/miscs.js index a383d7f3..a545ae46 100644 --- a/backend/src/restful/miscs.js +++ b/backend/src/restful/miscs.js @@ -266,7 +266,7 @@ async function gistBackupAction( `Gist backup action: ${action}, keep: ${keep}, token strategy: ${queryTokenStrategy}, settings token strategy: ${settings.gistDownloadTokenStrategy}, final token strategy: ${tokenStrategy}, encode: ${encode}, settings encode: ${settings.gistUpload}, final encoding: ${encoding}`, ); switch (action) { - case 'upload': + case 'upload': { let backupContent; try { const keepAgeSecretKey = isAgeGistBackupEncoding(encoding); @@ -323,13 +323,11 @@ async function gistBackupAction( $.info(`上传备份中...`); await $.wait(100); try { - await gist.upload( - { - [GIST_BACKUP_FILE_NAME]: { - content: uploadContent, - }, - } - ); + await gist.upload({ + [GIST_BACKUP_FILE_NAME]: { + content: uploadContent, + }, + }); $.info(`上传备份完成`); } catch (err) { @@ -346,7 +344,6 @@ async function gistBackupAction( await $.wait(500); const downloadedContent = await gist.download( GIST_BACKUP_FILE_NAME, - gists, ); const onlineContent = await decryptGistBackupContent( @@ -382,6 +379,7 @@ async function gistBackupAction( throw err; } break; + } case 'download': { $.info(`还原备份中...`); content = await gist.download(GIST_BACKUP_FILE_NAME); diff --git a/backend/src/restful/settings.js b/backend/src/restful/settings.js index 94a12ce5..38e0c590 100644 --- a/backend/src/restful/settings.js +++ b/backend/src/restful/settings.js @@ -105,6 +105,11 @@ async function updateSettings(req, res) { ...settings, ...req.body, }; + for (const key of ['githubUser', 'gistToken']) { + if (typeof newSettings[key] === 'string') { + newSettings[key] = newSettings[key].trim(); + } + } if (isPlainObject(req.body?.appearanceSetting)) { newSettings.appearanceSetting = { ...(isPlainObject(settings?.appearanceSetting) diff --git a/backend/src/test/restful/gist-backup-age.spec.js b/backend/src/test/restful/gist-backup-age.spec.js index 268f38bb..3383f41b 100644 --- a/backend/src/test/restful/gist-backup-age.spec.js +++ b/backend/src/test/restful/gist-backup-age.spec.js @@ -119,6 +119,74 @@ describe('Gist backup age encryption', function () { expect(backup.settings.syncTime).to.be.a('number'); }); + for (const encoding of ['base64', 'plaintext', 'age']) { + it(`accepts ${encoding} backups when upload fails but verification matches`, async function () { + const pair = await generateKeyPair(); + let uploadedContent; + const downloadedFiles = []; + + installState({ + settings: { + gistToken: 'token', + gistUpload: encoding, + syncTime: 123, + [AGE_SECRET_KEY]: pair[AGE_SECRET_KEY], + }, + subs: [], + }); + + Gist.prototype.download = async (filename) => { + downloadedFiles.push(filename); + return uploadedContent ?? 'old backup'; + }; + Gist.prototype.upload = async (files) => { + uploadedContent = files[GIST_BACKUP_FILE_NAME].content; + throw new Error('Upload response lost'); + }; + + await gistBackupAction('upload'); + + expect(uploadedContent).to.be.a('string'); + expect(downloadedFiles).to.deep.equal([ + GIST_BACKUP_FILE_NAME, + GIST_BACKUP_FILE_NAME, + ]); + expect($.cache.settings.syncTime).to.be.greaterThan(123); + }); + } + + it('restores syncTime and rejects the upload error when verification differs', async function () { + const uploadError = new Error('Upload failed'); + let downloadCount = 0; + + installState({ + settings: { + gistToken: 'token', + syncTime: 123, + }, + subs: [], + }); + + Gist.prototype.download = async () => { + downloadCount++; + return 'old backup'; + }; + Gist.prototype.upload = async () => { + throw uploadError; + }; + + let error; + try { + await gistBackupAction('upload'); + } catch (caught) { + error = caught; + } + + expect(error).to.equal(uploadError); + expect(downloadCount).to.equal(2); + expect($.cache.settings.syncTime).to.equal(123); + }); + it('replaces matching unencrypted online backups in age mode', async function () { const pair = await generateKeyPair(); const initialContent = { diff --git a/backend/src/test/restful/settings.spec.js b/backend/src/test/restful/settings.spec.js index 8137382b..9db7f844 100644 --- a/backend/src/test/restful/settings.spec.js +++ b/backend/src/test/restful/settings.spec.js @@ -8,6 +8,7 @@ import registerSettingsRoutes, { shouldRefreshArtifactStoreForSettingsPatch, } from '@/restful/settings'; import { AGE_SECRET_KEY, generateKeyPair } from '@/utils/age'; +import Gist from '@/utils/gist'; function createRouteApp() { const handlers = new Map(); @@ -119,10 +120,12 @@ describe('settings routes', function () { describe('validated settings', function () { const originalRead = $.read.bind($); const originalWrite = $.write.bind($); + const originalLocate = Gist.prototype.locate; afterEach(function () { $.read = originalRead; $.write = originalWrite; + Gist.prototype.locate = originalLocate; }); async function patchSettings(initialSettings, body) { @@ -148,6 +151,57 @@ describe('settings routes', function () { }; } + it('trims GitHub credentials before saving and refreshing the artifact store', async function () { + let authorization; + Gist.prototype.locate = async function () { + authorization = this.headers.Authorization; + return { html_url: 'https://gist.example.com/artifacts' }; + }; + + const { res, settings } = await patchSettings( + {}, + { + githubUser: ' \t\nexample-user\u3000 ', + gistToken: ' \r\nexample-token\t\u00a0', + }, + ); + + expect(res.body.status).to.equal('success'); + expect(settings.githubUser).to.equal('example-user'); + expect(settings.gistToken).to.equal('example-token'); + expect(res.body.data.githubUser).to.equal('example-user'); + expect(res.body.data.gistToken).to.equal('example-token'); + expect(authorization).to.equal('token example-token'); + }); + + it('saves whitespace-only GitHub credentials as empty without a Gist request', async function () { + let locateCalls = 0; + Gist.prototype.locate = async () => { + locateCalls++; + }; + + const { res, settings } = await patchSettings( + { githubUser: 'old-user', gistToken: 'old-token' }, + { githubUser: ' \t\n\u3000', gistToken: '\r\n \u00a0' }, + ); + + expect(res.body.status).to.equal('success'); + expect(settings.githubUser).to.equal(''); + expect(settings.gistToken).to.equal(''); + expect(locateCalls).to.equal(0); + }); + + it('does not add omitted GitHub credentials on unrelated patches', async function () { + const { res, settings } = await patchSettings( + {}, + { logsMaxCount: 2 }, + ); + + expect(res.body.status).to.equal('success'); + expect(settings).not.to.have.property('githubUser'); + expect(settings).not.to.have.property('gistToken'); + }); + it('does not validate existing age-secret-key on unrelated patches', async function () { const { res, settings } = await patchSettings( {