Skip to content

Commit bf75869

Browse files
taimoorzaeemwolfgangwalther
authored andcommitted
fix: login with uppercase and mixed case role names
PostgREST failed when querying role settings where current role name contained uppercase letters. This commit resolves it by quoting the CURRENT_USER. Signed-off-by: Taimoor Zaeem <taimoorzaeem@gmail.com>
1 parent a5cc457 commit bf75869

10 files changed

Lines changed: 22 additions & 21 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ All notable changes to this project will be documented in this file. From versio
1818
### Fixed
1919

2020
- Shutdown should wait for in flight requests by @mkleczek in #4702
21+
- Fix login with uppercase and mixed case role names by @taimoorzaeem in #4678
2122

2223
### Changed
2324

nix/tools/withTools.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ let
2424
"ARG_OPTIONAL_SINGLE([fixtures], [f], [SQL file to load fixtures from])"
2525
"ARG_POSITIONAL_SINGLE([command], [Command to run])"
2626
"ARG_LEFTOVERS([command arguments])"
27-
"ARG_USE_ENV([PGUSER], [postgrest_test_authenticator], [Authenticator PG role])"
27+
"ARG_USE_ENV([PGUSER], [Postgrest_Test_Authenticator], [Authenticator PG role])" # user is written in mixed case to implicitly test that it is being properly quoted in schema cache queries
2828
"ARG_USE_ENV([PGDATABASE], [postgres], [PG database name])"
2929
"ARG_USE_ENV([PGRST_DB_SCHEMAS], [test], [Schema to expose])"
3030
"ARG_USE_ENV([PGTZ], [utc], [Timezone to use])"

src/PostgREST/Config/Database.hs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ queryDbSettings preConfFunc =
102102
SELECT setdatabase as database,
103103
unnest(setconfig) as setting
104104
FROM pg_catalog.pg_db_role_setting
105-
WHERE setrole = CURRENT_USER::regrole::oid
105+
WHERE setrole = quote_ident(CURRENT_USER)::regrole::oid
106106
AND setdatabase IN (0, (SELECT oid FROM pg_catalog.pg_database WHERE datname = CURRENT_CATALOG))
107107
),
108108
kv_settings AS (
@@ -142,7 +142,7 @@ queryRoleSettings pgVer =
142142
select r.rolname, unnest(r.rolconfig) as setting
143143
from pg_auth_members m
144144
join pg_roles r on r.oid = m.roleid
145-
where member = current_user::regrole::oid
145+
where member = quote_ident(current_user)::regrole::oid
146146
),
147147
kv_settings AS (
148148
SELECT
@@ -167,7 +167,7 @@ queryRoleSettings pgVer =
167167
|]
168168

169169
hasParameterPrivilege
170-
| pgVer >= pgVersion150 = "or has_parameter_privilege(current_user::regrole::oid, ps.name, 'set')"
170+
| pgVer >= pgVersion150 = "or has_parameter_privilege(quote_ident(current_user)::regrole::oid, ps.name, 'set')"
171171
| otherwise = ""
172172

173173
processRows :: [(Text, Maybe Text, [(Text, Text)])] -> (RoleSettings, RoleIsolationLvl)

test/io/fixtures/big_schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11399,7 +11399,7 @@ $$;
1139911399
DROP ROLE IF EXISTS postgrest_test_anonymous;
1140011400
CREATE ROLE postgrest_test_anonymous;
1140111401

11402-
GRANT postgrest_test_anonymous TO :PGUSER;
11402+
GRANT postgrest_test_anonymous TO :"PGUSER";
1140311403

1140411404
GRANT USAGE ON SCHEMA apflora TO postgrest_test_anonymous;
1140511405
GRANT USAGE ON SCHEMA fuzzysearch TO postgrest_test_anonymous;

test/io/fixtures/replica.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ create table replica.items as select x as id from generate_series(1, 10) x;
1313
DROP ROLE IF EXISTS postgrest_test_anonymous;
1414
CREATE ROLE postgrest_test_anonymous;
1515

16-
GRANT postgrest_test_anonymous TO :PGUSER;
16+
GRANT postgrest_test_anonymous TO :"PGUSER";
1717

1818
GRANT USAGE ON SCHEMA replica TO postgrest_test_anonymous;
1919

test/io/fixtures/roles.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ CREATE ROLE postgrest_test_w_superuser_settings;
1212
GRANT
1313
postgrest_test_anonymous, postgrest_test_author,
1414
postgrest_test_serializable, postgrest_test_repeatable_read,
15-
postgrest_test_w_superuser_settings TO :PGUSER;
15+
postgrest_test_w_superuser_settings TO :"PGUSER";
1616

17-
ALTER ROLE :PGUSER SET pgrst.db_anon_role = 'postgrest_test_anonymous';
17+
ALTER ROLE :"PGUSER" SET pgrst.db_anon_role = 'postgrest_test_anonymous';
1818
ALTER ROLE postgrest_test_serializable SET default_transaction_isolation = 'serializable';
1919
ALTER ROLE postgrest_test_repeatable_read SET default_transaction_isolation = 'REPEATABLE READ';
2020

