Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(server): fetch a refused registration challenge once more instead of backing off

Maurice414a7ad98f99
diff --git a/server/src/nest/service-identity/issuer.client.ts b/server/src/nest/service-identity/issuer.client.ts
index b806ff6ba..bb581072d 100644
--- a/server/src/nest/service-identity/issuer.client.ts
+++ b/server/src/nest/service-identity/issuer.client.ts
@@ -18,7 +18,7 @@ const TIMEOUT_MS = 10_000;
/** Every answer of the issuer is a few hundred bytes. */
const MAX_BYTES = 4096;
-interface IssuerAnswer<T> {
+export interface IssuerAnswer<T> {
body: T;
/** The issuer's clock from its Date header. */
serverMs: number | null;
diff --git a/server/src/nest/service-identity/service-key.service.ts b/server/src/nest/service-identity/service-key.service.ts
index 83de1f132..15c2a317a 100644
--- a/server/src/nest/service-identity/service-key.service.ts
+++ b/server/src/nest/service-identity/service-key.service.ts
@@ -4,7 +4,7 @@ import { servicesConfig } from '../app-config/tokens';
import { logInfo, logWarn } from '../audit/audit-log.logger';
import { conflict } from '../common/domain-error';
import { withRequestContext } from '../database/request-context';
-import { IssuerClient, IssuerError } from './issuer.client';
+import { IssuerClient, IssuerError, type IssuerAnswer } from './issuer.client';
import { RefusalStreak } from './refusal-streak';
import { ServiceClock } from './service-clock';
import { SERVICE_ISSUER_ORIGIN } from './service-identity.constants';
@@ -246,10 +246,7 @@ export class ServiceKeyService {
private async register(): Promise<void> {
this.registering = true;
try {
- const challenge = await this.issuer.challenge();
- this.clock.learn(challenge.serverMs);
- const nonce = await solvePow(challenge.body, this.clock.toLocalMs(challenge.body.expires_at));
- const grant = await this.issuer.register({ c: challenge.body.c, nonce, v: runningVersion() });
+ const grant = await this.requestKey();
this.clock.learn(grant.serverMs);
const key = this.readGrant(grant.body);
const state: KeyState = { ...EMPTY_KEY_STATE, renewAfter: grant.body.renew_after };
@@ -269,6 +266,27 @@ export class ServiceKeyService {
}
}
+ /**
+ * A challenge is bound to the network that fetched it, and the registration
+ * may leave over the other address family. A refused challenge is therefore
+ * fetched once more at once instead of waiting out the backoff.
+ */
+ private async requestKey(): Promise<IssuerAnswer<ServiceKeyGrant>> {
+ try {
+ return await this.solveChallenge();
+ } catch (err) {
+ if (!(err instanceof IssuerError) || err.status !== 400) throw err;
+ return this.solveChallenge();
+ }
+ }
+
+ private async solveChallenge(): Promise<IssuerAnswer<ServiceKeyGrant>> {
+ const challenge = await this.issuer.challenge();
+ this.clock.learn(challenge.serverMs);
+ const nonce = await solvePow(challenge.body, this.clock.toLocalMs(challenge.body.expires_at));
+ return this.issuer.register({ c: challenge.body.c, nonce, v: runningVersion() });
+ }
+
/** Same installation, new expiry; the old key is the proof, so no proof of work. */
private async renew(key: ServiceKey): Promise<void> {
const renewal = await this.issuer.renew(key.token).then(
diff --git a/server/tests/unit/nest/service-identity/service-key.service.test.ts b/server/tests/unit/nest/service-identity/service-key.service.test.ts
index 8212e5d56..c7a847e5e 100644
--- a/server/tests/unit/nest/service-identity/service-key.service.test.ts
+++ b/server/tests/unit/nest/service-identity/service-key.service.test.ts
@@ -189,6 +189,26 @@ describe('registration', () => {
expect(issuer.challenge).toHaveBeenCalledTimes(2);
expect(keys.bearer()).toBe(KEY);
});
+
+ it('SIKEYS-007: a refused challenge is fetched once more at once, then left to the backoff', async () => {
+ const issuer = fakeIssuer();
+ issuer.register.mockRejectedValueOnce(new IssuerError(400, null, null));
+ const { keys } = await build({ issuer });
+ keys.bearer();
+ await settled(keys);
+ expect(issuer.challenge).toHaveBeenCalledTimes(2);
+ expect(keys.bearer()).toBe(KEY);
+
+ const refusing = fakeIssuer();
+ refusing.register.mockRejectedValue(new IssuerError(400, null, null));
+ await t?.close();
+ const again = await build({ issuer: refusing });
+ again.keys.bearer();
+ await settled(again.keys);
+ again.keys.bearer();
+ await settled(again.keys);
+ expect(refusing.challenge).toHaveBeenCalledTimes(2);
+ });
});
describe('when it never registers', () => {