Skip to content

Commit bcee386

Browse files
authored
Sessions pages fix (#1813)
1 parent a3beca0 commit bcee386

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

pages/sessions/basic.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ The secret hash is stored as a raw binary value. You can hex- or base64-encode i
2828

2929
```
3030
CREATE TABLE session (
31-
id TEXT NOT NULL PRIMARY KEY,
32-
secret_hash BLOB NOT NULL, -- blob is a SQLite data type for raw binary
33-
created_at INTEGER NOT NULL -- unix time (seconds)
31+
id TEXT NOT NULL PRIMARY KEY,
32+
secret_hash BLOB NOT NULL, -- blob is a SQLite data type for raw binary
33+
created_at INTEGER NOT NULL -- unix time (seconds)
3434
) STRICT;
3535
```
3636

@@ -47,7 +47,7 @@ Since these strings will be used as secrets as well, it's crucial to use a crypt
4747
```ts
4848
function generateSecureRandomString(): string {
4949
// Human readable alphabet (a-z, 0-9 without l, o, 0, 1 to avoid confusion)
50-
const alphabet = "abcdefghijklmnpqrstuvwxyz23456789";
50+
const alphabet = "abcdefghijkmnpqrstuvwxyz23456789";
5151

5252
// Generate 24 bytes = 192 bits of entropy.
5353
// We're only going to use 5 bits per byte so the total entropy will be 192 * 5 / 8 = 120 bits
@@ -56,7 +56,7 @@ function generateSecureRandomString(): string {
5656

5757
let id = "";
5858
for (let i = 0; i < bytes.length; i++) {
59-
// >> 3 s"removes" the right-most 3 bits of the byte
59+
// >> 3 "removes" the right-most 3 bits of the byte
6060
id += alphabet[bytes[i] >> 3];
6161
}
6262
return id;
@@ -125,13 +125,16 @@ async function createSession(dbPool: DBPool): Promise<SessionWithToken> {
125125

126126
async function validateSessionToken(dbPool: DBPool, token: string): Promise<Session | null> {
127127
const tokenParts = token.split(".");
128-
if (tokenParts.length != 2) {
128+
if (tokenParts.length !== 2) {
129129
return null;
130130
}
131131
const sessionId = tokenParts[0];
132132
const sessionSecret = tokenParts[1];
133133

134134
const session = await getSession(dbPool, sessionId);
135+
if (!session) {
136+
return null;
137+
}
135138

136139
const tokenSecretHash = await hashSecret(sessionSecret);
137140
const validSecret = constantTimeEqual(tokenSecretHash, session.secretHash);

pages/sessions/inactivity-timeout.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ interface Session {
2121

2222
```
2323
CREATE TABLE session (
24-
id TEXT NOT NULL PRIMARY KEY,
25-
secret_hash BLOB NOT NULL,
26-
last_verified_at INTEGER NOT NULL, -- unix (seconds)
27-
created_at INTEGER NOT NULL,
24+
id TEXT NOT NULL PRIMARY KEY,
25+
secret_hash BLOB NOT NULL,
26+
last_verified_at INTEGER NOT NULL, -- unix (seconds)
27+
created_at INTEGER NOT NULL,
2828
) STRICT;
2929
```
3030

@@ -40,13 +40,16 @@ async function validateSessionToken(dbPool: DBPool, token: string): Promise<Sess
4040
const now = new Date();
4141

4242
const tokenParts = token.split(".");
43-
if (tokenParts.length != 2) {
43+
if (tokenParts.length !== 2) {
4444
return null;
4545
}
4646
const sessionId = tokenParts[0];
47-
const sessionSecret = tokensParts[1];
47+
const sessionSecret = tokenParts[1];
4848

4949
const session = await getSession(dbPool, sessionId);
50+
if (!session) {
51+
return null;
52+
}
5053

5154
const tokenSecretHash = await hashSecret(sessionSecret);
5255
const validSecret = constantTimeEqual(tokenSecretHash, session.secretHash);
@@ -86,7 +89,7 @@ async function getSession(dbPool: DBPool, sessionId: string): Promise<Session |
8689

8790
// Inactivity timeout
8891
if (now.getTime() - session.lastVerifiedAt.getTime() >= inactivityTimeoutSeconds * 1000) {
89-
await deleteSession(sessionId);
92+
await deleteSession(dbPool, sessionId);
9093
return null;
9194
}
9295

0 commit comments

Comments
 (0)