Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions gateway/src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,19 @@ export async function apiKeyAuth(
const authorization = request.headers.get('authorization')
const xApiKey = request.headers.get('x-api-key')

let authHeader: string | null = null
if (authorization && xApiKey) {
throw new ResponseError(401, 'Unauthorized - Both Authorization and X-API-Key headers are present, use only one')
if (startsWithPaig(authorization) && !startsWithPaig(xApiKey)) {
authHeader = authorization
} else if (!startsWithPaig(authorization) && startsWithPaig(xApiKey)) {
authHeader = xApiKey
} else {
throw new ResponseError(401, 'Unauthorized - Both Authorization and X-API-Key headers are present, use only one')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

only worth complaining if they have different values

}
} else {
authHeader = authorization || xApiKey
}

const authHeader = authorization || xApiKey

let key: string
if (authHeader) {
if (authHeader.toLowerCase().startsWith('bearer ')) {
Expand Down Expand Up @@ -84,3 +91,6 @@ export async function changeProjectState(project: number, options: Pick<GatewayO

const apiKeyCacheKey = (key: string, kvVersion: string) => `apiKeyAuth:${kvVersion}:${key}`
const projectStateCacheKey = (project: number, kvVersion: string) => `projectState:${kvVersion}:${project}`

const startsWithPaig = (headerValue: string | null): headerValue is 'Bearer paig' | 'paig' =>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

surely headerValue is 'Bearer paig' | 'paig' is wrong, that would mean it's literally one of those exact values?

headerValue?.startsWith('Bearer paig') || headerValue?.startsWith('paig') || false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
headerValue?.startsWith('Bearer paig') || headerValue?.startsWith('paig') || false
headerValue?.toLowerCase().startsWith('bearer paig_') || headerValue?.startsWith('paig_') || false