Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add gateway forbidden content types #46

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions packages/edge-gateway/src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ export const HTTP_STATUS_RATE_LIMITED = 429
export const HTTP_STATUS_SUCCESS = 200
export const REQUEST_PREVENTED_RATE_LIMIT_CODE = 'RATE_LIMIT'
export const TIMEOUT_CODE = 'TIMEOUT'
export const FORBIDDEN_CONTENT_TYPES = ['application/octet-stream']
15 changes: 15 additions & 0 deletions packages/edge-gateway/src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,21 @@ export class InvalidUrlError extends Error {
}
InvalidUrlError.CODE = 'ERROR_INVALID_URL'

export class ForbiddenContentError extends Error {
/**
* @param {string} message
*/
constructor(message = 'Forbidden content') {
const status = 403
super(createErrorHtmlContent(status, message))
this.name = 'ForbiddenContentError'
this.status = status
this.code = ForbiddenContentError.CODE
this.contentType = 'text/html'
}
}
ForbiddenContentError.CODE = 'ERROR_INVALID_URL'
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

export class TimeoutError extends Error {
/**
* @param {string} message
Expand Down
18 changes: 15 additions & 3 deletions packages/edge-gateway/src/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import pAny, { AggregateError } from 'p-any'
import { FilterError } from 'p-some'
import pSettle from 'p-settle'

import { TimeoutError } from './errors.js'
import { TimeoutError, ForbiddenContentError } from './errors.js'
import { getCidFromSubdomainUrl } from './utils/cid.js'
import { toDenyListAnchor } from './utils/deny-list.js'
import {
Expand All @@ -17,6 +17,7 @@ import {
HTTP_STATUS_RATE_LIMITED,
REQUEST_PREVENTED_RATE_LIMIT_CODE,
TIMEOUT_CODE,
FORBIDDEN_CONTENT_TYPES,
} from './constants.js'

/**
Expand Down Expand Up @@ -109,7 +110,6 @@ export async function gatewayGet(request, env, ctx) {
const contentLengthMb = Number(
winnerGwResponse.response.headers.get('content-length')
)

await Promise.all([
storeWinnerGwResponse(request, env, winnerGwResponse),
settleGatewayRequests(),
Expand All @@ -120,6 +120,19 @@ export async function gatewayGet(request, env, ctx) {
})()
)

// Block content types
if (
FORBIDDEN_CONTENT_TYPES.includes(
winnerGwResponse.response.headers.get('content-type')
)
) {
throw new ForbiddenContentError(
`Forbidden content type: ${winnerGwResponse.response.headers.get(
'content-type'
)}`
)

Choose a reason for hiding this comment

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

We need to do this check before the waitUntil above don't we? Otherwise we cache the response and on subsequent requests it'll be served from the cache.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah good catch

}
vasco-santos marked this conversation as resolved.
Show resolved Hide resolved

// forward winner gateway response
return winnerGwResponse.response
} catch (err) {
Expand Down Expand Up @@ -166,7 +179,6 @@ export async function gatewayGet(request, env, ctx) {
throw new TimeoutError()
}
}

throw err
}
}
Expand Down