Skip to content

Commit 072d4a4

Browse files
committed
fix(claim): Send X-Device-Mid on the billing plane from the env fallback
Android injects the per-install device id as ZCODE_IDENTITY_DEVICE_MID and never writes identity.deviceMid into config.yaml, but the loader only read YAML — so the claim client sent no X-Device-Mid and the gateway answered biz 3001 "parameter error" on every claim POST. Preview survived only because the marketing face has its own derived fallback, which is why the app could enumerate plans and then fail to grab them. Observed on the Android app as `[claim] claim: invalid_request (3001) — parameter error` every 10 min while desktop/CLI claimed fine on the same account. - loader: resolve identity.deviceMid as env > YAML (blank counts as unset), matching the documented contract on ProxyIdentity and the order proxy/identity.ts already applies to the request headers, so the two planes agree on one fingerprint. - claim/runtime: add claimDeviceMid() and use it for BOTH the preview and claim clients — the billing face has no fallback of its own, so a config without identity.deviceMid must not reach it with the header omitted. - tests: env precedence / blank-env fallthrough in loader.test.ts, plus claim/runtime.test.ts for the shared-UUID contract.
1 parent 17efdcf commit 072d4a4

4 files changed

Lines changed: 102 additions & 5 deletions

File tree

‎src/claim/runtime.test.ts‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Tests for the claim-plane wiring helpers.
3+
*
4+
* `claimDeviceMid` exists because the billing gateway rejects every preview /
5+
* claim call that arrives without a UUID `X-Device-Mid` (biz 3001 "parameter
6+
* error", client.ts file header). A config with no `identity.deviceMid` used to
7+
* reach the claim POST with the header omitted — the observed Android symptom
8+
* was `[claim] claim: invalid_request (3001) — parameter error`, even though
9+
* the marketing face could still enumerate the plan via its own fallback.
10+
*/
11+
import { describe, it, expect } from "bun:test";
12+
import { claimDeviceMid } from "./runtime.js";
13+
import type { ProxyConfig } from "../config/types.js";
14+
15+
const UUID_V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/;
16+
17+
function configWith(deviceMid?: string): ProxyConfig {
18+
return {
19+
identity: {
20+
appVersion: "3.18.2",
21+
sourceTitle: "cli",
22+
refererOrigin: "https://zcode.z.ai",
23+
...(deviceMid !== undefined ? { deviceMid } : {}),
24+
},
25+
} as ProxyConfig;
26+
}
27+
28+
describe("claimDeviceMid", () => {
29+
it("uses the configured identity.deviceMid verbatim", () => {
30+
expect(claimDeviceMid(configWith("0d2c62ae-717c-4d88-9bbe-24c28fd45ed6")))
31+
.toBe("0d2c62ae-717c-4d88-9bbe-24c28fd45ed6");
32+
});
33+
34+
it("treats a blank/whitespace value as unset", () => {
35+
expect(claimDeviceMid(configWith(" "))).toMatch(UUID_V4);
36+
});
37+
38+
it("falls back to a stable UUIDv4 when the config carries none", () => {
39+
const first = claimDeviceMid(configWith());
40+
expect(first).toMatch(UUID_V4);
41+
// Fingerprint stability: upstream risk engines require one device id per
42+
// install — never randomize per call.
43+
expect(claimDeviceMid(configWith())).toBe(first);
44+
});
45+
});

‎src/claim/runtime.ts‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ function claimCaptchaRequired(config: ProxyConfig, resolveCredential: () => Prom
4141
.catch(() => true);
4242
}
4343

44+
/**
45+
* Device identity for the claim plane. Configured value (YAML, or the
46+
* `ZCODE_IDENTITY_DEVICE_MID` env NodeRunner injects on Android) first — the
47+
* gateway answers biz 3001 "parameter error" to any billing call whose
48+
* `X-Device-Mid` is missing or not a UUID (client.ts file header). The billing
49+
* face has no fallback of its own, so a config without `identity.deviceMid`
50+
* used to enumerate plans (marketing face falls back here) and then fail the
51+
* claim POST with 3001 — hence the shared derived fallback.
52+
*/
53+
export function claimDeviceMid(config: ProxyConfig): string {
54+
return config.identity.deviceMid?.trim() || stableMarketingDeviceMid();
55+
}
56+
4457
export function startAutoClaim(config: ProxyConfig, auth: AuthManager): ClaimScheduler {
4558
const scheduler = new ClaimScheduler({
4659
// AuthManager first (fresh), then the encrypted store — on Android the
@@ -59,8 +72,8 @@ export function startAutoClaim(config: ProxyConfig, auth: AuthManager): ClaimSch
5972
jwt,
6073
appVersion: config.identity.appVersion,
6174
platform: claimPlatform(),
62-
deviceMid: config.identity.deviceMid,
63-
marketingDeviceMid: config.identity.deviceMid ?? stableMarketingDeviceMid(),
75+
deviceMid: claimDeviceMid(config),
76+
marketingDeviceMid: claimDeviceMid(config),
6477
}),
6578
getCaptcha: async () => {
6679
const token = await getCaptchaToken(config.identity.appVersion);
@@ -112,8 +125,8 @@ export async function runClaimCli(config: ProxyConfig, mode: "list" | "now"): Pr
112125
jwt,
113126
appVersion: config.identity.appVersion,
114127
platform: claimPlatform(),
115-
deviceMid: config.identity.deviceMid,
116-
marketingDeviceMid: config.identity.deviceMid ?? stableMarketingDeviceMid(),
128+
deviceMid: claimDeviceMid(config),
129+
marketingDeviceMid: claimDeviceMid(config),
117130
});
118131

119132
let plans: ClaimablePlan[];

‎src/config/loader.test.ts‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ beforeEach(() => {
2323
delete process.env.ZCODE_PROVIDER;
2424
delete process.env.ZCODE_SOURCE_TITLE;
2525
delete process.env.ZCODE_REFERER_ORIGIN;
26+
delete process.env.ZCODE_IDENTITY_DEVICE_MID;
2627
delete process.env.ZCODE_ASYNC_ENABLED;
2728
delete process.env.ZCODE_ASYNC_ORIGIN;
2829
delete process.env.ZCODE_CLAIM_ENABLED;
@@ -82,6 +83,35 @@ identity:
8283
`);
8384
expect(loadConfig(path).identity.deviceMid).toBe("0f1e2d3c-4b5a-4978-8796-a5b4c3d2e1f0");
8485
});
86+
87+
// Android: NodeRunner injects the app-private UUID as an env var and never
88+
// writes identity.deviceMid into config.yaml — the claim plane reads the
89+
// resolved value, so dropping the env here made every claim POST hit biz
90+
// 3001 "parameter error" (missing X-Device-Mid) on the app.
91+
it("takes identity.deviceMid from ZCODE_IDENTITY_DEVICE_MID when YAML has none", () => {
92+
const path = writeYaml(`
93+
server:
94+
port: 9090
95+
provider: zai
96+
`);
97+
process.env.ZCODE_IDENTITY_DEVICE_MID = "0d2c62ae-717c-4d88-9bbe-24c28fd45ed6";
98+
expect(loadConfig(path).identity.deviceMid).toBe("0d2c62ae-717c-4d88-9bbe-24c28fd45ed6");
99+
});
100+
101+
it("prefers ZCODE_IDENTITY_DEVICE_MID over YAML (blank env falls through)", () => {
102+
const path = writeYaml(`
103+
server:
104+
port: 9090
105+
provider: zai
106+
identity:
107+
deviceMid: "0f1e2d3c-4b5a-4978-8796-a5b4c3d2e1f0"
108+
`);
109+
process.env.ZCODE_IDENTITY_DEVICE_MID = "11111111-2222-4333-8444-555555555555";
110+
expect(loadConfig(path).identity.deviceMid).toBe("11111111-2222-4333-8444-555555555555");
111+
112+
process.env.ZCODE_IDENTITY_DEVICE_MID = " ";
113+
expect(loadConfig(path).identity.deviceMid).toBe("0f1e2d3c-4b5a-4978-8796-a5b4c3d2e1f0");
114+
});
85115
});
86116

87117
describe("loadConfig", () => {

‎src/config/loader.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const ENV = {
1212
PROVIDER: "ZCODE_PROVIDER",
1313
SOURCE_TITLE: "ZCODE_SOURCE_TITLE",
1414
REFERER_ORIGIN: "ZCODE_REFERER_ORIGIN",
15+
IDENTITY_DEVICE_MID: "ZCODE_IDENTITY_DEVICE_MID",
1516
ASYNC_ENABLED: "ZCODE_ASYNC_ENABLED",
1617
ASYNC_ORIGIN: "ZCODE_ASYNC_ORIGIN",
1718
ASYNC_MAX_RETRIES: "ZCODE_ASYNC_MAX_RETRIES",
@@ -139,6 +140,7 @@ export function loadConfig(path: string): ProxyConfig {
139140
sourceTitleYaml: parsed?.identity?.sourceTitle,
140141
refererEnv: process.env[ENV.REFERER_ORIGIN],
141142
refererYaml: parsed?.identity?.refererOrigin,
143+
deviceMidEnv: process.env[ENV.IDENTITY_DEVICE_MID],
142144
deviceMidYaml: parsed?.identity?.deviceMid,
143145
});
144146

@@ -396,6 +398,7 @@ interface IdentityInputs {
396398
sourceTitleYaml?: string;
397399
refererEnv?: string;
398400
refererYaml?: string;
401+
deviceMidEnv?: string;
399402
deviceMidYaml?: string;
400403
}
401404

@@ -408,7 +411,13 @@ function resolveIdentity(inp: IdentityInputs): ProxyIdentity {
408411
const refererOrigin = (inp.refererEnv ?? inp.refererYaml ?? DEFAULTS.REFERER_ORIGIN).trim()
409412
|| DEFAULTS.REFERER_ORIGIN;
410413

411-
const deviceMid = typeof inp.deviceMidYaml === "string" ? inp.deviceMidYaml.trim() : "";
414+
// env > YAML (identity.ts applies the same order to the request headers, so
415+
// both planes agree on one fingerprint). Android has no deviceMid to persist
416+
// — NodeRunner injects ZCODE_IDENTITY_DEVICE_MID instead, and the claim plane
417+
// reads this resolved value (a missing X-Device-Mid is rejected as biz 3001).
418+
const deviceMid = [inp.deviceMidEnv, inp.deviceMidYaml]
419+
.map((v) => (typeof v === "string" ? v.trim() : ""))
420+
.find((v) => v.length > 0) ?? "";
412421
return { appVersion: DEFAULT_APP_VERSION, sourceTitle, refererOrigin, ...(deviceMid ? { deviceMid } : {}) };
413422
}
414423

0 commit comments

Comments
 (0)