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

Next v0.3.0 #21

Draft
wants to merge 139 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
Show all changes
139 commits
Select commit Hold shift + click to select a range
d9bc422
init
jgoux Aug 6, 2024
e5e5418
simplify
jgoux Aug 6, 2024
81d13bd
fix salt
jgoux Aug 6, 2024
8a6a596
fix sendAuthentictionSASL
jgoux Aug 6, 2024
540639a
getting closer
jgoux Aug 6, 2024
bcfc264
read clientFinalMessage correctly
jgoux Aug 6, 2024
06b3796
it works!
jgoux Aug 6, 2024
ebaf371
refactor
jgoux Aug 6, 2024
d289bf0
use biome
jgoux Aug 6, 2024
1e3d59e
lock
jgoux Aug 6, 2024
159939b
call validateCredentials for the certificate auth mode
jgoux Aug 6, 2024
ddbac77
delete extra types
jgoux Aug 6, 2024
0a7cb3c
use supabase styles
jgoux Aug 7, 2024
60df052
align sasl workflow with pg server
jgoux Aug 7, 2024
3d0425e
use biome and tsconfig everywhere
jgoux Aug 7, 2024
16a2c26
fix types
jgoux Aug 7, 2024
61a6486
refactor incoming
jgoux Aug 7, 2024
1bc57ca
big refactor
jgoux Aug 7, 2024
fdf0e34
remove commented code
jgoux Aug 7, 2024
46bc054
rename types
jgoux Aug 7, 2024
bdc951a
remove util
jgoux Aug 7, 2024
30ef55d
rename type
jgoux Aug 7, 2024
5a01051
isolate buffer logic
jgoux Aug 7, 2024
4dfdcab
isolate TLS
jgoux Aug 7, 2024
1a641df
address comments
jgoux Aug 8, 2024
322451e
export BackendError
jgoux Aug 8, 2024
25764a5
isolate all the auth flow
jgoux Aug 8, 2024
f488d5d
add all the auth examples
jgoux Aug 8, 2024
2e66022
put regular pg port back
jgoux Aug 8, 2024
64da415
remove logs
jgoux Aug 8, 2024
b8c8b37
harmonize how the password is provided
jgoux Aug 8, 2024
ff64552
memoize for scram
jgoux Aug 8, 2024
3f428aa
fix cert flow
jgoux Aug 8, 2024
2ec114b
pass ConnectionState in all callbacks
jgoux Aug 8, 2024
9a301a3
Update examples/pglite-auth/package.json
jgoux Aug 8, 2024
0fe04a0
Update examples/pglite-auth/cert.ts
jgoux Aug 8, 2024
39a77ee
apply comment
jgoux Aug 8, 2024
716c1d9
apply password naming change
jgoux Aug 8, 2024
8a2554d
Update examples/pglite-auth/scram-sha-256.ts
jgoux Aug 8, 2024
be9649d
Update examples/pglite-auth/scram-sha-256.ts
jgoux Aug 8, 2024
e1d19ff
Update examples/pglite-auth/trust.ts
jgoux Aug 8, 2024
7a0bb68
remove the possibility to pass a salt
jgoux Aug 8, 2024
82a1310
pause/resume socket
jgoux Aug 8, 2024
11c0833
Merge pull request #1 from jgoux/feat/sasl
gregnr Aug 8, 2024
c1a3fdf
v0.3.0-alpha.1
gregnr Aug 8, 2024
4d7fa70
internalize pg-protocol buffer classes
jgoux Aug 9, 2024
fac5e4d
add missing comment
jgoux Aug 9, 2024
a7207d5
fix lock
jgoux Aug 9, 2024
207f27e
Merge pull request #2 from jgoux/chore-internalize-buffer
gregnr Aug 9, 2024
9ec4acb
v0.3.0-alpha.2
gregnr Aug 9, 2024
4cc47d3
chore: bump pglite
jgoux Aug 9, 2024
e1fda9d
fix deps
jgoux Aug 9, 2024
b5394f8
Merge pull request #3 from jgoux/chore-bump-pglite
gregnr Aug 9, 2024
d961d53
v0.3.0-alpha.3
gregnr Aug 9, 2024
f75928e
fix error message
jgoux Aug 12, 2024
5b26685
fix: guard against startup message without ssl
jgoux Aug 12, 2024
9ce49b4
Merge pull request #5 from supabase-community/fix/guard-ssl-requirement
gregnr Aug 12, 2024
65597ba
v0.3.0-alpha.4
gregnr Aug 12, 2024
8a2ddda
fix workflow
jgoux Aug 14, 2024
c66835b
add detached state
jgoux Aug 14, 2024
2412e31
fix examples
jgoux Aug 14, 2024
6337051
Merge pull request #7 from supabase-community/fix/call-on-startup
jgoux Aug 14, 2024
29058dc
v0.3.0-alpha.5
gregnr Aug 14, 2024
988b9bb
chore: add callback form for serverVersion
jgoux Aug 14, 2024
5b6ea9c
feat: add callback form for serverVersion
jgoux Aug 14, 2024
f4efd16
v0.3.0-alpha.6
gregnr Aug 14, 2024
1c1bc46
docs: update readme with auth changes
gregnr Aug 19, 2024
0adfd95
feat: return response data from onMessage hook
gregnr Aug 19, 2024
b381741
Merge pull request #10 from supabase-community/feat/on-message-respon…
gregnr Aug 20, 2024
d2c774b
wip: web standard apis
gregnr Aug 28, 2024
28487ac
Merge branch 'next' into docs/update-readme
gregnr Aug 29, 2024
466b19e
chore: upgrade pglite to latest
gregnr Aug 29, 2024
c44aec8
chore: gitignore
gregnr Aug 29, 2024
e4d4373
Merge pull request #9 from supabase-community/docs/update-readme
gregnr Aug 29, 2024
4ce09a4
Merge branch 'next' into refactor/web-standard-apis
gregnr Aug 29, 2024
9f9cf88
feat: convert BufferWriter to use web buffers
gregnr Aug 29, 2024
044a192
feat: switch ArrayBuffer to Uint8Array
gregnr Aug 29, 2024
d7dce20
feat: basic deno test
gregnr Aug 29, 2024
0f25722
feat: basic node test
gregnr Aug 29, 2024
7a0ae0c
feat: in-memory sockets via duplex pairs
gregnr Aug 30, 2024
b5c31a7
feat: in-memory pg client stream
gregnr Aug 30, 2024
2ae0be1
refactor: pg pass-through socket utils
gregnr Aug 30, 2024
3cc4cc7
fix: reverse proxy example
gregnr Aug 30, 2024
dfcee11
fix: node test import
gregnr Aug 30, 2024
bf51714
feat: convert md5 ArrayBuffer to Uint8Array
gregnr Aug 30, 2024
513509d
feat: browser tests
gregnr Sep 5, 2024
cb8d9bf
feat: pglite extended query patch
gregnr Sep 5, 2024
6834ab6
feat: test pglite extended query patch with postgres.js
gregnr Sep 6, 2024
b11b8f5
refactor: duplicate code
gregnr Sep 6, 2024
d011e44
Merge branch 'feat/pglite-extended-query-patch' into refactor/web-sta…
gregnr Sep 6, 2024
fa25ff0
fix: missing test include dirs
gregnr Sep 6, 2024
be66873
fix: readable stream async iterator missing on safari
gregnr Sep 6, 2024
83697db
feat: remove readable stream polyfill
gregnr Sep 6, 2024
e78b867
chore: upgrade playwright
gregnr Sep 6, 2024
c3c560c
fix: remove logs
gregnr Sep 6, 2024
f311e89
chore: ci tests
gregnr Sep 6, 2024
451058a
ci: move playwright setup to a composite action
gregnr Sep 6, 2024
562077f
feat: handle cert auth method
gregnr Sep 10, 2024
673375a
fix
jgoux Sep 10, 2024
d9dc14f
Merge pull request #14 from supabase-community/feat/end-method
jgoux Sep 10, 2024
c1c704e
v0.3.0-alpha.7
gregnr Sep 10, 2024
095fbd1
Merge pull request #13 from supabase-community/chore/ci
gregnr Sep 10, 2024
9b578a0
feat: tls tests
gregnr Sep 10, 2024
86763de
chore: upgrade pglite to remove extended query patch
gregnr Sep 10, 2024
c6a7e8f
Merge branch 'next' into refactor/web-standard-apis
gregnr Sep 10, 2024
6895e1e
chore: log openssl stderr for better ci debugging
gregnr Sep 10, 2024
3bdbec7
fix: test cert generation to be pure js
gregnr Sep 11, 2024
bd2b321
chore: upgrade pglite to latest in all examples
gregnr Sep 11, 2024
8c8358f
chore: remove commented code
gregnr Sep 11, 2024
b3efdad
refactor: disposable clients/servers in tests
gregnr Sep 11, 2024
466c76d
chore: deno.lock update
gregnr Sep 11, 2024
7ef5ee8
refactor: cleaner server cleanup in tests
gregnr Sep 11, 2024
0237127
refactor: split signals into separate file
gregnr Sep 11, 2024
6d67444
Merge pull request #11 from supabase-community/refactor/web-standard-…
gregnr Sep 11, 2024
a8663e5
docs: update readme with web standard api changes
gregnr Sep 11, 2024
535251b
fix: incorrect import in node tls file
gregnr Sep 11, 2024
774bd96
feat: add test to detect pglite hang after error
gregnr Sep 12, 2024
e7a50b6
Merge pull request #15 from supabase-community/fix/node-upgrade-tls
gregnr Sep 12, 2024
249fc61
Merge pull request #16 from supabase-community/docs/web-standard-apis
gregnr Sep 12, 2024
4809cfc
v0.3.0-beta.1
gregnr Sep 12, 2024
9ad9f7a
fix: bundle @std deps since they come from jsr
gregnr Sep 12, 2024
422f030
chore: update package-lock.json
gregnr Sep 12, 2024
fc4e2c4
Merge pull request #18 from supabase-community/fix/bundle-deps
gregnr Sep 12, 2024
250ee0c
v0.3.0-beta.2
gregnr Sep 12, 2024
5691efd
error story
jgoux Sep 13, 2024
4f7fc24
tests
jgoux Sep 13, 2024
13fb14d
Merge pull request #19 from supabase-community/feat/throw-errors-and-end
gregnr Sep 13, 2024
3a2f613
v0.3.0-beta.3
gregnr Sep 13, 2024
7b6248d
docs: fix missing async in createServer callback
gregnr Sep 13, 2024
c7622c0
Merge branch 'next' into fix/pglite-error-hang
gregnr Sep 13, 2024
1efb930
chore: upgrade all versions of pglite
gregnr Sep 13, 2024
87f2096
chore: update pglite version in deno.lock
gregnr Sep 13, 2024
e1f300e
docs: fix note in pglite example now that we use trust auth method
gregnr Sep 13, 2024
69f6d69
docs: remove development note
gregnr Sep 13, 2024
561fa36
Merge pull request #20 from supabase-community/docs/fix-missing-async
gregnr Sep 14, 2024
0ed68f0
Merge pull request #17 from supabase-community/fix/pglite-error-hang
gregnr Sep 23, 2024
8fa27cd
fix: tsup cjs transpile errors
gregnr Dec 23, 2024
c7ccafb
Merge pull request #24 from supabase-community/fix/tsup-cjs-errors
gregnr Dec 23, 2024
8b89d7d
v0.3.0-beta.4
gregnr Dec 23, 2024
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
Prev Previous commit
Next Next commit
simplify
  • Loading branch information
