-
Notifications
You must be signed in to change notification settings - Fork 0
fix: postgres init-databases.sh psql variable syntax error #8
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: main
Are you sure you want to change the base?
Changes from all commits
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 |
|---|---|---|
|
|
@@ -4,18 +4,13 @@ set -euo pipefail | |
| # Create service metadata databases during the first Postgres initialization. | ||
| create_database() { | ||
| local database="$1" | ||
| local exists | ||
|
|
||
| exists="$( | ||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres --set=database="$database" \ | ||
| --tuples-only --no-align \ | ||
| --command "SELECT 1 FROM pg_database WHERE datname = :'database'" | ||
| )" | ||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \ | ||
| --command "SELECT 1 FROM pg_database WHERE datname = '$database'" \ | ||
| --tuples-only --no-align | grep -q 1 && return 0 | ||
|
|
||
| if [[ "$exists" != "1" ]]; then | ||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres --set=database="$database" \ | ||
| --command 'CREATE DATABASE :"database"' | ||
| fi | ||
| psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \ | ||
| --command "CREATE DATABASE \"$database\"" | ||
|
Comment on lines
+12
to
+13
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. Database creation uses safer identifier quoting but still benefits from input validation. The double-quoted identifier syntax ( The input validation suggested in the previous comment (lines 8-10) would also protect this statement. 🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| create_database "${KOIN_DATA_AIRFLOW_DB:-airflow_metadata}" | ||
|
|
||
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.
SQL injection vulnerability in database existence check.
The
$databasevariable is directly interpolated into the SQL WHERE clause using single quotes:datname = '$database'. If the database name contains a single quote character, it can break out of the string literal and execute arbitrary SQL.Example attack vector:
KOIN_DATA_AIRFLOW_DB="'; DROP DATABASE postgres; --"Would result in:
While this script runs in a trusted container initialization context with operator-controlled environment variables, it's still a security posture gap that should be addressed.
🛡️ Proposed fix with input validation
Add input validation to ensure database names only contain safe characters:
create_database() { local database="$1" + + # Validate database name (alphanumeric, underscore only) + if ! [[ "$database" =~ ^[a-zA-Z_][a-zA-Z0-9_]*$ ]]; then + echo "Error: Invalid database name '$database'" >&2 + return 1 + fi psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname postgres \Alternatively, use PostgreSQL's dollar-quoted strings to avoid single-quote escaping issues:
🤖 Prompt for AI Agents