Harden wallet challenge proxy

This commit is contained in:
2569718930@qq.com
2026-06-01 22:15:38 +08:00
parent 8fc9f3ae1a
commit fc0a8b8ff5
4 changed files with 127 additions and 21 deletions
@@ -44,6 +44,10 @@ export function runTests() {
);
const accountCenterSource = fs.readFileSync(accountCenterPath, "utf8");
const walletBindSource = fs.readFileSync(
path.join(projectRoot, "components", "account", "useWalletBind.ts"),
"utf8",
);
const hookPath = path.join(
projectRoot,
"components",
@@ -221,4 +225,25 @@ export function runTests() {
`${route} must allow bearer-backed payment mutations while still rejecting requests with no auth context`,
);
}
const walletChallengeRouteSource = fs.readFileSync(
path.join(projectRoot, "app/api/payments/wallets/challenge/route.ts"),
"utf8",
);
assert(
walletChallengeRouteSource.includes("fetchWalletChallengeWithRetry"),
"wallet challenge proxy must retry a transient backend connection failure before blocking payment",
);
assert(
walletChallengeRouteSource.includes("Invalid wallet challenge request"),
"wallet challenge proxy must return a client error for malformed JSON instead of a generic payment failure",
);
assert(
walletChallengeRouteSource.includes("retryable: true"),
"wallet challenge proxy exception response must mark transient failures as retryable",
);
assert(
walletBindSource.includes("readPaymentApiErrorMessage"),
"wallet binding errors must show the API error message instead of raw JSON",
);
}
@@ -68,6 +68,29 @@ export type NormalizedPaymentError = {
userRejected: boolean;
};
export async function readPaymentApiErrorMessage(
response: Response,
fallback = "Request failed",
limit = 300,
) {
const raw = (await response.text()).slice(0, limit);
if (!raw) return fallback;
try {
const parsed = JSON.parse(raw) as {
error?: unknown;
detail?: unknown;
message?: unknown;
};
const message = [parsed.error, parsed.detail, parsed.message].find(
(item) => typeof item === "string" && item.trim(),
);
if (typeof message === "string") return message.trim();
} catch {
// Fall back to the raw response body below.
}
return raw;
}
export function normalizePaymentError(error: unknown): NormalizedPaymentError {
const source = error as any;
const code = Number(
+9 -5
View File
@@ -16,7 +16,11 @@ import {
WALLET_TRANSACTION_REQUEST_TIMEOUT_MS,
} from "./constants";
import { shortAddress } from "./formatters";
import { normalizePaymentError, requestWalletWithTimeout } from "./payment-utils";
import {
normalizePaymentError,
readPaymentApiErrorMessage,
requestWalletWithTimeout,
} from "./payment-utils";
import {
eip6963Providers,
getEvmProvider,
@@ -300,8 +304,8 @@ export function useWalletBind(params: UseWalletBindParams) {
method: "POST", headers: authHeaders, body: JSON.stringify({ address }),
});
if (!challengeRes.ok) {
const raw = (await challengeRes.text()).slice(0, 300);
throw new Error(copy.challengeFailed.replace("{raw}", raw));
const message = await readPaymentApiErrorMessage(challengeRes);
throw new Error(copy.challengeFailed.replace("{raw}", message));
}
const challengeJson = (await challengeRes.json()) as { nonce?: string; message?: string };
@@ -314,8 +318,8 @@ export function useWalletBind(params: UseWalletBindParams) {
method: "POST", headers: authHeaders, body: JSON.stringify({ address, nonce, signature }),
});
if (!verifyRes.ok) {
const raw = (await verifyRes.text()).slice(0, 300);
throw new Error(copy.verifyFailedRaw.replace("{raw}", raw));
const message = await readPaymentApiErrorMessage(verifyRes);
throw new Error(copy.verifyFailedRaw.replace("{raw}", message));
}
setPaymentInfo(`${walletLabel} 绑定成功: ${shortAddress(address)}${binanceBindHint || "现在可点击“立即订阅并激活服务”。"}`);