Skip to content

Commit d81798f

Browse files
authored
fix: replace auth header for backwards compatibility (#4206)
2 parents 0ad8340 + f5e1774 commit d81798f

File tree

2 files changed

+23
-16
lines changed

2 files changed

+23
-16
lines changed

internal/start/start.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,14 @@ EOF
349349
ApiHost: utils.Config.Hostname,
350350
ApiPort: utils.Config.Api.Port,
351351
BearerToken: fmt.Sprintf(
352-
// Pass down apikey as Authorization header for backwards compatibility with legacy JWT.
353-
// If Authorization header is already set, Kong simply skips evaluating this Lua script.
354-
`$((function() return (headers.apikey == '%s' and 'Bearer %s') or (headers.apikey == '%s' and 'Bearer %s') or headers.apikey end)())`,
352+
// If Authorization header is set to a self-minted JWT, we want to pass it down.
353+
// Legacy supabase-js may set Authorization header to Bearer <apikey>. We must remove it
354+
// to avoid failing JWT validation.
355+
// If Authorization header is missing, we want to match against apikey header to set the
356+
// default JWT for downstream services.
357+
// Finally, the apikey header may be set to a legacy JWT. In that case, we want to copy
358+
// it to Authorization header for backwards compatibility.
359+
`$((function() return (headers.authorization ~= nil and headers.authorization:sub(1, 10) ~= 'Bearer sb_' and headers.authorization) or (headers.apikey == '%s' and 'Bearer %s') or (headers.apikey == '%s' and 'Bearer %s') or headers.apikey end)())`,
355360
utils.Config.Auth.SecretKey.Value,
356361
utils.Config.Auth.ServiceRoleKey.Value,
357362
utils.Config.Auth.PublishableKey.Value,

internal/start/templates/kong.yml

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ services:
1212
- name: cors
1313
- name: request-transformer
1414
config:
15-
add:
15+
replace:
1616
headers:
1717
- "Authorization: {{ .BearerToken }}"
1818
- name: auth-v1-open-callback
@@ -27,7 +27,7 @@ services:
2727
- name: cors
2828
- name: request-transformer
2929
config:
30-
add:
30+
replace:
3131
headers:
3232
- "Authorization: {{ .BearerToken }}"
3333
- name: auth-v1-open-authorize
@@ -42,7 +42,7 @@ services:
4242
- name: cors
4343
- name: request-transformer
4444
config:
45-
add:
45+
replace:
4646
headers:
4747
- "Authorization: {{ .BearerToken }}"
4848
- name: auth-v1
@@ -57,7 +57,7 @@ services:
5757
- name: cors
5858
- name: request-transformer
5959
config:
60-
add:
60+
replace:
6161
headers:
6262
- "Authorization: {{ .BearerToken }}"
6363
- name: rest-v1
@@ -72,7 +72,7 @@ services:
7272
- name: cors
7373
- name: request-transformer
7474
config:
75-
add:
75+
replace:
7676
headers:
7777
- "Authorization: {{ .BearerToken }}"
7878
- name: rest-admin-v1
@@ -87,7 +87,7 @@ services:
8787
- name: cors
8888
- name: request-transformer
8989
config:
90-
add:
90+
replace:
9191
headers:
9292
- "Authorization: {{ .BearerToken }}"
9393
- name: graphql-v1
@@ -105,6 +105,8 @@ services:
105105
add:
106106
headers:
107107
- "Content-Profile: graphql_public"
108+
replace:
109+
headers:
108110
- "Authorization: {{ .BearerToken }}"
109111
- name: realtime-v1-ws
110112
_comment: "Realtime: /realtime/v1/* -> ws://realtime:4000/socket/websocket"
@@ -119,7 +121,7 @@ services:
119121
- name: cors
120122
- name: request-transformer
121123
config:
122-
add:
124+
replace:
123125
headers:
124126
- "Authorization: {{ .BearerToken }}"
125127
- name: realtime-v1-longpoll
@@ -135,7 +137,7 @@ services:
135137
- name: cors
136138
- name: request-transformer
137139
config:
138-
add:
140+
replace:
139141
headers:
140142
- "Authorization: {{ .BearerToken }}"
141143
- name: realtime-v1-rest
@@ -151,7 +153,7 @@ services:
151153
- name: cors
152154
- name: request-transformer
153155
config:
154-
add:
156+
replace:
155157
headers:
156158
- "Authorization: {{ .BearerToken }}"
157159
- name: storage-v1
@@ -166,7 +168,7 @@ services:
166168
- name: cors
167169
- name: request-transformer
168170
config:
169-
add:
171+
replace:
170172
headers:
171173
- "Authorization: {{ .BearerToken }}"
172174
- name: pg-meta
@@ -192,7 +194,7 @@ services:
192194
- name: cors
193195
- name: request-transformer
194196
config:
195-
add:
197+
replace:
196198
headers:
197199
- "Authorization: {{ .BearerToken }}"
198200
- name: analytics-v1
@@ -207,7 +209,7 @@ services:
207209
- name: cors
208210
- name: request-transformer
209211
config:
210-
add:
212+
replace:
211213
headers:
212214
- "Authorization: {{ .BearerToken }}"
213215
- name: pooler-v2-ws
@@ -223,7 +225,7 @@ services:
223225
- name: cors
224226
- name: request-transformer
225227
config:
226-
add:
228+
replace:
227229
headers:
228230
- "Authorization: {{ .BearerToken }}"
229231
- name: mcp

0 commit comments

Comments
 (0)