test/io/fixtures/schema.sql

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ VALUES (1, 'pulp fiction', 1),
4242
DO $do$BEGIN
4343
IF (SELECT current_setting('server_version_num')::INT >= 150000) THEN
4444
ALTER ROLE postgrest_test_w_superuser_settings SET log_min_duration_sample = 12345;
45-
GRANT SET ON PARAMETER log_min_duration_sample to postgrest_test_authenticator;
45+
GRANT SET ON PARAMETER log_min_duration_sample to "Postgrest_Test_Authenticator";
4646
END IF;
4747
END$do$;
4848

@@ -61,7 +61,7 @@ $$ language sql;
6161
create function change_max_rows_config(val int, notify bool default false) returns void as $_$
6262
begin
6363
execute format($$
64-
alter role postgrest_test_authenticator set pgrst.db_max_rows = %L;
64+
alter role "Postgrest_Test_Authenticator" set pgrst.db_max_rows = %L;
6565
$$, val);
6666
if notify then
6767
perform pg_notify('pgrst', 'reload config');
@@ -70,28 +70,28 @@ end $_$ volatile security definer language plpgsql ;
7070

7171
create function reset_max_rows_config() returns void as $_$
7272
begin
73-
alter role postgrest_test_authenticator reset pgrst.db_max_rows;
73+
alter role "Postgrest_Test_Authenticator" reset pgrst.db_max_rows;
7474
end $_$ volatile security definer language plpgsql ;
7575

7676
create function change_db_schema_and_full_reload(schemas text) returns void as $_$
7777
begin
7878
execute format($$
79-
alter role postgrest_test_authenticator set pgrst.db_schemas = %L;
79+
alter role "Postgrest_Test_Authenticator" set pgrst.db_schemas = %L;
8080
$$, schemas);
8181
perform pg_notify('pgrst', 'reload config');
8282
perform pg_notify('pgrst', 'reload schema');
8383
end $_$ volatile security definer language plpgsql ;
8484

8585
create function v1.reset_db_schema_config() returns void as $_$
8686
begin
87-
alter role postgrest_test_authenticator reset pgrst.db_schemas;
87+
alter role "Postgrest_Test_Authenticator" reset pgrst.db_schemas;
8888
perform pg_notify('pgrst', 'reload config');
8989
perform pg_notify('pgrst', 'reload schema');
9090
end $_$ volatile security definer language plpgsql ;
9191

9292
create function invalid_role_claim_key_reload() returns void as $_$
9393
begin
94-
alter role postgrest_test_authenticator set pgrst.jwt_role_claim_key = 'test';
94+
alter role "Postgrest_Test_Authenticator" set pgrst.jwt_role_claim_key = 'test';
9595
perform pg_notify('pgrst', 'reload config');
9696
end $_$ volatile security definer language plpgsql ;
9797

@@ -104,7 +104,7 @@ $_$ language sql;
104104

105105
create function reset_invalid_role_claim_key() returns void as $_$
106106
begin
107-
alter role postgrest_test_authenticator reset pgrst.jwt_role_claim_key;
107+
alter role "Postgrest_Test_Authenticator" reset pgrst.jwt_role_claim_key;
108108
perform pg_notify('pgrst', 'reload config');
109109
end $_$ volatile security definer language plpgsql ;
110110

@@ -229,12 +229,12 @@ $$ language sql;
229229

230230
create function change_db_schemas_config() returns void as $_$
231231
begin
232-
alter role postgrest_test_authenticator set pgrst.db_schemas = 'test';
232+
alter role "Postgrest_Test_Authenticator" set pgrst.db_schemas = 'test';
233233
end $_$ volatile security definer language plpgsql;
234234

235235
create function reset_db_schemas_config() returns void as $_$
236236
begin
237-
alter role postgrest_test_authenticator reset pgrst.db_schemas;
237+
alter role "Postgrest_Test_Authenticator" reset pgrst.db_schemas;
238238
end $_$ volatile security definer language plpgsql ;
239239

240240
create function test.get_current_schema() returns text as $$

test/load/fixtures.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
CREATE ROLE postgrest_test_anonymous;
22
CREATE ROLE postgrest_test_author;
3-
GRANT postgrest_test_anonymous TO :PGUSER;
4-
GRANT postgrest_test_author TO :PGUSER;
3+
GRANT postgrest_test_anonymous TO :"PGUSER";
4+
GRANT postgrest_test_author TO :"PGUSER";
55
CREATE SCHEMA test;
66

77
-- PUT+PATCH target needs one record and column to modify

test/observability/fixtures/roles.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ DROP ROLE IF EXISTS postgrest_test_anonymous, postgrest_test_author;
22
CREATE ROLE postgrest_test_anonymous;
33
CREATE ROLE postgrest_test_author;
44

5-
GRANT postgrest_test_anonymous, postgrest_test_author TO :PGUSER;
5+
GRANT postgrest_test_anonymous, postgrest_test_author TO :"PGUSER";

test/spec/fixtures/roles.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ CREATE ROLE postgrest_test_default_role;
44
CREATE ROLE postgrest_test_author;
55
CREATE ROLE postgrest_test_superuser WITH SUPERUSER;
66

7-
GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author, postgrest_test_superuser TO :PGUSER;
7+
GRANT postgrest_test_anonymous, postgrest_test_default_role, postgrest_test_author, postgrest_test_superuser TO :"PGUSER";

0 commit comments

Comments
 (0)