mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-07-31 02:51:21 +08:00
fix(deeplink): import usage scripts disabled and show their code
An imported usage script is JavaScript that runs whenever usage is
queried. Two things made it possible to acquire one without seeing it:
- `usage_enabled.unwrap_or(!code.is_empty())` treated the presence of
code as a decision to run it, so a link that simply carried a script
got it enabled
- the confirmation dialog rendered only an enabled/disabled badge; the
script body was never displayed
Default to disabled. Enabling now requires `usageEnabled=true` in the
link -- which is the link author's request, not the user's consent. The
consent is the user pressing Import after seeing the full script body
and the badge, which is why both displays are load-bearing rather than
decorative.
The badge predicate moves from `!== false` to `=== true` to match the
new backend default. Left alone it would have started rendering "did not
say" as a green "Enabled" -- more optimistic than what would actually
happen.
Extracts the payload decode into `decodeDeeplinkPayload`, which falls
back to the raw string when decoding fails or yields empty. A dialog
whose job is to show what is about to be written must not let a payload
vanish just because it is malformed; empty reads as "there is no
script", which is exactly the wrong impression.
This commit is contained in:
@@ -20,9 +20,11 @@ import { ProviderIcon } from "./ProviderIcon";
|
||||
import {
|
||||
classifyEndpoint,
|
||||
classifyEnvKey,
|
||||
decodeDeeplinkPayload,
|
||||
maskValue,
|
||||
riskI18nKey,
|
||||
} from "@/utils/deeplinkRisk";
|
||||
import { decodeBase64Utf8 } from "@/lib/utils/base64";
|
||||
|
||||
interface DeeplinkError {
|
||||
url: string;
|
||||
@@ -649,14 +651,19 @@ export function DeepLinkImportDialog() {
|
||||
})}
|
||||
</div>
|
||||
<div className="col-span-2 text-sm">
|
||||
{/*
|
||||
判据是 `=== true`,与后端 `usage_enabled.unwrap_or(false)`
|
||||
严格对齐。此前用的 `!== false` 会把"链接没说"渲染成绿色的
|
||||
「已启用」——徽章必须显示实际会发生的事,不能比后端更乐观。
|
||||
*/}
|
||||
<span
|
||||
className={`inline-flex items-center px-2 py-0.5 rounded-md text-xs font-medium ${
|
||||
request.usageEnabled !== false
|
||||
request.usageEnabled === true
|
||||
? "bg-green-100 dark:bg-green-900/30 text-green-700 dark:text-green-300"
|
||||
: "bg-gray-100 dark:bg-gray-800 text-gray-600 dark:text-gray-400"
|
||||
}`}
|
||||
>
|
||||
{request.usageEnabled !== false
|
||||
{request.usageEnabled === true
|
||||
? t("deeplink.usageScriptEnabled", {
|
||||
defaultValue: "已启用",
|
||||
})
|
||||
@@ -667,6 +674,33 @@ export function DeepLinkImportDialog() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/*
|
||||
脚本正文必须完整展示。这段是会执行的 JavaScript,而 payload
|
||||
常常整条藏在中间——`whitespace-pre-wrap break-all` + 可滚动容器,
|
||||
不用 truncate,任何字符都不得被 CSS 藏起来。
|
||||
*/}
|
||||
<div className="space-y-1">
|
||||
<div className="font-medium text-sm text-muted-foreground">
|
||||
{t("deeplink.usageScriptCode")}
|
||||
</div>
|
||||
<pre className="max-h-48 overflow-auto rounded border border-border-default bg-muted/40 p-2 text-xs font-mono whitespace-pre-wrap break-all">
|
||||
{decodeDeeplinkPayload(
|
||||
request.usageScript,
|
||||
decodeBase64Utf8,
|
||||
)}
|
||||
</pre>
|
||||
</div>
|
||||
|
||||
{/*
|
||||
无条件显示,不看 `usageEnabled`:代码无论启用与否都会被写入供应商
|
||||
配置,用户之后在应用内一键即可开启。挂条件等于让攻击者省略参数就能
|
||||
关掉这条警告。
|
||||
*/}
|
||||
<div className="text-yellow-600 dark:text-yellow-500 text-sm flex items-start gap-2">
|
||||
<span aria-hidden="true">⚠️</span>
|
||||
<span>{t("deeplink.usageScriptWarning")}</span>
|
||||
</div>
|
||||
|
||||
{/* Usage API Key (if different from provider) */}
|
||||
{request.usageApiKey &&
|
||||
request.usageApiKey !== request.apiKey && (
|
||||
|
||||
@@ -2509,6 +2509,8 @@
|
||||
"usageScript": "Usage Query",
|
||||
"usageScriptEnabled": "Enabled",
|
||||
"usageScriptDisabled": "Disabled",
|
||||
"usageScriptCode": "Script code",
|
||||
"usageScriptWarning": "This is JavaScript that runs when usage is queried, once enabled. Import it only if you trust the source.",
|
||||
"usageApiKey": "Usage API Key",
|
||||
"usageBaseUrl": "Usage Query URL",
|
||||
"usageAutoInterval": "Auto Query",
|
||||
|
||||
@@ -2509,6 +2509,8 @@
|
||||
"usageScript": "使用量クエリ",
|
||||
"usageScriptEnabled": "有効",
|
||||
"usageScriptDisabled": "無効",
|
||||
"usageScriptCode": "スクリプトコード",
|
||||
"usageScriptWarning": "これは有効化すると使用量クエリ時に実行される JavaScript です。提供元が信頼できる場合のみインポートしてください。",
|
||||
"usageApiKey": "使用量 API キー",
|
||||
"usageBaseUrl": "使用量クエリ URL",
|
||||
"usageAutoInterval": "自動クエリ",
|
||||
|
||||
@@ -2480,6 +2480,8 @@
|
||||
"usageScript": "用量查詢",
|
||||
"usageScriptEnabled": "已啟用",
|
||||
"usageScriptDisabled": "未啟用",
|
||||
"usageScriptCode": "指令碼程式碼",
|
||||
"usageScriptWarning": "這是一段 JavaScript 程式碼,啟用後會在查詢用量時執行。請確認來源可信後再匯入。",
|
||||
"usageApiKey": "用量 API Key",
|
||||
"usageBaseUrl": "用量查詢位址",
|
||||
"usageAutoInterval": "自動查詢",
|
||||
|
||||
@@ -2509,6 +2509,8 @@
|
||||
"usageScript": "用量查询",
|
||||
"usageScriptEnabled": "已启用",
|
||||
"usageScriptDisabled": "未启用",
|
||||
"usageScriptCode": "脚本代码",
|
||||
"usageScriptWarning": "这是一段 JavaScript 代码,启用后会在查询用量时执行。请确认来源可信后再导入。",
|
||||
"usageApiKey": "用量 API Key",
|
||||
"usageBaseUrl": "用量查询地址",
|
||||
"usageAutoInterval": "自动查询",
|
||||
|
||||
@@ -3,6 +3,7 @@ import {
|
||||
classifyCommand,
|
||||
classifyEndpoint,
|
||||
classifyEnvKey,
|
||||
decodeDeeplinkPayload,
|
||||
maskValue,
|
||||
} from "./deeplinkRisk";
|
||||
|
||||
@@ -158,6 +159,38 @@ describe("classifyCommand", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("decodeDeeplinkPayload", () => {
|
||||
const ok = (v: string) => `decoded:${v}`;
|
||||
const boom = () => {
|
||||
throw new Error("bad base64");
|
||||
};
|
||||
|
||||
it("returns the decoded payload on success", () => {
|
||||
expect(decodeDeeplinkPayload("abc", ok)).toBe("decoded:abc");
|
||||
});
|
||||
|
||||
it("falls back to the raw string when decoding throws", () => {
|
||||
// 确认框必须展示即将写入的东西。解不开也要原样显示——返回空串会让整块
|
||||
// 内容消失,界面看起来像"没有脚本",那正是攻击者要的效果。
|
||||
expect(decodeDeeplinkPayload("!!!not-base64!!!", boom)).toBe(
|
||||
"!!!not-base64!!!",
|
||||
);
|
||||
});
|
||||
|
||||
it("falls back to the raw string when decoding yields empty", () => {
|
||||
// 解出空串同样可疑:不能让 payload 静默消失
|
||||
expect(decodeDeeplinkPayload("d293", () => "")).toBe("d293");
|
||||
});
|
||||
|
||||
it("returns empty for a non-string field instead of throwing", () => {
|
||||
// 该字段来自解码后的任意 JSON,形状不可信;抛错会让确认框整个渲染失败
|
||||
for (const hostile of [42, null, undefined, { a: 1 }, ["x"]]) {
|
||||
expect(() => decodeDeeplinkPayload(hostile, ok)).not.toThrow();
|
||||
expect(decodeDeeplinkPayload(hostile, ok)).toBe("");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("maskValue", () => {
|
||||
it("masks credential-shaped keys but keeps ordinary values readable", () => {
|
||||
expect(maskValue("ANTHROPIC_AUTH_TOKEN", "sk-ant-1234567890abcdef")).toBe(
|
||||
|
||||
@@ -211,3 +211,24 @@ export function maskValue(key: string, value: string): string {
|
||||
export function riskI18nKey(kind: RiskKind): string {
|
||||
return `deeplink.risk.${kind}`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 解码 deeplink 携带的 base64 载荷,供确认框展示。
|
||||
*
|
||||
* 解码失败时**回落到原始串,绝不返回空串**。确认框的职责是让用户看见即将写入
|
||||
* 什么;一段解不开的 payload 也必须以原样出现。返回空会让整块内容凭空消失,
|
||||
* 界面上看起来就像"没有脚本"——那正是攻击者想要的效果。
|
||||
*/
|
||||
export function decodeDeeplinkPayload(
|
||||
encoded: unknown,
|
||||
decode: (value: string) => string,
|
||||
): string {
|
||||
if (typeof encoded !== "string") return "";
|
||||
try {
|
||||
const decoded = decode(encoded);
|
||||
// 解出空串同样可疑:宁可显示原始 base64,也不显示"什么都没有"。
|
||||
return decoded === "" ? encoded : decoded;
|
||||
} catch {
|
||||
return encoded;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user