From 8086be42716bdeeb37aa1cafca3fbcb4b161fbcb Mon Sep 17 00:00:00 2001 From: Erik Dubbelboer Date: Wed, 23 Feb 2022 12:40:43 +0100 Subject: [PATCH] Warn about unsafe ServeFile usage See: https://github.com/valyala/fasthttp/issues/1226 --- fs.go | 16 ++++++++++++++++ server.go | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/fs.go b/fs.go index 257f06604a..72c832aad6 100644 --- a/fs.go +++ b/fs.go @@ -30,6 +30,10 @@ import ( // with good compression ratio. // // See also RequestCtx.SendFileBytes. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func ServeFileBytesUncompressed(ctx *RequestCtx, path []byte) { ServeFileUncompressed(ctx, b2s(path)) } @@ -43,6 +47,10 @@ func ServeFileBytesUncompressed(ctx *RequestCtx, path []byte) { // with good compression ratio. // // See also RequestCtx.SendFile. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func ServeFileUncompressed(ctx *RequestCtx, path string) { ctx.Request.Header.DelBytes(strAcceptEncoding) ServeFile(ctx, path) @@ -62,6 +70,10 @@ func ServeFileUncompressed(ctx *RequestCtx, path string) { // file contents. // // See also RequestCtx.SendFileBytes. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func ServeFileBytes(ctx *RequestCtx, path []byte) { ServeFile(ctx, b2s(path)) } @@ -79,6 +91,10 @@ func ServeFileBytes(ctx *RequestCtx, path []byte) { // Use ServeFileUncompressed is you don't need serving compressed file contents. // // See also RequestCtx.SendFile. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func ServeFile(ctx *RequestCtx, path string) { rootFSOnce.Do(func() { rootFSHandler = rootFS.NewRequestHandler() diff --git a/server.go b/server.go index 82bc010de5..1a8ba8c062 100644 --- a/server.go +++ b/server.go @@ -1338,6 +1338,10 @@ func (ctx *RequestCtx) ResetBody() { // SendFile logs all the errors via ctx.Logger. // // See also ServeFile, FSHandler and FS. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func (ctx *RequestCtx) SendFile(path string) { ServeFile(ctx, path) } @@ -1349,6 +1353,10 @@ func (ctx *RequestCtx) SendFile(path string) { // SendFileBytes logs all the errors via ctx.Logger. // // See also ServeFileBytes, FSHandler and FS. +// +// WARNING: do not pass any user supplied paths to this function! +// WARNING: if path is based on user input users will be able to request +// any file on your filesystem! Use fasthttp.FS with a sane Root instead. func (ctx *RequestCtx) SendFileBytes(path []byte) { ServeFileBytes(ctx, path) }