jgoux committed Aug 6, 2024
commit e5e5418f3230077025b8786f794444bab048e74b
27 changes: 6 additions & 21 deletions packages/pg-gateway/src/connection.ts
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ import { createSecureContext, TLSSocket, TLSSocketOptions } from 'node:tls';
import { BufferReader } from 'pg-protocol/dist/buffer-reader';
import { Writer } from 'pg-protocol/dist/buffer-writer';
import { generateMd5Salt } from './util.js';
import { randomBytes, pbkdf2Sync, createHmac } from 'crypto';
import { randomBytes, pbkdf2Sync, createHmac, createHash, timingSafeEqual } from 'crypto';

export const enum FrontendMessageCode {
Query = 0x51, // Q
@@ -88,11 +88,7 @@ export type SaslCredentials = {
authMode: 'sasl';
user: string;
clientProof: Buffer;
salt: Buffer;
iterations: number;
nonce: string;
serverFirstMessage: string;
clientFinalMessageWithoutProof: string;
authMessage: string;
};

export type Credentials = CleartextPasswordCredentials | Md5PasswordCredentials | SaslCredentials;
@@ -132,10 +128,7 @@ export type PostgresConnectionOptions = {
* Callback should return `true` if credentials are valid and
* `false` if credentials are invalid.
*/
validateCredentials?(
credentials: Credentials,
state: State
): boolean | Promise<boolean> | { password: string } | Promise<{ password: string }>;
validateCredentials?(credentials: Credentials, state: State): boolean | Promise<boolean>;

/**
* Callback after the connection has been upgraded to TLS.
@@ -1057,19 +1050,13 @@ export default class PostgresConnection {
}

const { user } = this.clientInfo.parameters;
const [, saltBase64, iterationsStr] = this.saslServerFirstMessage.split(',');
const salt = Buffer.from(saltBase64.substring(2), 'base64');
const iterations = parseInt(iterationsStr.substring(2), 10);
const authMessage = `n=${user},r=${this.saslNonce},${this.saslServerFirstMessage},${clientFinalMessageWithoutProof}`;

const valid = await this.options.validateCredentials?.({
authMode: 'sasl',
user,
clientProof,
salt,
iterations,
nonce: this.saslNonce,
serverFirstMessage: this.saslServerFirstMessage,
clientFinalMessageWithoutProof,
authMessage,
}, this.state);

if (!valid) {
@@ -1079,9 +1066,7 @@ export default class PostgresConnection {
}

// Compute server signature
const saltedPassword = pbkdf2Sync(valid.password, salt, iterations, 32, 'sha256');
const serverKey = createHmac('sha256', saltedPassword).update('Server Key').digest();
const authMessage = `n=${user},r=${this.saslNonce},${this.saslServerFirstMessage},${clientFinalMessageWithoutProof}`;
const serverKey = createHmac('sha256', clientProof).update('Server Key').digest();
const serverSignature = createHmac('sha256', serverKey).update(authMessage).digest('base64');

const serverFinalMessage = `v=${serverSignature}`;
13 changes: 4 additions & 9 deletions packages/pg-gateway/src/util.ts
Original file line number Diff line number Diff line change
@@ -27,21 +27,16 @@ export function generateMd5Salt() {

export function verifySaslPassword(params: {
password: string,
user: string,
clientProof: Buffer,
salt: Buffer,
iterations: number,
nonce: string,
serverFirstMessage: string,
clientFinalMessageWithoutProof: string
clientProof: Buffer,
authMessage: string
}): boolean {
const { password, user, clientProof, salt, iterations, nonce, serverFirstMessage, clientFinalMessageWithoutProof } = params;
const { password, salt, iterations, clientProof, authMessage } = params;

const saltedPassword = pbkdf2Sync(password, salt, iterations, 32, 'sha256');
const clientKey = createHmac('sha256', saltedPassword).update('Client Key').digest();
const storedKey = createHash('sha256').update(clientKey).digest();

const authMessage = `n=${user},r=${nonce},${serverFirstMessage},${clientFinalMessageWithoutProof}`;

const clientSignature = createHmac('sha256', storedKey).update(authMessage).digest();
const computedClientProof = Buffer.alloc(clientSignature.length);
@@ -50,4 +45,4 @@ export function verifySaslPassword(params: {
}

return timingSafeEqual(clientProof, computedClientProof);
}
}