Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase auth nonce entropy #1383

Merged
merged 2 commits into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/datasources/auth-api/siwe-api.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,24 @@ import { verifyMessage } from 'viem';

@Injectable()
export class SiweApi implements IAuthApi {
private static readonly NONCE_LENGTH = 8;
/**
* The official SiWe implementation uses a nonce length of 17:
*
* > 96 bits has been chosen as a number to sufficiently balance size and security
* > considerations relative to the lifespan of it's usage.
*
* ```
* const ALPHANUMERIC = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
* const length = Math.ceil(96 / (Math.log(ALPHANUMERIC.length) / Math.LN2)) // 17
* ```
*
* @see https://github.com/spruceid/siwe/blob/0e63b05cd3c722abd282dd1128aa8878648a8620/packages/siwe/lib/utils.ts#L36-L53
* @see https://github.com/StableLib/stablelib/blob/5243520e343c217b6a751464dec1bc980cb510d8/packages/random/random.ts#L80-L99
*
* As we rely on typed arrays to generate random values, we must use an even number.
* We therefore use a length of 18 to be compatible and remain as similar as possible.
*/
private static readonly NONCE_LENGTH = 18;

constructor(
@Inject(LoggingService)
Expand Down