Skip to content

Commit 7530d44

Browse files
authored
Merge pull request #68 from NikolayS/security/fix-sql-injection-vulnerabilities
feat(roles): add role management to menu and improve security
2 parents 1bdc9b7 + 407d045 commit 7530d44

13 files changed

+45
-24
lines changed

init/generate.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@ cat > "$WARMUP" <<- VersCheck
1616
select 1/0;
1717
\endif
1818
19+
select current_setting('server_version_num')::integer >= 170000 as postgres_dba_pgvers_17plus \gset
20+
21+
select current_setting('server_version_num')::integer >= 130000 as postgres_dba_pgvers_13plus \gset
22+
1923
select current_setting('server_version_num')::integer >= 100000 as postgres_dba_pgvers_10plus \gset
2024
\if :postgres_dba_pgvers_10plus
2125
\set postgres_dba_last_wal_receive_lsn pg_last_wal_receive_lsn

roles/alter_user_with_random_password.psql

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,17 @@ begin
4343
j := int4(random() * allowed_len);
4444
pwd := pwd || substr(allowed, j+1, 1);
4545
end loop;
46-
sql := 'alter role ' || current_setting('postgres_dba.username')::text || ' password ''' || pwd || ''';';
46+
sql := format('alter role %I password %L', current_setting('postgres_dba.username')::text, pwd);
4747
raise debug 'SQL: %', sql;
4848
execute sql;
49-
sql := 'alter role ' || current_setting('postgres_dba.username')::text
50-
|| (case when lower(current_setting('postgres_dba.is_superuser')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' superuser' else '' end)
51-
|| ';';
49+
sql := format('alter role %I%s',
50+
current_setting('postgres_dba.username')::text,
51+
(case when lower(current_setting('postgres_dba.is_superuser')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' superuser' else '' end));
5252
raise debug 'SQL: %', sql;
5353
execute sql;
54-
sql := 'alter role ' || current_setting('postgres_dba.username')::text
55-
|| (case when lower(current_setting('postgres_dba.login')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' login' else '' end)
56-
|| ';';
54+
sql := format('alter role %I%s',
55+
current_setting('postgres_dba.username')::text,
56+
(case when lower(current_setting('postgres_dba.login')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' login' else '' end));
5757
raise debug 'SQL: %', sql;
5858
execute sql;
5959
raise debug 'User % altered, password: %', current_setting('postgres_dba.username')::text, pwd;

roles/create_user_with_random_password.psql

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,11 @@ begin
4343
j := int4(random() * allowed_len);
4444
pwd := pwd || substr(allowed, j+1, 1);
4545
end loop;
46-
sql := 'create role ' || current_setting('postgres_dba.username')::text
47-
|| (case when lower(current_setting('postgres_dba.is_superuser')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' superuser' else '' end)
48-
|| (case when lower(current_setting('postgres_dba.login')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' login' else '' end)
49-
|| ' password ''' || pwd || ''';';
46+
sql := format('create role %I%s%s password %L',
47+
current_setting('postgres_dba.username')::text,
48+
(case when lower(current_setting('postgres_dba.is_superuser')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' superuser' else '' end),
49+
(case when lower(current_setting('postgres_dba.login')::text) not in ('0', '', 'no', 'false', 'n', 'f') then ' login' else '' end),
50+
pwd);
5051
raise debug 'SQL: %', sql;
5152
execute sql;
5253
raise info 'User % created, password: %', current_setting('postgres_dba.username')::text, pwd;

sql/0_node.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--Node & current DB information: master/replica, lag, DB size, tmp files, etc.
1+
--Node and current database information: primary/replica, lag, database size, temporary files, etc.
22

33
/*
44
For Postgres versions older than 10, run this first:

sql/a1_activity.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--Current activity: count of current connections grouped by database, user name, state
1+
--Current activity: count of current connections grouped by database, username, state
22
select
33
coalesce(usename, '** ALL users **') as "User",
44
coalesce(datname, '** ALL databases **') as "DB",

sql/e1_extensions.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--Extensions installed in current DB
1+
--Extensions installed in current database
22

33
select
44
ae.name,

sql/i3_non_indexed_fks.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--FKs with Missing/Bad Indexes
1+
--Foreign keys with missing or bad indexes
22

33
--Created by PostgreSQL Experts https://github.com/pgexperts/pgx_scripts/blob/master/indexes/fk_no_index.sql
44

sql/l1_lock_trees.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
--Lock trees (leightweight)
1+
--Lock trees (lightweight)
22

33
-- Source: https://github.com/dataegret/pg-utils/blob/master/sql/locktree.sql
44
-- The paths won't be precise but this query is very light and may be used quite frequently
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--Create user with random password (interactive)
2+
\ir ../roles/create_user_with_random_password.psql
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--Alter user with random password (interactive)
2+
\ir ../roles/alter_user_with_random_password.psql

0 commit comments

Comments
 (0)