-
Notifications
You must be signed in to change notification settings - Fork 438
Fix Insecure Default Configuration #1554
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
base: development
Are you sure you want to change the base?
Changes from all commits
f3aef3a
204f0ef
759870d
a817ee5
4c8354f
fa7baf5
52815d0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| /* This Source Code Form is subject to the terms of the Mozilla Public | ||
| * License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
|
||
| const { Client } = require('pg'); | ||
|
|
||
| const newPassword = process.argv[2]; | ||
|
|
||
| console.log('Attempting to change postgres user password...'); | ||
|
|
||
| function escapePassword(password) { | ||
| return password.replace(/'/g, "''"); | ||
| } | ||
|
|
||
| async function changePassword() { | ||
| const client = new Client({ | ||
| host: process.env.OED_DB_HOST, | ||
| port: parseInt(process.env.OED_DB_PORT), | ||
| user: 'postgres', | ||
| password: 'pleaseChange', | ||
| database: 'postgres', | ||
| connectionTimeoutMillis: 10000 | ||
| }); | ||
|
|
||
| try { | ||
| await client.connect(); | ||
|
|
||
| const sql = `ALTER USER postgres WITH PASSWORD '${escapePassword(newPassword)}'`; | ||
| await client.query(sql); | ||
|
|
||
| console.log('Password changed successfully'); | ||
| await client.end(); | ||
|
|
||
| } catch (error) { | ||
| console.error('Error:', error.message); | ||
|
|
||
| if (error.message.includes('ECONNREFUSED')) { | ||
| console.error('Database is not accepting connections yet.'); | ||
| } else if (error.message.includes('password authentication')) { | ||
| console.error('Authentication failed. Current password may be incorrect.'); | ||
| } | ||
|
|
||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| // Wait for database to be ready | ||
| setTimeout(async () => { | ||
| try { | ||
| await changePassword(); | ||
| console.log('Password change completed'); | ||
| process.exit(0); | ||
| } catch (error) { | ||
| console.error('Failed to change password'); | ||
| process.exit(1); | ||
| } | ||
| }, 10000); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -58,6 +58,21 @@ if [ -f ".env" ]; then | |
| source .env | ||
| fi | ||
|
|
||
| # Creating a centralized variable to keep track of the type of installation. | ||
| INSTALL_MODE="production" | ||
|
|
||
| if [ "$production" = "yes" ] || [ "$OED_PRODUCTION" = "yes" ]; then | ||
| INSTALL_MODE="production" | ||
| elif [ "$production" = "no" ] || [ "$OED_PRODUCTION" = "no" ]; then | ||
| INSTALL_MODE="development" | ||
| fi | ||
|
|
||
| if [ "$INSTALL_MODE" = "invalid" ]; then | ||
| printf "\nFailure: Invalid or missing environment configuration." | ||
| printf "\nSet OED_PRODUCTION to 'yes' for production or 'no' for development." | ||
| exit 10 | ||
| fi | ||
|
|
||
| # Skip the install if the node_modules were installed before the package files. | ||
| # The two package files | ||
| packageFile="package.json" | ||
|
|
@@ -147,7 +162,7 @@ else | |
|
|
||
| # Create a user | ||
| set -e | ||
| if [ "$production" == "no" ] && [ ! "$OED_PRODUCTION" == "yes" ]; then | ||
| if [ "$INSTALL_MODE" = "development" ]; then | ||
| npm run createUser -- $usernameTest password | ||
| createuserTest_code=$? | ||
| # this second username uses an email: [email protected] and we will remove this eventually | ||
|
|
@@ -176,7 +191,7 @@ else | |
| fi | ||
|
|
||
| # Build webpack if needed | ||
| if [ "$production" == "yes" ] || [ "$OED_PRODUCTION" == "yes" ]; then | ||
| if [ "$INSTALL_MODE" = "production" ]; then | ||
| npm run webpack:build | ||
| elif [ "$dostart" == "no" ]; then | ||
| npm run webpack | ||
|
|
@@ -186,13 +201,68 @@ printf "%s\n" "OED install finished" | |
|
|
||
| # Start OED | ||
| if [ "$dostart" == "yes" ]; then | ||
| if [ "$production" == "yes" ] || [ "$OED_PRODUCTION" == "yes" ]; then | ||
| if [ "$INSTALL_MODE" = "production" ]; then | ||
| printf "%s\n" "Starting OED in production mode" | ||
| # Checking if the user has set a mail method and left one of the mailing environment variables default, warning if so | ||
| if [ -z "$OED_MAIL_METHOD" ] || [ "$OED_MAIL_METHOD" != "none" ]; then | ||
| if [ "$OED_MAIL_SMTP" = "smtp.example.com" ] || \ | ||
| [ "$OED_MAIL_SMTP_PORT" = "465" ] || \ | ||
| [ "$OED_MAIL_IDENT" = "[email protected]" ] || \ | ||
| [ "$OED_MAIL_CREDENTIAL" = "credential" ] || \ | ||
| [ "$OED_MAIL_FROM" = "[email protected]" ] || \ | ||
| [ "$OED_MAIL_TO" = "[email protected]" ] || \ | ||
| [ "$OED_MAIL_ORG" = "My Organization Name" ]; then | ||
| printf "\n********************************************************************************\n" | ||
| printf "* WARNING: You have set your mail method but one or more of the mail environment variables are still set to the default value!*\n" | ||
| printf "********************************************************************************\n\n" | ||
| fi | ||
| fi | ||
| # If the user is in production and their token secret has been left default, generating a random one | ||
| if [ -z "$OED_TOKEN_SECRET" ] || [ "$OED_TOKEN_SECRET" = "?" ]; then | ||
| printf "\nNo valid OED_TOKEN_SECRET detected. Generating a secure random secret...\n" | ||
|
|
||
| # Generate 32 bytes of random data and convert to 64-character hex | ||
| OED_TOKEN_SECRET=$(openssl rand -hex 32) | ||
| export OED_TOKEN_SECRET | ||
|
|
||
| printf "\n********************************************************************************\n" | ||
| printf "Generated OED_TOKEN_SECRET: %s\n" "$OED_TOKEN_SECRET" | ||
huss marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| printf "\n Make sure to save or change this value" | ||
| printf "********************************************************************************\n\n" | ||
|
|
||
| # Save to .env for future runs | ||
| if [ -f ".env" ]; then | ||
| if grep -q "^OED_TOKEN_SECRET=" .env; then | ||
| sed -i "s/^OED_TOKEN_SECRET=.*/OED_TOKEN_SECRET=$OED_TOKEN_SECRET/" .env | ||
| else | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment applies in a general way but put here. I'm thinking about the fact that there are settings in .env and docker-compose.yml that apply to the same case. I think originally it was designed to allow flexibility in how the values were given and to override them. I think the reality is that people don't use this ability. Also, security has become more important so I'm thinking having it in two places is less desirable since people might miss one. Finally, I think OED will be adopting new ways to set values that is more secure and easier so getting to one way now might help for that future effort. First: What do you think about getting rid of one of the ways to set these values? Second: If yes, then should we only use the docker-compose.yml file? |
||
| echo "OED_TOKEN_SECRET=$OED_TOKEN_SECRET" >> .env | ||
| fi | ||
| else | ||
| echo "OED_TOKEN_SECRET=$OED_TOKEN_SECRET" > .env | ||
| fi | ||
| fi | ||
| # If the user is in production and their postgres password has been left default, generating a random one | ||
| if [ -z "$POSTGRES_PASSWORD" ] || [ "$POSTGRES_PASSWORD" = "pleaseChange" ]; then | ||
| printf "\nNo valid PostgreSQL password detected. Generating a secure random password...\n" | ||
| POSTGRES_PASSWORD=$(openssl rand -base64 12) | ||
| node ./src/scripts/changePass.js "$POSTGRES_PASSWORD" | ||
| printf "\n********************************************************************************\n" | ||
| printf "Generated POSTGRES_PASSWORD: %s\n" "$POSTGRES_PASSWORD" | ||
| printf "\n Make sure to save or change this value" | ||
| printf "\n********************************************************************************\n\n" | ||
| fi | ||
| npm run start | ||
| else | ||
| printf "%s\n" "Starting OED in development mode" | ||
| # Warning the user if they've left their token or postgres password default, we don't randomly generate it in dev mode | ||
| if [ -z "$OED_TOKEN_SECRET" ] || [ "$OED_TOKEN_SECRET" = "?" ]; then | ||
| printf "Warning: you are using OED in development mode with the default OED_TOKEN_SECRET set in docker-compose.yml. If this is not intentional, please update it there.\n" | ||
| fi | ||
| if [ -z "$POSTGRES_PASSWORD" ] || [ "$POSTGRES_PASSWORD" = "pleaseChange" ]; then | ||
| printf "* Warning: you are using OED in development mode with the default PostgreSQL password set in docker-compose.yml. If this is not intentional, please update it there. *\n" printf "********************************************************************************\n\n" | ||
| fi | ||
| printf "%s\n" "Starting OED in development mode." | ||
| ./src/scripts/devstart.sh | ||
| fi | ||
| else | ||
| printf "%s\n" "Not starting OED due to --nostart" | ||
| printf "%s\n" "Not starting OED due to --nostart." | ||
| fi | ||
Uh oh!
There was an error while loading. Please reload this page.