@@ -22,6 +22,21 @@ const DB_CONNECTION = `postgres://${DB_USERNAME}:${encodeURIComponent(DB_PASSWOR
2222// Determine if SSL is enabled for database connections
2323const isSslEnabled = DB_SSL_ENABLED === 'true' ;
2424
25+ // Determine if the server's certificate should be validated against the local CA bundle.
26+ // Defaults to true (most secure). This is only overridden if SSL is enabled AND the
27+ // DB_SSL_REJECT_UNAUTHORIZED variable is explicitly set.
28+ let rejectUnauthorized = true ;
29+
30+ if ( isSslEnabled ) {
31+ try {
32+ // getRequiredEnvString throws if the env var is not present. We catch this to make it optional.
33+ const rejectUnauthorizedEnv = getRequiredEnvString ( 'DB_SSL_REJECT_UNAUTHORIZED' ) ;
34+ rejectUnauthorized = rejectUnauthorizedEnv !== 'false' ;
35+ } catch ( error ) {
36+ // The env var is not set; we'll proceed with the default of rejectUnauthorized = true.
37+ }
38+ }
39+
2540/**
2641 * PostgreSQL connection pool for direct query execution.
2742 * This provides a lower-level database access mechanism than Sequelize.
@@ -35,8 +50,11 @@ export const rootPgPool = new Pool({
3550 connectionString : DB_CONNECTION ,
3651 ...( isSslEnabled && {
3752 ssl : {
38- rejectUnauthorized : true ,
39- ca : fs . readFileSync ( __dirname + '/global-bundle.pem' ) . toString ( ) ,
53+ rejectUnauthorized : rejectUnauthorized ,
54+ // Only include the CA if we are validating the certificate
55+ ...( rejectUnauthorized && {
56+ ca : fs . readFileSync ( __dirname + '/global-bundle.pem' ) . toString ( ) ,
57+ } ) ,
4058 } ,
4159 } ) ,
4260} ) ;
@@ -75,8 +93,11 @@ export const sequelize = new Sequelize(
7593 dialectOptions : {
7694 ssl : {
7795 require : true ,
78- rejectUnauthorized : true ,
79- ca : fs . readFileSync ( __dirname + '/global-bundle.pem' ) . toString ( ) ,
96+ rejectUnauthorized : rejectUnauthorized ,
97+ // Only include the CA if we are validating the certificate
98+ ...( rejectUnauthorized && {
99+ ca : fs . readFileSync ( __dirname + '/global-bundle.pem' ) . toString ( ) ,
100+ } ) ,
80101 } ,
81102 } ,
82103 } ) ,
0 commit comments