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 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
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_FORBIDDEN_CONTENT'

export class TimeoutError extends Error {
/**
* @param {string} message
Expand Down
14 changes: 11 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 @@ -104,12 +105,20 @@ export async function gatewayGet(request, env, ctx) {
])
}

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

ctx.waitUntil(
(async () => {
const contentLengthMb = Number(
winnerGwResponse.response.headers.get('content-length')
)

await Promise.all([
storeWinnerGwResponse(request, env, winnerGwResponse),
settleGatewayRequests(),
Expand Down Expand Up @@ -166,7 +175,6 @@ export async function gatewayGet(request, env, ctx) {
throw new TimeoutError()
}
}

throw err
}
}
Expand Down