Harden the containers
This commit is contained in:
+58
-22
@@ -1,7 +1,7 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
import 'dotenv/config'
|
||||
import express, { Router } from 'express'
|
||||
import express, { RequestHandler, Router } from 'express'
|
||||
import { Request } from 'express-serve-static-core'
|
||||
import { resolve } from 'path'
|
||||
import dayjs from 'dayjs'
|
||||
@@ -35,9 +35,11 @@ import { getShare } from './share'
|
||||
import { ASSET_VERSION } from './version'
|
||||
import { UploadPage } from './view/upload'
|
||||
|
||||
// Must run before any code that calls getConfigOption. The bundled
|
||||
// config.json sits one level above this file, which is /app/config.json in
|
||||
// the image.
|
||||
/*
|
||||
Must run before any code that calls getConfigOption. The bundled
|
||||
config.json sits one level above this file, which is /app/config.json in
|
||||
the image.
|
||||
*/
|
||||
loadConfig({ defaultPath: resolve(__dirname, '../config.json') })
|
||||
|
||||
const receiveUpload = createUploadReceiver()
|
||||
@@ -46,7 +48,35 @@ const inProduction = process.env.NODE_ENV === 'production'
|
||||
const app = express()
|
||||
app.disable('x-powered-by')
|
||||
|
||||
// Reachable at the root whatever the mount prefix, so one compose healthcheck fits both routing shapes
|
||||
/*
|
||||
The server has no whole-request timeout (see `requestTimeout` at the end),
|
||||
so nothing else stops a sender trickling a body to a route that has already
|
||||
answered. A request that carries a body closes its connection once the
|
||||
response is sent, read or not. Each upload is its own request, so no
|
||||
visitor loses a connection worth keeping.
|
||||
*/
|
||||
app.use((req, res, next) => {
|
||||
if (req.headers['content-length'] || req.headers['transfer-encoding']) res.set('Connection', 'close')
|
||||
next()
|
||||
})
|
||||
|
||||
/**
|
||||
* Drop a request that has no response within `ms`. For a route that reads
|
||||
* its body before answering: with no whole-request timeout, a slow sender
|
||||
* would otherwise hold it open forever.
|
||||
*/
|
||||
function bodyDeadline (ms: number): RequestHandler {
|
||||
return (req, res, next) => {
|
||||
const timer = setTimeout(() => req.destroy(), ms)
|
||||
res.once('close', () => clearTimeout(timer))
|
||||
next()
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
[ROUTE] Healthcheck, reachable at the root whatever the mount prefix, so one
|
||||
compose healthcheck fits both routing shapes
|
||||
*/
|
||||
app.get(['/healthcheck', '/share/healthcheck'], asyncHandler(healthcheck))
|
||||
|
||||
const router = Router()
|
||||
@@ -65,7 +95,10 @@ router.use((_req, res, next) => {
|
||||
})
|
||||
router.use(sessionMiddleware({ name: 'upload-session' }))
|
||||
|
||||
// Before the share routes, which would otherwise read "healthcheck" as a key
|
||||
/*
|
||||
[ROUTE] Healthcheck under the mount prefix. Before the share routes, which
|
||||
would otherwise read "healthcheck" as a key
|
||||
*/
|
||||
router.get(['/healthcheck', '/share/healthcheck'], asyncHandler(healthcheck))
|
||||
|
||||
type ShareResolution =
|
||||
@@ -84,8 +117,8 @@ async function resolveShare (req: Request): Promise<ShareResolution> {
|
||||
}
|
||||
|
||||
/*
|
||||
* [ROUTE] Upload page, or the password page for a locked share
|
||||
*/
|
||||
[ROUTE] Upload page, or the password page for a locked share
|
||||
*/
|
||||
router.get('/:shareType(share|s)/:key', decodeCookie, asyncHandler(async (req, res) => {
|
||||
const key = req.params.key
|
||||
const resolved = await resolveShare(req)
|
||||
@@ -111,17 +144,16 @@ router.get('/:shareType(share|s)/:key', decodeCookie, asyncHandler(async (req, r
|
||||
}))
|
||||
|
||||
/*
|
||||
* [ROUTE] Password unlock from the password page
|
||||
*/
|
||||
router.post('/share/unlock', express.json({ limit: '10kb' }), unlockHandler)
|
||||
[ROUTE] Password unlock from the password page
|
||||
*/
|
||||
router.post('/share/unlock', bodyDeadline(10_000), express.json({ limit: '10kb' }), unlockHandler)
|
||||
|
||||
/*
|
||||
* [ROUTE] One file from a visitor, streamed to Immich
|
||||
*/
|
||||
[ROUTE] One file from a visitor, streamed to Immich
|
||||
*/
|
||||
router.post('/:shareType(share|s)/:key/upload', decodeCookie, asyncHandler(async (req, res) => {
|
||||
const resolved = await resolveShare(req)
|
||||
if (!resolved.ok) {
|
||||
res.set('Connection', 'close')
|
||||
respondToInvalidRequest(res, 404, resolved.reason)
|
||||
return
|
||||
}
|
||||
@@ -129,17 +161,17 @@ router.post('/:shareType(share|s)/:key/upload', decodeCookie, asyncHandler(async
|
||||
}))
|
||||
|
||||
/*
|
||||
* The same routes under `/upload` and at the root, so either reverse proxy
|
||||
* shape works with no URL configuration: a hostname of its own forwards
|
||||
* `/share/<key>`, a path on the IPP hostname forwards `/upload/share/<key>`.
|
||||
* Handlers read the prefix they were reached by from `req.baseUrl`.
|
||||
*/
|
||||
The same routes under `/upload` and at the root, so either reverse proxy
|
||||
shape works with no URL configuration: a hostname of its own forwards
|
||||
`/share/<key>`, a path on the IPP hostname forwards `/upload/share/<key>`.
|
||||
Handlers read the prefix they were reached by from `req.baseUrl`.
|
||||
*/
|
||||
app.use('/upload', router)
|
||||
app.use('/', router)
|
||||
|
||||
/*
|
||||
* Everything else, including `/`, gets the 404 policy
|
||||
*/
|
||||
[ROUTE] Everything else, including `/`, gets the 404 policy
|
||||
*/
|
||||
app.all('*', (req, res) => {
|
||||
addNoStoreHeaders(res)
|
||||
respondToInvalidRequest(res, 404, 'Invalid route ' + req.path)
|
||||
@@ -166,5 +198,9 @@ const server = app.listen(port, () => {
|
||||
console.log(dayjs().format() + ' Upload service started on port ' + port)
|
||||
enforceMinimumImmichVersion().catch(() => {})
|
||||
})
|
||||
// A large video on a phone uplink outlasts any whole-request limit; the body has an idle timeout instead
|
||||
/*
|
||||
A large video on a phone uplink outlasts any whole-request limit; the body
|
||||
has an idle timeout instead, and the connection rules at the top of this
|
||||
file cover every other route
|
||||
*/
|
||||
server.requestTimeout = 0
|
||||
|
||||
Reference in New Issue
Block a user