Skip to content

Commit b154c20

Browse files
committed
fix(daemon): preserve unauthorized RPC code
1 parent 1a285e2 commit b154c20

3 files changed

Lines changed: 8 additions & 12 deletions

File tree

‎src/daemon/server/http-instance-precondition.test.ts‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
skipWhenLoopbackUnavailable,
1212
} from '../../__tests__/test-utils/loopback.ts';
1313

14-
type RpcErrorResponse = { error?: { data?: { code?: string } } };
14+
type RpcErrorResponse = { error?: { code?: number; data?: { code?: string } } };
1515

1616
test('a stale RPC instance is refused after authentication and before command dispatch', async (t) => {
1717
if (await skipWhenLoopbackUnavailable(t)) return;
@@ -45,10 +45,9 @@ test('a stale RPC instance is refused after authentication and before command di
4545
const unauthorized = await rpc('previous-instance', 'wrong-token');
4646
assert.equal(unauthorized.status, 401);
4747
assert.equal(unauthorized.headers.get(DAEMON_HTTP_INSTANCE_MISMATCH_HEADER), null);
48-
assert.equal(
49-
((await unauthorized.json()) as RpcErrorResponse).error?.data?.code,
50-
'UNAUTHORIZED',
51-
);
48+
const unauthorizedBody = (await unauthorized.json()) as RpcErrorResponse;
49+
assert.equal(unauthorizedBody.error?.code, -32000);
50+
assert.equal(unauthorizedBody.error?.data?.code, 'UNAUTHORIZED');
5251
const stale = await rpc('previous-instance');
5352
assert.equal(stale.status, 409);
5453
assert.equal(stale.headers.get(DAEMON_HTTP_INSTANCE_MISMATCH_HEADER), 'true');

‎src/daemon/server/http-server.ts‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ import {
3636
} from '../../request-progress-protocol.ts';
3737
import {
3838
buildDaemonHealthPayload,
39-
DAEMON_HTTP_INSTANCE_HEADER,
4039
DAEMON_HTTP_NETWORK_ACCESS_HEADER,
4140
DAEMON_HTTP_PUBLIC_NETWORK_ACCESS,
4241
DAEMON_HTTP_TENANT_HEADER,
@@ -48,6 +47,7 @@ import { tryHandleUploadHttpRoute } from '../upload-http.ts';
4847
import { tryHandleDownloadableArtifactHttpRoute } from '../downloadable-artifact-http.ts';
4948
import { tryHandleRequestDiagnosticsHttpRoute } from '../request-diagnostics-http.ts';
5049
import { resolveTrustedTenant, tenantTrustRejectionError } from './tenant-trust.ts';
50+
import { refuseStaleDaemonInstance } from './http-instance-precondition.ts';
5151
import type { TenantSessionNamespace } from '../session-tenant-scope.ts';
5252
import { tryHandleHumanControlHttpRoute } from '../human-control-http.ts';
5353
import type { LeaseRegistry } from '../lease-registry.ts';
@@ -760,15 +760,12 @@ export async function createDaemonHttpServer(options: {
760760
if (tokenError) {
761761
sendJson(
762762
res,
763-
createRpcError(rpcRequest.id ?? null, -32001, tokenError.message, tokenError),
763+
createRpcError(rpcRequest.id ?? null, -32000, tokenError.message, tokenError),
764764
401,
765765
);
766766
return;
767767
}
768-
if (req.headers[DAEMON_HTTP_INSTANCE_HEADER]) {
769-
const { refuseStaleDaemonInstance } = await import('./http-instance-precondition.ts');
770-
if (refuseStaleDaemonInstance(req, res, rpcRequest.id ?? null, instanceId)) return;
771-
}
768+
if (refuseStaleDaemonInstance(req, res, rpcRequest.id ?? null, instanceId)) return;
772769
daemonRequest.meta = {
773770
...daemonRequest.meta,
774771
tenantId: tenantTrust.tenantId,

‎test/wire-compat/ledger.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@
270270
{
271271
"declaration": "src/daemon-client/daemon-client-transport.ts#readDaemonHttpHealth",
272272
"digest": "sha256:6b64ae9b8e461a4b7dc1afb88465fcbaef908c31cc0be24499b2f440616e1cb5",
273-
"rationale": "#2650 bounds a retry's health probe by the RPC's remaining deadline, including response-body reads. The health request and accepted payload fields are unchanged; only a stalled probe ends sooner when the command deadline is near."
273+
"rationale": "#2650 gives every HTTP health probe a total-time cap (3s remote or 500ms local), including response-body reads. A restart retry also uses the RPC's remaining deadline. The health request and accepted payload fields are unchanged; stalled probes now end sooner."
274274
},
275275
{
276276
"declaration": "src/daemon-client/daemon-client-transport.ts#readRemoteDaemonHealth",

0 commit comments

Comments
 (0)