GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Add support for SCRAM-SHA-256-PLUS i.e. channel binding by jawj · Pull Request #3356 · brianc/node-postgres

Conversation

@jawj

@jawj jawj commented Jan 10, 2025

Copy link
Copy Markdown
Contributor

Hi all. I hope you'll consider this patch, which adds support for SCRAM-SHA-256-PLUS.

SCRAM-SHA-256-PLUS on Postgres enables tls-server-end-point channel binding, where the client sends the server a hash of the certificate it received as part of the TLS handshake. This prevents some kinds of MITM attacks where the attacker obtains a certificate that appears valid for the server, but is not actually the server's.

So far I've tested it working against Neon (who support SCRAM-SHA-256-PLUS) and Supabase (who don't), and with the new client.enableChannelBinding flag both true and false.

Feel free to make any changes you think appropriate.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@jawj
Added support for SCRAM-SHA-256-PLUS i.e. channel binding
65e1c72
@jawj jawj changed the title Added support for SCRAM-SHA-256-PLUS i.e. channel binding Add support for SCRAM-SHA-256-PLUS i.e. channel binding Jan 10, 2025
brianc
brianc reviewed Jan 13, 2025
View reviewed changes
Comment thread packages/pg/lib/crypto/sasl.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

brianc
brianc reviewed Jan 13, 2025
View reviewed changes
Comment thread packages/pg/package.json Resolved

Uh oh!

There was an error while loading. Please reload this page.

brianc
brianc reviewed Jan 13, 2025
View reviewed changes
Comment thread packages/pg/test/integration/client/sasl-scram-tests.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

brianc
brianc requested changes Jan 13, 2025
View reviewed changes

@brianc brianc left a comment

Copy link
Copy Markdown
Owner

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the PR! Put a few comments on here. My main concern is changing the default behavior - as much as its a pain on users to enable the feature, I'd rather it be an opt-in thing vs a default...otherwise it's a slight breaking change, which requires a new semver major. Would be nice to add a callout in the docs about this as well...though I can handle that if you don't want to

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jawj added 2 commits January 14, 2025 12:34
@jawj
Requested tweaks to channel binding
a338521
@jawj
Additional tweaks to channel binding
8ab2b17
@jawj

jawj commented Jan 14, 2025

Copy link
Copy Markdown
Contributor Author

Thanks @brianc. I've made those changes and added a short section to the docs.

However, while writing the docs it struck me that I have only provided the opt-in option directly on Client. How should this feature be exposed for those using pool.query?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj
Fixed lint complaints
37f9285
charmander
charmander requested changes Jan 16, 2025
View reviewed changes
Comment thread packages/pg/lib/crypto/sasl.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/pg/lib/crypto/sasl.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/pg/lib/crypto/sasl.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/pg/lib/crypto/sasl.js Outdated
// override if channel binding is in use:
if (session.mechanism === 'SCRAM-SHA-256-PLUS') {
const peerCert = stream.getPeerCertificate().raw
const parsedCert = new x509.X509Certificate(peerCert)

@charmander charmander Jan 16, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of a separate package, could stream.getPeerCertificate().fingerprint256 be used with a fixed selection of SHA-256? Or is that not the same hash?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@charmander charmander Jan 16, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind – that wouldn’t be spec-compliant and I missed that the hash wasn’t being used for anything more than its name anyway.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jawj reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@jawj jawj Jan 16, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do appreciate that bringing in a whole X509 parsing library (and then parsing the whole certificate, when all we actually need is the signature algorithm) feels like overkill.

I did actually have a go at doing the minimum necessary parsing manually: see https://gist.github.com/jawj/04a90e51196ac054d6741c8d079d9cff

The reason I didn't go with that in the PR is that I haven't been able to find a list of either (a) what signature algorithms could theoretically be used or even (b) what signature algorithms would cover 99% of cases.

But I strongly suspect that the cases covered by this code would be almost all of them, and any missing ones might be plugged if people filed issues about them. So this could be another option?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@brianc brianc Jan 17, 2025

Copy link
Copy Markdown
Owner

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I strongly suspect that the cases covered by this code would be almost all of them, and any missing ones might be plugged if people filed issues about them. So this could be another option?

I am absolutely totally down w/ the "make it mostly work and patch if different algorithms show up later" mode if it removes the requirement to pull in an entire dependency! I wouldn't say it's a mandatory change but certainly would be welcome. 😄 I have tried very hard over the years to keep as many 3rd party dependencies out of the code as possible just because....well...left-pad and all that stuff, ya know?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jawj jawj Jan 20, 2025

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@brianc Good to hear: I'm all for that approach. I've done a bit more work on this code (including putting a base64 hash of the public cert in the error message, to make it easy to report failures in a way we can investigate) and removed the dependency.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread packages/pg/lib/client.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread packages/pg/test/integration/client/sasl-scram-tests.js Outdated
}

