Skip to content

Commit 52d9b62

Browse files
wtfsayoclaude
andcommitted
fix(quota): reject a Devin status with no plan or a malformed plan-end timestamp
- A present plan-end timestamp that fails to decode now fails the whole status instead of reading as absent, so a malformed response keeps the last-good row rather than publishing an authoritative-empty one. - A status with neither the top-level PlanInfo nor the nested PlanStatus plan is rejected instead of decoded into a zero-valued plan, which could read as a credit-billed account with every credit spent. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 9e23686 commit 52d9b62

2 files changed

Lines changed: 34 additions & 3 deletions

File tree

‎src/providers/quota/devin.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,16 @@ function sub(fields: Fields, num: number): Buffer | undefined {
8989
return Buffer.isBuffer(value) ? value : undefined;
9090
}
9191

92-
function timestampMs(buf: Buffer | undefined): number | undefined {
93-
const f = buf ? fieldsOf(buf) : null;
94-
return f ? epochMillis(int(f, 1)) : undefined;
92+
/** Undefined for an absent timestamp; null for one that is present but malformed. */
93+
function timestampMs(buf: Buffer | undefined): number | undefined | null {
94+
if (!buf) return undefined;
95+
const f = fieldsOf(buf);
96+
return f ? epochMillis(int(f, 1)) : null;
9597
}
9698

99+
/** Null when neither plan copy is present: a zero-valued plan would read as an exhausted account. */
97100
function decodePlanInfo(buf: Buffer | undefined): DevinPlanInfo | null {
101+
if (!buf) return null;
98102
const f = fieldsOf(buf);
99103
if (!f) return null;
100104
return {
@@ -120,6 +124,7 @@ export function decodeDevinUserStatus(buf: Buffer): DevinUserStatus | null {
120124
if (!plan) return null;
121125
if (!plan.teamsTier) plan.teamsTier = int(user, 10);
122126
const planEndMs = timestampMs(sub(status, 3));
127+
if (planEndMs === null) return null;
123128
const dailyResetMs = epochMillis(int(status, 17));
124129
const weeklyResetMs = epochMillis(int(status, 18));
125130
return {

‎tests/providers/devin-quota.test.ts‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,21 @@ describe("Devin GetUserStatus decode and mapping", () => {
141141
expect(devinQuotaFromStatus(decodeDevinUserStatus(creditPlan({ 6: 0, 8: 0 }))!, 1).monthlyPercent).toBe(100);
142142
});
143143

144+
test("a status with no plan copy is not decoded into a zero-valued plan", () => {
145+
// Neither the top-level PlanInfo nor PlanStatus #1: a zero plan would read as an exhausted account.
146+
const planStatus = Buffer.concat([encodeMessage(3, int(1, PLAN_END)), int(6, 0), int(8, 0)]);
147+
const noPlan = encodeMessage(1, Buffer.concat([int(10, 1), encodeMessage(13, planStatus)]));
148+
expect(decodeDevinUserStatus(noPlan)).toBeNull();
149+
});
150+
151+
test("a present but truncated plan-end timestamp fails the decode instead of reading as absent", () => {
152+
const plan = planInfo({ tier: 16, name: "Pro", billing: 1 });
153+
// PlanStatus #3 holds a field declaring 127 bytes with none present.
154+
const planStatus = Buffer.concat([encodeMessage(1, plan), encodeMessage(3, Buffer.from([0x12, 0x7f])), int(6, 0), int(8, 0)]);
155+
const malformed = Buffer.concat([encodeMessage(1, Buffer.concat([int(10, 1), encodeMessage(13, planStatus)])), encodeMessage(2, plan)]);
156+
expect(decodeDevinUserStatus(malformed)).toBeNull();
157+
});
158+
144159
test("a response without PlanStatus is not a usable status", () => {
145160
expect(decodeDevinUserStatus(encodeMessage(1, encodeString(3, "Fixture User")))).toBeNull();
146161
expect(decodeDevinUserStatus(Buffer.alloc(0))).toBeNull();
@@ -189,6 +204,17 @@ describe("fetchDevinQuota transport", () => {
189204
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
190205
});
191206

207+
test("a missing plan or a truncated nested timestamp keeps the last-good row", async () => {
208+
const plan = planInfo({ tier: 16, name: "Pro", billing: 1 });
209+
const truncatedEnd = Buffer.concat([encodeMessage(1, plan), encodeMessage(3, Buffer.from([0x12, 0x7f])), int(6, 0), int(8, 0)]);
210+
const noPlan = Buffer.concat([encodeMessage(3, int(1, PLAN_END)), int(6, 0), int(8, 0)]);
211+
for (const planStatus of [truncatedEnd, noPlan]) {
212+
const body = encodeMessage(1, Buffer.concat([int(10, 1), encodeMessage(13, planStatus)]));
213+
globalThis.fetch = (async () => new Response(new Uint8Array(body), { status: 200 })) as unknown as typeof fetch;
214+
expect(await fetchDevinQuota("devin", KEY, undefined)).toBeNull();
215+
}
216+
});
217+
192218
test("a complete field followed by a truncated one is malformed, not authoritative-empty", async () => {
193219
// An empty UserStatus, then field 2 declaring 127 bytes with none present.
194220
globalThis.fetch = (async () => new Response(new Uint8Array([0x0a, 0x02, 0x6a, 0x00, 0x12, 0x7f]), { status: 200 })) as unknown as typeof fetch;

0 commit comments

Comments
 (0)