mirror of
https://github.com/sub-store-org/Sub-Store.git
synced 2026-11-28 23:40:30 +00:00
feat: 支持 Gist 备份使用 age 加密并优化设置页配置
This commit is contained in:
7 files changed
+535
-41
No files matched your search
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "sub-store",
|
||||
"version": "2.28.2",
|
||||
"version": "2.29.0",
|
||||
"description": "Advanced Subscription Manager for QX, Loon, Surge, Stash and Shadowrocket.",
|
||||
"main": "src/main.js",
|
||||
"packageManager": "pnpm@11.0.9",
|
||||
|
||||
+102
-28
@@ -3,7 +3,7 @@ import _ from 'lodash';
|
||||
import $ from '@/core/app';
|
||||
import { ENV } from '@/vendor/open-api';
|
||||
import { failed, success } from '@/restful/response';
|
||||
import { updateArtifactStore, updateAvatar } from '@/restful/settings';
|
||||
import { updateArtifactStore } from '@/restful/settings';
|
||||
import resourceCache from '@/utils/resource-cache';
|
||||
import scriptResourceCache from '@/utils/script-resource-cache';
|
||||
import headersResourceCache from '@/utils/headers-resource-cache';
|
||||
@@ -17,6 +17,13 @@ import Gist from '@/utils/gist';
|
||||
import migrate from '@/utils/migration';
|
||||
import env from '@/utils/env';
|
||||
import { formatDateTime } from '@/utils';
|
||||
import {
|
||||
AGE_SECRET_KEY,
|
||||
decryptArmorIfPresent,
|
||||
derivePublicKey,
|
||||
encryptArmor,
|
||||
isAgeArmor,
|
||||
} from '@/utils/age';
|
||||
|
||||
export default function register($app) {
|
||||
// utils
|
||||
@@ -144,6 +151,65 @@ async function refresh(_, res) {
|
||||
success(res);
|
||||
}
|
||||
|
||||
function readCurrentBackupContent() {
|
||||
let content = $.read('#sub-store');
|
||||
content = content ? JSON.parse(content) : {};
|
||||
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
|
||||
return content;
|
||||
}
|
||||
|
||||
function serializeGistBackupContent(content, encoding, options = {}) {
|
||||
const backup = JSON.parse(JSON.stringify(content || {}));
|
||||
if (!options.keepAgeSecretKey && backup.settings?.[AGE_SECRET_KEY]) {
|
||||
delete backup.settings[AGE_SECRET_KEY];
|
||||
}
|
||||
if (encoding === 'plaintext') {
|
||||
backup.settings = backup.settings || {};
|
||||
backup.settings.gistToken = '恢复后请重新设置 GitHub Token';
|
||||
return JSON.stringify(backup, null, ` `);
|
||||
}
|
||||
|
||||
return Base64.encode(JSON.stringify(backup, null, ` `));
|
||||
}
|
||||
|
||||
function normalizeGistBackupEncoding(encoding) {
|
||||
return ['base64', 'plaintext', 'age'].includes(encoding)
|
||||
? encoding
|
||||
: 'base64';
|
||||
}
|
||||
|
||||
function getGistBackupPayloadEncoding(encoding) {
|
||||
return encoding === 'plaintext' ? 'plaintext' : 'base64';
|
||||
}
|
||||
|
||||
function isAgeGistBackupEncoding(encoding) {
|
||||
return encoding === 'age';
|
||||
}
|
||||
|
||||
async function encryptGistBackupContent(content, settings, encoding) {
|
||||
if (!isAgeGistBackupEncoding(encoding)) return content;
|
||||
|
||||
const ageSecretKey = settings?.[AGE_SECRET_KEY];
|
||||
if (!ageSecretKey) {
|
||||
throw new Error('age 加密模式需要配置 age 解密私钥');
|
||||
}
|
||||
|
||||
$.info(`使用 age 加密 Gist 备份内容`);
|
||||
return encryptArmor(content, await derivePublicKey(ageSecretKey));
|
||||
}
|
||||
|
||||
async function decryptGistBackupContent(content, settings, encoding) {
|
||||
if (!isAgeGistBackupEncoding(encoding)) return content;
|
||||
|
||||
const ageSecretKey = settings?.[AGE_SECRET_KEY];
|
||||
if (!ageSecretKey) {
|
||||
throw new Error('age 加密模式需要配置 age 解密私钥');
|
||||
}
|
||||
|
||||
$.info(`尝试使用 age 解密 Gist 备份内容`);
|
||||
return decryptArmorIfPresent(content, ageSecretKey);
|
||||
}
|
||||
|
||||
async function gistBackupAction(action, keep, encode) {
|
||||
// read token
|
||||
const { gistToken, syncPlatform } = $.read(SETTINGS_KEY);
|
||||
@@ -154,38 +220,40 @@ async function gistBackupAction(action, keep, encode) {
|
||||
key: GIST_BACKUP_KEY,
|
||||
syncPlatform,
|
||||
});
|
||||
let currentContent = $.read('#sub-store');
|
||||
currentContent = currentContent ? JSON.parse(currentContent) : {};
|
||||
if ($.env.isNode) currentContent = JSON.parse(JSON.stringify($.cache));
|
||||
let currentContent = readCurrentBackupContent();
|
||||
let content;
|
||||
const settings = $.read(SETTINGS_KEY);
|
||||
const updated = settings.syncTime;
|
||||
|
||||
const encoding = encode || settings.gistUpload || 'base64';
|
||||
const encoding = normalizeGistBackupEncoding(
|
||||
encode || settings.gistUpload || 'base64',
|
||||
);
|
||||
$.info(
|
||||
`Gist backup action: ${action}, keep: ${keep}, encode: ${encode}, settings encode: ${settings.gistUpload}, final encoding: ${encoding}`,
|
||||
);
|
||||
switch (action) {
|
||||
case 'upload':
|
||||
try {
|
||||
content = $.read('#sub-store');
|
||||
content = content ? JSON.parse(content) : {};
|
||||
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
|
||||
if (encoding === 'plaintext') {
|
||||
content.settings.gistToken =
|
||||
'恢复后请重新设置 GitHub Token';
|
||||
content = JSON.stringify(content, null, ` `);
|
||||
} else {
|
||||
content = Base64.encode(
|
||||
JSON.stringify(content, null, ` `),
|
||||
);
|
||||
}
|
||||
const keepAgeSecretKey = isAgeGistBackupEncoding(encoding);
|
||||
content = serializeGistBackupContent(
|
||||
readCurrentBackupContent(),
|
||||
getGistBackupPayloadEncoding(encoding),
|
||||
{ keepAgeSecretKey },
|
||||
);
|
||||
|
||||
$.info(`下载备份, 与本地内容对比...`);
|
||||
const onlineContent = await gist.download(
|
||||
const downloadedContent = await gist.download(
|
||||
GIST_BACKUP_FILE_NAME,
|
||||
);
|
||||
if (onlineContent === content) {
|
||||
const onlineContent = await decryptGistBackupContent(
|
||||
downloadedContent,
|
||||
settings,
|
||||
encoding,
|
||||
);
|
||||
const canReuseOnlineContent =
|
||||
!isAgeGistBackupEncoding(encoding) ||
|
||||
isAgeArmor(downloadedContent);
|
||||
if (canReuseOnlineContent && onlineContent === content) {
|
||||
$.info(`内容一致, 无需上传备份`);
|
||||
return;
|
||||
}
|
||||
@@ -196,15 +264,16 @@ async function gistBackupAction(action, keep, encode) {
|
||||
// update syncTime
|
||||
settings.syncTime = new Date().getTime();
|
||||
$.write(settings, SETTINGS_KEY);
|
||||
content = $.read('#sub-store');
|
||||
content = content ? JSON.parse(content) : {};
|
||||
if ($.env.isNode) content = JSON.parse(JSON.stringify($.cache));
|
||||
if (encoding === 'plaintext') {
|
||||
content.settings.gistToken = '恢复后请重新设置 GitHub Token';
|
||||
content = JSON.stringify(content, null, ` `);
|
||||
} else {
|
||||
content = Base64.encode(JSON.stringify(content, null, ` `));
|
||||
}
|
||||
content = serializeGistBackupContent(
|
||||
readCurrentBackupContent(),
|
||||
getGistBackupPayloadEncoding(encoding),
|
||||
{ keepAgeSecretKey: isAgeGistBackupEncoding(encoding) },
|
||||
);
|
||||
content = await encryptGistBackupContent(
|
||||
content,
|
||||
settings,
|
||||
encoding,
|
||||
);
|
||||
$.info(`上传备份中...`);
|
||||
try {
|
||||
await gist.upload({
|
||||
@@ -221,6 +290,11 @@ async function gistBackupAction(action, keep, encode) {
|
||||
case 'download':
|
||||
$.info(`还原备份中...`);
|
||||
content = await gist.download(GIST_BACKUP_FILE_NAME);
|
||||
content = await decryptGistBackupContent(
|
||||
content,
|
||||
settings,
|
||||
encoding,
|
||||
);
|
||||
try {
|
||||
content = JSON.parse(Base64.decode(content));
|
||||
if (!(Object.keys(content.settings).length >= 0)) {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { SETTINGS_KEY, ARTIFACT_REPOSITORY_KEY } from '@/constants';
|
||||
import { success, failed } from './response';
|
||||
import { InternalServerError } from '@/restful/errors';
|
||||
import { InternalServerError, RequestInvalidError } from '@/restful/errors';
|
||||
import $ from '@/core/app';
|
||||
import Gist, { getGithubGistBaseURL } from '@/utils/gist';
|
||||
import { clearLogSettingsCache } from '@/utils/debug-logs';
|
||||
@@ -8,6 +8,10 @@ import {
|
||||
BACKEND_REQUEST_CONCURRENCY_SETTING,
|
||||
BACKEND_REQUEST_CONCURRENCY_WAIT_TIME_SETTING,
|
||||
} from '@/utils/request-concurrency';
|
||||
import {
|
||||
AGE_SECRET_KEY,
|
||||
normalizeAgeSecretKeyConfig,
|
||||
} from '@/utils/age';
|
||||
|
||||
const ARTIFACT_STORE_SETTING_KEYS = [
|
||||
'gistToken',
|
||||
@@ -16,6 +20,34 @@ const ARTIFACT_STORE_SETTING_KEYS = [
|
||||
'defaultProxy',
|
||||
];
|
||||
|
||||
function isPlainObject(value) {
|
||||
return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function hasOwn(object, key) {
|
||||
return Object.prototype.hasOwnProperty.call(object || {}, key);
|
||||
}
|
||||
|
||||
function shouldValidateGistAgeSecretKey(settings, body = {}) {
|
||||
return (
|
||||
body.gistUpload === 'age' ||
|
||||
(settings?.gistUpload === 'age' &&
|
||||
hasOwn(body, AGE_SECRET_KEY))
|
||||
);
|
||||
}
|
||||
|
||||
async function normalizeAndValidateGistAgeSecretKey(settings) {
|
||||
await normalizeAgeSecretKeyConfig(settings);
|
||||
|
||||
const ageSecretKey = settings[AGE_SECRET_KEY];
|
||||
if (!ageSecretKey) {
|
||||
throw new RequestInvalidError(
|
||||
'INVALID_GIST_AGE_KEYS',
|
||||
'age 加密模式需要配置 age-secret-key',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function shouldRefreshArtifactStoreForSettingsPatch(body = {}) {
|
||||
return ARTIFACT_STORE_SETTING_KEYS.some((key) =>
|
||||
Object.prototype.hasOwnProperty.call(body, key),
|
||||
@@ -69,6 +101,14 @@ async function updateSettings(req, res) {
|
||||
...settings,
|
||||
...req.body,
|
||||
};
|
||||
if (isPlainObject(req.body?.appearanceSetting)) {
|
||||
newSettings.appearanceSetting = {
|
||||
...(isPlainObject(settings?.appearanceSetting)
|
||||
? settings.appearanceSetting
|
||||
: {}),
|
||||
...req.body.appearanceSetting,
|
||||
};
|
||||
}
|
||||
[
|
||||
'defaultTimeout',
|
||||
'githubApiTimeout',
|
||||
@@ -126,6 +166,9 @@ async function updateSettings(req, res) {
|
||||
];
|
||||
}
|
||||
}
|
||||
if (shouldValidateGistAgeSecretKey(newSettings, req.body)) {
|
||||
await normalizeAndValidateGistAgeSecretKey(newSettings);
|
||||
}
|
||||
$.write(newSettings, SETTINGS_KEY);
|
||||
clearLogSettingsCache();
|
||||
if (shouldRefreshArtifactStoreForSettingsPatch(req.body)) {
|
||||
@@ -137,11 +180,13 @@ async function updateSettings(req, res) {
|
||||
$.error(`Failed to update settings: ${e.message ?? e}`);
|
||||
failed(
|
||||
res,
|
||||
new InternalServerError(
|
||||
`FAILED_TO_UPDATE_SETTINGS`,
|
||||
`Failed to update settings`,
|
||||
`Reason: ${e.message ?? e}`,
|
||||
),
|
||||
e instanceof RequestInvalidError
|
||||
? e
|
||||
: new InternalServerError(
|
||||
`FAILED_TO_UPDATE_SETTINGS`,
|
||||
`Failed to update settings`,
|
||||
`Reason: ${e.message ?? e}`,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
import { expect } from 'chai';
|
||||
import { afterEach, describe, it } from 'mocha';
|
||||
import { Base64 } from 'js-base64';
|
||||
|
||||
import $ from '@/core/app';
|
||||
import { GIST_BACKUP_FILE_NAME, SETTINGS_KEY } from '@/constants';
|
||||
import { gistBackupAction } from '@/restful/miscs';
|
||||
import Gist from '@/utils/gist';
|
||||
import {
|
||||
AGE_ARMOR_HEADER,
|
||||
AGE_PUBLIC_KEY,
|
||||
AGE_SECRET_KEY,
|
||||
decryptArmorIfPresent,
|
||||
encryptArmor,
|
||||
generateKeyPair,
|
||||
} from '@/utils/age';
|
||||
|
||||
describe('Gist backup age encryption', function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
const originalInfo = $.info.bind($);
|
||||
const originalError = $.error.bind($);
|
||||
const originalUpload = Gist.prototype.upload;
|
||||
const originalDownload = Gist.prototype.download;
|
||||
const originalCache = $.cache;
|
||||
|
||||
afterEach(function () {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
$.info = originalInfo;
|
||||
$.error = originalError;
|
||||
Gist.prototype.upload = originalUpload;
|
||||
Gist.prototype.download = originalDownload;
|
||||
$.cache = originalCache;
|
||||
});
|
||||
|
||||
function installState(initialContent) {
|
||||
$.cache = JSON.parse(JSON.stringify(initialContent));
|
||||
$.read = (key) => {
|
||||
if (key === SETTINGS_KEY) return $.cache.settings;
|
||||
if (key === '#sub-store') return JSON.stringify($.cache);
|
||||
return originalRead(key);
|
||||
};
|
||||
$.write = (data, key) => {
|
||||
if (key === SETTINGS_KEY) {
|
||||
$.cache.settings = data;
|
||||
return true;
|
||||
}
|
||||
if (key === '#sub-store') {
|
||||
$.cache = JSON.parse(data);
|
||||
return true;
|
||||
}
|
||||
return originalWrite(data, key);
|
||||
};
|
||||
$.info = () => {};
|
||||
$.error = () => {};
|
||||
}
|
||||
|
||||
it('encrypts Gist backup uploads in age mode', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
let uploadedContent;
|
||||
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
gistUpload: 'age',
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async () => 'old backup';
|
||||
Gist.prototype.upload = async (files) => {
|
||||
uploadedContent = files[GIST_BACKUP_FILE_NAME].content;
|
||||
return {};
|
||||
};
|
||||
|
||||
await gistBackupAction('upload', undefined, 'age');
|
||||
|
||||
expect(uploadedContent).to.contain(AGE_ARMOR_HEADER);
|
||||
const decrypted = await decryptArmorIfPresent(
|
||||
uploadedContent,
|
||||
pair[AGE_SECRET_KEY],
|
||||
);
|
||||
const backup = JSON.parse(Base64.decode(decrypted));
|
||||
|
||||
expect(backup.settings.gistToken).to.equal('token');
|
||||
expect(backup.settings).not.to.have.property(AGE_PUBLIC_KEY);
|
||||
expect(backup.settings[AGE_SECRET_KEY]).to.equal(pair[AGE_SECRET_KEY]);
|
||||
expect(backup.settings.syncTime).to.be.a('number');
|
||||
});
|
||||
|
||||
it('does not use age encryption outside age mode', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
let uploadedContent;
|
||||
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
gistUpload: 'base64',
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async () => 'old backup';
|
||||
Gist.prototype.upload = async (files) => {
|
||||
uploadedContent = files[GIST_BACKUP_FILE_NAME].content;
|
||||
return {};
|
||||
};
|
||||
|
||||
await gistBackupAction('upload', undefined, 'base64');
|
||||
|
||||
expect(uploadedContent).not.to.contain(AGE_ARMOR_HEADER);
|
||||
const backup = JSON.parse(Base64.decode(uploadedContent));
|
||||
|
||||
expect(backup.settings.gistToken).to.equal('token');
|
||||
expect(backup.settings).not.to.have.property(AGE_PUBLIC_KEY);
|
||||
expect(backup.settings).not.to.have.property(AGE_SECRET_KEY);
|
||||
expect(backup.settings.syncTime).to.be.a('number');
|
||||
});
|
||||
|
||||
it('replaces matching unencrypted online backups in age mode', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
const initialContent = {
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
gistUpload: 'age',
|
||||
syncTime: 123,
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [],
|
||||
};
|
||||
const unencryptedOnlineContent = Base64.encode(
|
||||
JSON.stringify(initialContent, null, ` `),
|
||||
);
|
||||
let uploadedContent;
|
||||
|
||||
installState(initialContent);
|
||||
|
||||
Gist.prototype.download = async () => unencryptedOnlineContent;
|
||||
Gist.prototype.upload = async (files) => {
|
||||
uploadedContent = files[GIST_BACKUP_FILE_NAME].content;
|
||||
return {};
|
||||
};
|
||||
|
||||
await gistBackupAction('upload', undefined, 'age');
|
||||
|
||||
expect(uploadedContent).to.contain(AGE_ARMOR_HEADER);
|
||||
});
|
||||
|
||||
it('decrypts age-armored Gist backup downloads before restoring', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
const restoredBackup = {
|
||||
settings: {
|
||||
gistToken: 'restored-token',
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [
|
||||
{
|
||||
name: 'restored-sub',
|
||||
source: 'local',
|
||||
content: 'content',
|
||||
},
|
||||
],
|
||||
};
|
||||
const encryptedBackup = await encryptArmor(
|
||||
Base64.encode(JSON.stringify(restoredBackup, null, ` `)),
|
||||
pair[AGE_PUBLIC_KEY],
|
||||
);
|
||||
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'current-token',
|
||||
gistUpload: 'age',
|
||||
[AGE_SECRET_KEY]: pair[AGE_SECRET_KEY],
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async () => encryptedBackup;
|
||||
|
||||
await gistBackupAction('download');
|
||||
|
||||
expect($.cache.settings.gistToken).to.equal('restored-token');
|
||||
expect($.cache.settings).not.to.have.property(AGE_PUBLIC_KEY);
|
||||
expect($.cache.subs).to.deep.equal(restoredBackup.subs);
|
||||
});
|
||||
|
||||
it('rejects age upload mode without an age secret key', async function () {
|
||||
installState({
|
||||
settings: {
|
||||
gistToken: 'token',
|
||||
gistUpload: 'age',
|
||||
},
|
||||
subs: [],
|
||||
});
|
||||
|
||||
Gist.prototype.download = async () => 'old backup';
|
||||
|
||||
try {
|
||||
await gistBackupAction('upload', undefined, 'age');
|
||||
throw new Error('Expected age upload to fail');
|
||||
} catch (error) {
|
||||
expect(error.message).to.contain('age 解密私钥');
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -7,6 +7,7 @@ import registerSettingsRoutes, {
|
||||
getGithubAvatarApiUrl,
|
||||
shouldRefreshArtifactStoreForSettingsPatch,
|
||||
} from '@/restful/settings';
|
||||
import { AGE_SECRET_KEY, generateKeyPair } from '@/utils/age';
|
||||
|
||||
function createRouteApp() {
|
||||
const handlers = new Map();
|
||||
@@ -115,6 +116,161 @@ describe('settings routes', function () {
|
||||
});
|
||||
});
|
||||
|
||||
describe('age key settings', function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
|
||||
afterEach(function () {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
});
|
||||
|
||||
async function patchSettings(initialSettings, body) {
|
||||
const state = {
|
||||
[SETTINGS_KEY]: initialSettings,
|
||||
};
|
||||
$.read = (key) => state[key];
|
||||
$.write = (data, key) => {
|
||||
state[key] = data;
|
||||
return true;
|
||||
};
|
||||
|
||||
const app = createRouteApp();
|
||||
registerSettingsRoutes(app);
|
||||
const patchHandler = app.handlers.get('PATCH /api/settings');
|
||||
const res = createResponse('/api/settings');
|
||||
|
||||
await patchHandler({ body }, res);
|
||||
|
||||
return {
|
||||
res,
|
||||
settings: state[SETTINGS_KEY],
|
||||
};
|
||||
}
|
||||
|
||||
it('does not validate existing age-secret-key on unrelated patches', async function () {
|
||||
const { res, settings } = await patchSettings(
|
||||
{
|
||||
gistUpload: 'age',
|
||||
[AGE_SECRET_KEY]: '恢复后请重新设置 age 解密私钥',
|
||||
logsMaxCount: 1,
|
||||
},
|
||||
{ logsMaxCount: 2 },
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings.logsMaxCount).to.equal(2);
|
||||
expect(settings[AGE_SECRET_KEY]).to.equal(
|
||||
'恢复后请重新设置 age 解密私钥',
|
||||
);
|
||||
});
|
||||
|
||||
it('does not validate submitted age secret key outside age mode', async function () {
|
||||
const { res, settings } = await patchSettings(
|
||||
{},
|
||||
{
|
||||
gistUpload: 'base64',
|
||||
[AGE_SECRET_KEY]: 'invalid-secret-key',
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings[AGE_SECRET_KEY]).to.equal('invalid-secret-key');
|
||||
});
|
||||
|
||||
it('accepts valid age secret key in age mode', async function () {
|
||||
const pair = await generateKeyPair();
|
||||
const { res, settings } = await patchSettings(
|
||||
{},
|
||||
{
|
||||
gistUpload: 'age',
|
||||
[AGE_SECRET_KEY]: ` ${pair[AGE_SECRET_KEY]} `,
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
expect(settings[AGE_SECRET_KEY]).to.equal(pair[AGE_SECRET_KEY]);
|
||||
});
|
||||
|
||||
it('requires age secret key in age mode', async function () {
|
||||
const { res } = await patchSettings({}, { gistUpload: 'age' });
|
||||
|
||||
expect(res.body.status).to.equal('failed');
|
||||
expect(res.body.error.message).to.contain(
|
||||
'age 加密模式需要配置 age-secret-key',
|
||||
);
|
||||
});
|
||||
|
||||
it('rejects invalid submitted age secret key in age mode', async function () {
|
||||
const { res } = await patchSettings(
|
||||
{},
|
||||
{
|
||||
gistUpload: 'age',
|
||||
[AGE_SECRET_KEY]: 'invalid-secret-key',
|
||||
},
|
||||
);
|
||||
|
||||
expect(res.body.status).to.equal('failed');
|
||||
expect(res.body.error.message).to.contain(
|
||||
'age-secret-key 仅支持',
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('appearance settings', function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
|
||||
afterEach(function () {
|
||||
$.read = originalRead;
|
||||
$.write = originalWrite;
|
||||
});
|
||||
|
||||
async function patchSettings(initialSettings, body) {
|
||||
const state = {
|
||||
[SETTINGS_KEY]: initialSettings,
|
||||
};
|
||||
$.read = (key) => state[key];
|
||||
$.write = (data, key) => {
|
||||
state[key] = data;
|
||||
return true;
|
||||
};
|
||||
|
||||
const app = createRouteApp();
|
||||
registerSettingsRoutes(app);
|
||||
const patchHandler = app.handlers.get('PATCH /api/settings');
|
||||
const res = createResponse('/api/settings');
|
||||
|
||||
await patchHandler({ body }, res);
|
||||
|
||||
expect(res.body.status).to.equal('success');
|
||||
return state[SETTINGS_KEY];
|
||||
}
|
||||
|
||||
it('merges appearanceSetting patches without dropping unrelated fields', async function () {
|
||||
const settings = await patchSettings(
|
||||
{
|
||||
appearanceSetting: {
|
||||
isSimpleMode: false,
|
||||
editorGroupingMode: 'always',
|
||||
editorCommonDisplayMode: 'collapsed',
|
||||
},
|
||||
},
|
||||
{
|
||||
appearanceSetting: {
|
||||
isSimpleMode: true,
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(settings.appearanceSetting).to.deep.equal({
|
||||
isSimpleMode: true,
|
||||
editorGroupingMode: 'always',
|
||||
editorCommonDisplayMode: 'collapsed',
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('backend request concurrency settings', function () {
|
||||
const originalRead = $.read.bind($);
|
||||
const originalWrite = $.write.bind($);
|
||||
|
||||
@@ -42,6 +42,20 @@ function normalizeAgePublicKeyConfig(config) {
|
||||
return config;
|
||||
}
|
||||
|
||||
async function normalizeAgeSecretKeyConfig(config) {
|
||||
if (!config || typeof config !== 'object') {
|
||||
return config;
|
||||
}
|
||||
|
||||
const key = normalizeKey(config[AGE_SECRET_KEY]);
|
||||
if (key) {
|
||||
config[AGE_SECRET_KEY] = await validateIdentity(key);
|
||||
} else {
|
||||
delete config[AGE_SECRET_KEY];
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
function isSupportedX25519Recipient(key) {
|
||||
return (
|
||||
key.startsWith('age1') &&
|
||||
@@ -396,6 +410,7 @@ export {
|
||||
maskAgeSecret,
|
||||
maskAgeSecretInUrl,
|
||||
normalizeAgePublicKeyConfig,
|
||||
normalizeAgeSecretKeyConfig,
|
||||
validateIdentity,
|
||||
validateRecipient,
|
||||
};
|
||||
@@ -56,12 +56,8 @@ export { describeGistApiErrorResponse };
|
||||
export default class Gist {
|
||||
constructor({ token, key, syncPlatform }) {
|
||||
const { isStash, isLoon, isShadowRocket, isQX } = ENV();
|
||||
const {
|
||||
defaultProxy,
|
||||
githubApiTimeout,
|
||||
githubProxy,
|
||||
githubApiUrl,
|
||||
} = $.read(SETTINGS_KEY);
|
||||
const { defaultProxy, githubApiTimeout, githubProxy, githubApiUrl } =
|
||||
$.read(SETTINGS_KEY);
|
||||
const githubApiRequestTimeout = githubApiTimeout || 10000;
|
||||
const githubGistBaseURL = getGithubGistBaseURL({
|
||||
githubApiUrl,
|
||||
|
||||
Reference in new issue
Block a user