fix(server): solve the proof of work and read the revocation notice without awaiting in a loop

The proof of work and the bounded read of a 403 body recurse instead of looping, the background-work checks compare against null instead of testing a promise, and the tests repeat their requests through oneByOne. The renewal test loads the stored key before its first request, and the panel test picks the last confirm button without Array.prototype.at.
This commit is contained in:
Maurice
2026-10-29 09:30:13 +01:00
committed by Maurice
parent 414a7ad98f
commit eeae904867
7 changed files with 51 additions and 39 deletions
@@ -29,10 +29,13 @@ export function leadingZeroBits(hash: Uint8Array): number {
}
/** The first decimal nonce whose hash has at least `bits` leading zero bits, tried until `deadlineMs`. */
export async function solvePow(challenge: ServiceChallenge, deadlineMs: number): Promise<string> {
for (let nonce = 0; Date.now() < deadlineMs; nonce++) {
const hash = await powHash(challenge.c, String(nonce), challenge);
if (leadingZeroBits(hash) >= challenge.bits) return String(nonce);
}
throw new Error('the registration challenge expired before it was solved');
export function solvePow(challenge: ServiceChallenge, deadlineMs: number): Promise<string> {
return firstNonceFrom(0, challenge, deadlineMs);
}
async function firstNonceFrom(nonce: number, challenge: ServiceChallenge, deadlineMs: number): Promise<string> {
if (Date.now() >= deadlineMs) throw new Error('the registration challenge expired before it was solved');
const hash = await powHash(challenge.c, String(nonce), challenge);
if (leadingZeroBits(hash) >= challenge.bits) return String(nonce);
return firstNonceFrom(nonce + 1, challenge, deadlineMs);
}
@@ -154,7 +154,7 @@ export class ServiceKeyService {
/** Test seam: resolves once the background work queued so far has run. */
async whenIdle(): Promise<void> {
const work = this.work;
if (!work) return;
if (work === null) return;
await work;
await this.whenIdle();
}
@@ -201,7 +201,7 @@ export class ServiceKeyService {
}
private settleInBackground(): void {
if (this.work || (this.loaded && !this.wantsRegistration() && !this.keyToRenewNow())) return;
if (this.work !== null || (this.loaded && !this.wantsRegistration() && !this.keyToRenewNow())) return;
void this.inBackground(async () => {
await this.load();
if (this.wantsRegistration()) {
@@ -61,13 +61,20 @@ async function saysRevoked(response: Response): Promise<boolean> {
async function readUpTo(response: Response, maxChars: number): Promise<string> {
const reader = response.body?.getReader();
if (!reader) return '';
const decoder = new TextDecoder();
let text = '';
while (text.length <= maxChars) {
const { done, value } = await reader.read();
if (done) return text;
text += decoder.decode(value, { stream: true });
}
await reader.cancel();
return text;
return readOn(reader, new TextDecoder(), '', maxChars);
}
async function readOn(
reader: ReadableStreamDefaultReader<Uint8Array>,
decoder: TextDecoder,
text: string,
maxChars: number,
): Promise<string> {
if (text.length > maxChars) {
await reader.cancel();
return text;
}
const { done, value } = await reader.read();
if (done) return text;
return readOn(reader, decoder, text + decoder.decode(value, { stream: true }), maxChars);
}