suite.testAsync('can connect using sasl/scram', async () => {
suite.testAsync('can connect using sasl/scram (channel binding enabled)', async () => {

@charmander charmander Jan 16, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests would work just as well if there were no implementation of channel binding at all, so this PR probably needs a targeted test from someone.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@jawj jawj Jan 20, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in an ideal world we would test using SSL certs with a variety of different signature algorithms. But I'm not sure how easily that fits with the current test setup.

Locally, I have been using:

  • The server-*-cert.pem files from the OpenSSL tests: https://github.com/openssl/openssl/tree/master/test/certs

  • This Docker file:

#!/bin/zsh
docker run --rm --name postgres-tls-cert \
  -p 5432:5432 \
  -e POSTGRES_USER=frodo \
  -e POSTGRES_PASSWORD=friend \
  -v ./certs:/etc/pgssl \
  postgres:17.2-bookworm \
  -c ssl=on \
  -c ssl_cert_file=/etc/pgssl/server-$1-cert.pem \
  -c ssl_key_file=/etc/pgssl/server-$1-key.pem

Which is run like: ./docker-pg.sh ecdsa

  • And this test:
SCRAM_TEST_PGDATABASE=frodo \                                          
  SCRAM_TEST_PGUSER=frodo \
  SCRAM_TEST_PGPASSWORD=friend \
  PGSSLMODE=no-verify \
  node --tls-keylog=/path/to/keylog.txt packages/pg/test/integration/client/sasl-scram-tests.js

(The --tls-keylog enables the use of Wireshark if needed).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
jawj and others added 5 commits January 16, 2025 18:14
@jawj @charmander
Update packages/pg/lib/crypto/sasl.js
ae6ab3f
Co-authored-by: Charmander <[email protected]>
@jawj @charmander
Update packages/pg/lib/crypto/sasl.js
152396f
Co-authored-by: Charmander <[email protected]>
@jawj @charmander
Update packages/pg/lib/client.js
f899f8f
Co-authored-by: Charmander <[email protected]>
@jawj
Tweaks to channel binding
52e656c
@jawj
Merge branch 'add-scram-sha-256-plus' of https://github.com/jawj/node…
1003bff
…-postgres into add-scram-sha-256-plus
@brianc

brianc commented Jan 17, 2025

Copy link
Copy Markdown
Owner

How should this feature be exposed for those using pool.query?

hmm good question - would need to make it something that's thread through the pool to its create method as well. The pool passes its own constructor options to the client which probably if i were designing the API today from scratch I wouldn't do but..that ship sailed like 10 years ago 🙃 - so it should just be able to be set on the pool as well?

jawj reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc

brianc commented Jan 17, 2025

Copy link
Copy Markdown
Owner

@jawj ruh roh! looks like a test is failing (should be an easy fix). There are a lot of tests - some are duplicates, etc. If you just wanna run a subset locally for faster turn around you can run make test-unit and/or make test-integration from the ./packages/pg subfolder. In this case it was a unit test that broke (meaning you don't need an instance of postgres running locally)

jawj reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jawj added 4 commits January 20, 2025 09:48
@jawj
Now using homegrown certificate signature algorithm identification
50ee305
@jawj
Update ssl.mdx with channel binding changes
b3a8757
@jawj
Allow for config object being undefined when assigning enableChannelB…
67a6e9c
…inding
@jawj
Fixed a test failing on an updated error message
b604068
@jawj

jawj commented Jan 20, 2025

Copy link
Copy Markdown
Contributor Author

I think the tests should be passing again now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj

jawj commented Jan 20, 2025

Copy link
Copy Markdown
Contributor Author

BTW, you may find this interesting: https://bytebybyte.dev/?postgres

(Writing this was how I figured out how channel binding works).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj
jawj requested review from brianc and charmander January 20, 2025 17:47
@jawj
Removed - from hash names like SHA-256 for legacy crypto (Node 14 and…
88cbe49
… below)
@jawj

jawj commented Jan 21, 2025

Copy link
Copy Markdown
Contributor Author

I just noticed that legacy crypto expects SHA256 rather than SHA-256, and so on, which is now fixed.

But a potential annoyance is that SubtleCrypto supports a very limited range of hash functions compared to Node's legacy crypto. If people run into this as a problem with real certificates, it could be worth considering using legacy crypto, when available, more widely.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

jawj added 2 commits January 22, 2025 13:54
@jawj
Removed packageManager key from package.json
05690bb
@jawj
Added some SASL/channel binding unit tests
d9fdccf
@jawj
Added a unit test for continueSession to check expected SASL session …
9a91cb7
…data
@jawj

jawj commented Jan 28, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

OK, this seems to have stalled a little in relation to testing.

To summarise the previous position:

  • I had already added an integration test to ensure that SCRAM-SHA-256-PLUS is used (and the connection succeeds) when it's offered and channel binding is enabled. I think this may be failing in CI because an SSL connection is not used (PGTESTNOSSL: true). I guess I can add a check for SSL in the test, and abort if it's not used, but of course that means it won't be testing what it's meant to in CI. What do you think?

  • What's not currently tested automatically (via integration tests) is compatibility with various certificate signature algorithms. I think this would be nice to have, but it involves larger changes to the testing/CI infrastructure than I'm comfortable making. I have, however, successfully tested with the OpenSSL DSA, ECDSA, CECDSA, PSS and Ed25519 server certificates locally (Ed448 and ecdsa-brainpoolP256r1 are failing at the TLS-handshake stage, so the question of channel binding doesn't come up).

I've just added some more unit tests:

  • A few unit tests to ensure that SASL startSession returns the expected data when channel binding is and isn't enabled and SCRAM-SHA-256-PLUS is and isn't offered.

  • A similar unit test for SASL continueSession with mechanism: "SCRAM-SHA-256-PLUS", which includes a minimal ASN.1 certificate structure that can be parsed for a signature hash algorithm.

As part of the testing, I realised I have not provided a way to enforce (rather than just enable) channel binding. But since the client's response is different when it does and doesn't think the server supports channel binding, I don't think this makes a big difference to the risk of MITM attacks.

@brianc and @charmander, how do you think we can move this forward?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc

brianc commented Feb 13, 2025

Copy link
Copy Markdown
Owner

Sorry for the delay - just not sure what's missing here at this point. Code looks good to me, unfortunately some of the tests aren't passing is the only thing I can see now. 😱

Thanks for doing all the hard work here!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj
Modify tests: don't require channel binding (which cannot then work) …
8fc5f5f
…if not using SSL
@jawj

jawj commented Feb 13, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Thanks @brianc. Tests are now passing! I just modified the SCRAM-SHA-256-PLUS test to only expect SCRAM-SHA-256-PLUS if we're on an SSL connection (because otherwise there's no certificate to check).

So maybe this is ready to merge?

As I said before, in an ideal world this would probably have CI tests (1) over an SSL connection and, even better, (2) with a variety of certificate signature algorithms. But I'm probably not the right person to make those larger changes to the testing setup, and since this is an opt-in feature I think it's probably OK to go ahead without those for now.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj

jawj commented Mar 10, 2025

Copy link
Copy Markdown
Contributor Author

@brianc I think this is ready to merge (see above). Any remaining blockers?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc

brianc commented Mar 10, 2025

Copy link
Copy Markdown
Owner

Wow this is really fabulous. Thanks so much for going out of your way to take the time to add this. Just got back from a relatively long trip to south america. Merging this now! Will do a release tomorrow AM (I like to release in morning my time just in case something blows up I have more of the day to react to problems) 😄 ❤️

jawj reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@brianc
brianc merged commit b4022aa into brianc:master Mar 10, 2025
@brianc

brianc commented Mar 11, 2025

Copy link
Copy Markdown
Owner

@jawj published!

jawj reacted with hooray emoji
All reactions
  • 🎉 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jawj jawj mentioned this pull request Jan 8, 2026
Add SCRAM-SHA-256-PLUS support porsager/postgres#1008
Closed
@jawj

jawj commented May 18, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

Hi @brianc. I see some changes are proposed for connection security in v9

As noted there, I'm not sure I like these specific changes very much! But while we're trying to be more like libpq, perhaps this would this be a good time to recognise libpq's channel_binding=disable|prefer|require option, and make channel_binding=prefer the default? At present (based on this PR of mine), users have to specifically opt in via the non-standard enableChannelBinding option, and I suspect that means that the vast majority do not get the security bump this ought to be giving them.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@coderabbitai coderabbitai Bot mentioned this pull request Aug 5, 2026
implement database schema alirezakarbalaei/sportz-websockets#1
Merged
@carlosalbertolajr carlosalbertolajr mentioned this pull request Aug 11, 2026
[auto-generated] [pg] Add missing enableChannelBinding option DefinitelyTyped/DefinitelyTyped#75378
Merged
8 tasks done
@jawj jawj mentioned this pull request Aug 12, 2026
Support libpq's channel_binding and require_auth (and enforce them against every authentication request) #3746
Open
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@brianc brianc Awaiting requested review from brianc
@charmander charmander Awaiting requested review from charmander

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@jawj @brianc @charmander
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.