Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(dashboard): cap an API key expiry at the year 9999 in UTC

Yudhi Armyndharisd1b74ec6d35a
diff --git a/dashboard/src/pages/ApiKeys.test.ts b/dashboard/src/pages/ApiKeys.test.ts
index ecf3bca5d..f336b7eb5 100644
--- a/dashboard/src/pages/ApiKeys.test.ts
+++ b/dashboard/src/pages/ApiKeys.test.ts
@@ -438,6 +438,44 @@ test('an expiry past the year 9999 blocks Create and Save with a message', async
assert.deepEqual(updateBodies, []);
});
+async function inTimeZone(zone: string, run: () => Promise<void>): Promise<void> {
+ const previous = process.env.TZ;
+ process.env.TZ = zone;
+ try {
+ await run();
+ } finally {
+ if (previous === undefined) delete process.env.TZ;
+ else process.env.TZ = previous;
+ }
+}
+
+// 9999-12-31T23:00 in New York is 10000-01-01T04:00Z, which serializes with a six-digit year the
+// gateway refuses, so the cap follows the browser's offset.
+test('west of UTC the expiry cap is the last minute of 9999 in UTC', () =>
+ inTimeZone('America/New_York', async () => {
+ const { screen, fireEvent } = rtl;
+ const create = await openCreate();
+ fireEvent.change(screen.getByLabelText('Name'), { target: { value: 'crm-bot' } });
+ const input = screen.getByLabelText<HTMLInputElement>('Expires at (optional)');
+ assert.equal(input.max, '9999-12-31T18:59');
+ fireEvent.change(input, { target: { value: '9999-12-31T23:00' } });
+ fireEvent.click(create);
+ await screen.findByText(invalidExpiry);
+ assert.equal(createBody, undefined);
+ }));
+
+// A 9999-12-31T23:59:59Z expiry reads as 10000-01-01T06:59 in Jakarta, past the input's max.
+test('east of UTC a stored expiry past the cap does not block an unrelated edit', () =>
+ inTimeZone('Asia/Jakarta', async () => {
+ const { screen, fireEvent, waitFor } = rtl;
+ const save = await openEdit({ ...billingBot, expiresAt: '9999-12-31T23:59:59.000Z' });
+ assert.equal(screen.getByLabelText<HTMLInputElement>('Expires at (optional)').max, '9999-12-31T23:59');
+ fireEvent.change(screen.getByLabelText('Role'), { target: { value: 'viewer' } });
+ fireEvent.click(save);
+ await waitFor(() => assert.equal(updateBodies.length, 1));
+ assert.deepEqual(updateBodies[0], { role: 'viewer' });
+ }));
+
test('an expiry Save cannot convert ends in an error toast, not a silent no-op', async () => {
const { screen, fireEvent } = rtl;
const save = await openEdit(billingBot);
diff --git a/dashboard/src/pages/ApiKeys.tsx b/dashboard/src/pages/ApiKeys.tsx
index f1bc5a8fe..08769b1f2 100644
--- a/dashboard/src/pages/ApiKeys.tsx
+++ b/dashboard/src/pages/ApiKeys.tsx
@@ -47,6 +47,7 @@ import {
parseScopeList,
sameSessionScope,
sessionScopeNames,
+ toDateTimeLocal,
type ApiKeyDraft,
} from '../utils/sessionScope';
import './ApiKeys.css';
@@ -80,6 +81,14 @@ function limitErrors(
};
}
+// The latest expiry the gateway takes: a UTC year past 9999 serializes with a six-digit year that it
+// refuses. West of UTC that is earlier than 9999-12-31T23:59 local. East of it the UTC cap falls in the
+// local year 10000, which new Date() cannot parse, so the local cap (still in 9999 UTC) is kept.
+function expiryMax(): string {
+ const utcCap = toDateTimeLocal('9999-12-31T23:59:00.000Z');
+ return utcCap.startsWith('9999-') ? utcCap : '9999-12-31T23:59';
+}
+
// IP, chat and expiry limits shared by the create and edit modals. Chats are offered only where
// sessions are: admin keys stay unscoped in the dashboard.
function KeyLimitFields({
@@ -149,7 +158,7 @@ function KeyLimitFields({
id={`${idPrefix}-expires`}
ref={expiresRef}
type="datetime-local"
- max="9999-12-31T23:59"
+ max={expiryMax()}
value={expires}
disabled={disabled}
onChange={e => {
@@ -278,7 +287,10 @@ export function ApiKeys() {
const handleSave = async () => {
if (!editingKey || !editDraft || !canSave) return;
- if (editExpiresRef.current && !editExpiresRef.current.validity.valid) {
+ // A stored expiry left as it is is not sent (see apiKeyPatch), so one past this browser's max (set
+ // from another time zone or through the API) does not block an unrelated change.
+ const expiryUntouched = editDraft.expires !== '' && editDraft.expires === toDateTimeLocal(editingKey.expiresAt);
+ if (!expiryUntouched && editExpiresRef.current && !editExpiresRef.current.validity.valid) {
toast.error(t('apiKeys.edit.title'), t('apiKeys.expiry.invalid'));
return;
}