Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

feat(self-hosted): restrict rest root anon (#45462)

Luiz Felipe Machado9777f051d6ee
diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml
index 27d946a57ef..de2cb4b8968 100644
--- a/docker/docker-compose.yml
+++ b/docker/docker-compose.yml
@@ -92,6 +92,7 @@ services:
environment:
KONG_DATABASE: "off"
KONG_DECLARATIVE_CONFIG: /usr/local/kong/kong.yml
+ KONG_ROUTER_FLAVOR: expressions
# https://github.com/supabase/cli/issues/14
KONG_DNS_ORDER: LAST,A,CNAME
KONG_DNS_NOT_FOUND_TTL: 1
diff --git a/docker/tests/test-auth-keys.sh b/docker/tests/test-auth-keys.sh
index 2b1ad360934..f8671142673 100644
--- a/docker/tests/test-auth-keys.sh
+++ b/docker/tests/test-auth-keys.sh
@@ -68,13 +68,13 @@ echo ""
# ---------------------------------------------
echo "--- REST API (/rest/v1/) ---"
-check "Legacy ANON_KEY" "200" \
+check "Legacy ANON_KEY -> 403" "403" \
"$(http_status "$BASE_URL/rest/v1/" -H "apikey: $ANON_KEY")"
check "Legacy SERVICE_ROLE_KEY" "200" \
"$(http_status "$BASE_URL/rest/v1/" -H "apikey: $SERVICE_ROLE_KEY")"
if [ -n "$SUPABASE_PUBLISHABLE_KEY" ]; then
- check "New PUBLISHABLE_KEY" "200" \
+ check "New PUBLISHABLE_KEY -> 403" "403" \
"$(http_status "$BASE_URL/rest/v1/" -H "apikey: $SUPABASE_PUBLISHABLE_KEY")"
check "New SECRET_KEY" "200" \
"$(http_status "$BASE_URL/rest/v1/" -H "apikey: $SUPABASE_SECRET_KEY")"
@@ -166,17 +166,27 @@ echo ""
echo "--- supabase-js style requests (apikey + Authorization) ---"
# supabase-js sends both apikey header AND Authorization: Bearer <apikey>
if [ -n "$SUPABASE_PUBLISHABLE_KEY" ]; then
- check "apikey + Authorization: Bearer sb_ (replace path)" "200" \
+ check "apikey + Authorization: Bearer sb_ (replace path)" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $SUPABASE_PUBLISHABLE_KEY" \
-H "Authorization: Bearer $SUPABASE_PUBLISHABLE_KEY")"
+
+ check "secret apikey + Authorization: Bearer sb_secret" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SUPABASE_SECRET_KEY" \
+ -H "Authorization: Bearer $SUPABASE_SECRET_KEY")"
fi
-check "Legacy apikey + Authorization: Bearer <legacy jwt>" "200" \
+check "Legacy apikey + Authorization: Bearer <legacy jwt>" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $ANON_KEY" \
-H "Authorization: Bearer $ANON_KEY")"
+check "Service role apikey + Authorization: Bearer <legacy jwt>" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SERVICE_ROLE_KEY" \
+ -H "Authorization: Bearer $SERVICE_ROLE_KEY")"
+
echo ""
echo "--- Edge cases ---"
# Opaque key in Authorization only (no apikey header) - should be rejected by key-auth
@@ -271,11 +281,16 @@ if [ -n "$access_token" ]; then
fi
# Use the session JWT with PostgREST
- check "Session JWT with PostgREST" "200" \
+ check "Session JWT with PostgREST" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $ANON_KEY" \
-H "Authorization: Bearer $access_token")"
+ check "Session JWT with PostgREST + service role key" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SERVICE_ROLE_KEY" \
+ -H "Authorization: Bearer $access_token")"
+
# Use the session JWT with Storage
check "Session JWT with Storage" "200" \
"$(http_status "$BASE_URL/storage/v1/bucket" \
@@ -288,10 +303,14 @@ if [ -n "$access_token" ]; then
if [ -n "$SUPABASE_PUBLISHABLE_KEY" ]; then
echo ""
echo "--- Authenticated user + opaque key (critical path) ---"
- check "Opaque apikey + user JWT -> PostgREST uses user JWT" "200" \
+ check "Opaque apikey + user JWT -> PostgREST uses user JWT" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $SUPABASE_PUBLISHABLE_KEY" \
-H "Authorization: Bearer $access_token")"
+ check "Secret apikey + user JWT -> PostgREST allowed" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SUPABASE_SECRET_KEY" \
+ -H "Authorization: Bearer $access_token")"
check "Opaque apikey + user JWT -> Storage uses user JWT" "200" \
"$(http_status "$BASE_URL/storage/v1/bucket" \
-H "apikey: $SUPABASE_PUBLISHABLE_KEY" \
@@ -335,10 +354,14 @@ console.log(header+'.'+payload+'.'+sig);
" 2>/dev/null)
if [ -n "$hs256_token" ]; then
- check "HS256 token with PostgREST (backward compat)" "200" \
+ check "HS256 token with PostgREST (backward compat)" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $ANON_KEY" \
-H "Authorization: Bearer $hs256_token")"
+ check "HS256 token with PostgREST + service role key" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SERVICE_ROLE_KEY" \
+ -H "Authorization: Bearer $hs256_token")"
else
echo " SKIP: Could not mint HS256 token (node required)"
fi
diff --git a/docker/tests/test-self-hosted.sh b/docker/tests/test-self-hosted.sh
index 4cfedfa2b87..a72638abdec 100644
--- a/docker/tests/test-self-hosted.sh
+++ b/docker/tests/test-self-hosted.sh
@@ -197,10 +197,16 @@ fi
echo ""
echo "--- PostgREST ---"
-check "REST API query" "200" \
+check "REST API route with anon key" "403" \
"$(http_status "$BASE_URL/rest/v1/" \
-H "apikey: $ANON_KEY")"
+echo ""
+echo "--- PostgREST ---"
+check "REST API route with service role key" "200" \
+ "$(http_status "$BASE_URL/rest/v1/" \
+ -H "apikey: $SERVICE_ROLE_KEY")"
+
# ---------------------------------------------
# 5. GraphQL
# ---------------------------------------------
diff --git a/docker/volumes/api/envoy/lds.template.yaml b/docker/volumes/api/envoy/lds.template.yaml
index e2d72e95b9b..d6f5f314a9b 100644
--- a/docker/volumes/api/envoy/lds.template.yaml
+++ b/docker/volumes/api/envoy/lds.template.yaml
@@ -335,6 +335,41 @@ resources:
type.googleapis.com/envoy.config.route.v3.FilterConfig
disabled: true
+ - name: rest-v1-openapi-protected
+ match:
+ path: /rest/v1/
+ route:
+ cluster: rest
+ prefix_rewrite: /
+ timeout: 30s
+ request_headers_to_add:
+ - header:
+ key: X-Forwarded-Prefix
+ value: /rest/v1/
+ append_action: ADD_IF_ABSENT
+ typed_per_filter_config:
+ envoy.filters.http.basic_auth:
+ '@type': type.googleapis.com/envoy.config.route.v3.FilterConfig
+ disabled: true
+ envoy.filters.http.rbac:
+ '@type': type.googleapis.com/envoy.extensions.filters.http.rbac.v3.RBACPerRoute
+ rbac:
+ rules:
+ action: ALLOW
+ policies:
+ admin:
+ permissions:
+ - any: true
+ principals:
+ - header:
+ name: apikey
+ string_match:
+ exact: '${SERVICE_ROLE_KEY}'
+ - header:
+ name: apikey
+ string_match:
+ exact: '${SERVICE_ROLE_KEY_ASYMMETRIC}'
+
- name: rest-v1-protected
match:
prefix: /rest/v1/
@@ -667,6 +702,7 @@ resources:
local PROTECTED_ROUTES = {
["auth-v1-protected"] = true,
+ ["rest-v1-openapi-protected"] = true,
["rest-v1-protected"] = true,
["graphql-v1-protected"] = true,
["realtime-v1-api-protected"] = true,
diff --git a/docker/volumes/api/kong.yml b/docker/volumes/api/kong.yml
index 0581ad5b0f1..13fb01e1fca 100644
--- a/docker/volumes/api/kong.yml
+++ b/docker/volumes/api/kong.yml
@@ -127,6 +127,33 @@ services:
- admin
- anon
+ ## OpenAPI root - admin only
+ - name: rest-v1-openapi
+ _comment: 'PostgREST OpenAPI root: /rest/v1/ -> <http://rest:3000/> (admin only). See <https://github.com/orgs/supabase/discussions/42949>'
+ url: http://rest:3000/
+ routes:
+ - name: rest-v1-openapi-root
+ strip_path: true
+ expression: 'http.path == "/rest/v1/"'
+ plugins:
+ - name: cors
+ - name: key-auth
+ config:
+ hide_credentials: false
+ - name: request-transformer
+ config:
+ add:
+ headers:
+ - "Authorization: $LUA_AUTH_EXPR"
+ replace:
+ headers:
+ - "Authorization: $LUA_AUTH_EXPR"
+ - name: acl
+ config:
+ hide_groups_header: true
+ allow:
+ - admin
+
## Secure PostgREST routes
- name: rest-v1
_comment: 'PostgREST: /rest/v1/* -> http://rest:3000/*'