Skip to content
Merged
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
6 changes: 5 additions & 1 deletion services/print/pod-print/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@
export interface Config {
Port: number
Secret: string
AllowedHostnames: string[]
}

const parseNumber = (str: string | undefined): number | undefined => (str !== undefined ? Number(str) : undefined)

const config: Config = (() => {
const allowedHostnames = process.env.ALLOWED_HOSTNAMES

const params: Partial<Config> = {
Port: parseNumber(process.env.PORT) ?? 4005,
Secret: process.env.SECRET
Secret: process.env.SECRET,
AllowedHostnames: allowedHostnames == null ? [] : allowedHostnames.split(',')
}

const missingEnv = (Object.keys(params) as Array<keyof Config>).filter((key) => params[key] === undefined)
Expand Down
2 changes: 1 addition & 1 deletion services/print/pod-print/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const main = async (): Promise<void> => {
setupMetadata()

const storageConfig = storageConfigFromEnv()
const { app, close } = createServer(storageConfig)
const { app, close } = createServer(storageConfig, config.AllowedHostnames)
const server = listen(app, config.Port)

const shutdown = (): void => {
Expand Down
16 changes: 15 additions & 1 deletion services/print/pod-print/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,13 @@ const wrapRequest = (fn: AsyncRequestHandler) => (req: Request, res: Response, n
handleRequest(fn, req, res, next)
}

export function createServer (storageConfig: StorageConfiguration): { app: Express, close: () => void } {
export function createServer (
storageConfig: StorageConfiguration,
allowedHostnames: string[]
): { app: Express, close: () => void } {
const storageAdapter = buildStorageFromConfig(storageConfig)
const measureCtx = initStatisticsContext('print', {})
const whitelistedHostnames = allowedHostnames.length > 0 ? new Set(allowedHostnames) : null

const app = express()
app.use(cors())
Expand All @@ -124,6 +128,16 @@ export function createServer (storageConfig: StorageConfiguration): { app: Expre
const rawlink = req.query.link as string
const link = decodeURIComponent(rawlink)

// Verify that link is from the same host and protocol is among the allowed
const url = new URL(link)
if (
!['http:', 'https:'].includes(url.protocol) ||
(whitelistedHostnames != null && !whitelistedHostnames.has(url.hostname))
) {
console.error(`Rejected processing unexpected link: ${link}. Token: ${JSON.stringify(token)}`)
throw new ApiError(403, 'Cannot process provided link')
}

const kind = req.query.kind as PrintOptions['kind']

if (kind !== undefined && !validKinds.includes(kind as any)) {
Expand Down
Loading