Skip to content

Commit

Permalink
use secure random value for challenge
Browse files Browse the repository at this point in the history
  • Loading branch information
finn-block committed Nov 16, 2023
1 parent b0941a7 commit 760a16d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 15 deletions.
13 changes: 2 additions & 11 deletions src/pow.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHash } from 'crypto';
import { createHash, randomBytes } from 'crypto';
import type { Request, Response } from 'express';
import type { Express } from 'express';
import type { Dialect } from 'kysely';
Expand Down Expand Up @@ -122,17 +122,8 @@ export class ProofOfWork {
}
}

const challengeCharacters =
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';

function generateChallenge(): string {
let challenge = '';
while (challenge.length < 10) {
challenge += challengeCharacters.charAt(
Math.floor(Math.random() * challengeCharacters.length),
);
}
return challenge;
return randomBytes(10).toString('base64');
}

interface AuthorizedTenants {
Expand Down
8 changes: 4 additions & 4 deletions tests/http-api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe('http api', function () {
challenge: string;
complexity: number;
};
expect(body.challenge.length).to.equal(10);
expect(body.challenge.length).to.equal(16);
expect(body.complexity).to.equal(5);
});

Expand All @@ -83,7 +83,7 @@ describe('http api', function () {
challenge: string;
complexity: number;
};
expect(body.challenge.length).to.equal(10);
expect(body.challenge.length).to.equal(16);
expect(body.complexity).to.equal(5);

// solve the challenge
Expand Down Expand Up @@ -119,7 +119,7 @@ describe('http api', function () {
challenge: string;
complexity: number;
};
expect(body.challenge.length).to.equal(10);
expect(body.challenge.length).to.equal(16);

// solve the challenge
let response = '';
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('http api', function () {
challenge: string;
complexity: number;
};
expect(body.challenge.length).to.equal(10);
expect(body.challenge.length).to.equal(16);

// generate a nonce
let response = generateNonce(5);
Expand Down

0 comments on commit 760a16d

Please sign in to comment.