81460c4a4d
The handler reuses the home-path resolution of GET /files, refuses special filesystems and non-directories, writes with O_EXCL so an existing name is answered 409 instead of overwritten, enforces the chat upload size limit and replies with the stored name, path and size.
150 lines
4.4 KiB
Go
150 lines
4.4 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"net/url"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"unicode/utf8"
|
|
)
|
|
|
|
// maxHomeFileUploadBytes bounds one file stored through POST /files. It
|
|
// matches the chat upload limit so both raw-body routes behave alike.
|
|
const maxHomeFileUploadBytes = maxChatUploadBytes
|
|
|
|
type homeFileUploadReply struct {
|
|
Name string `json:"name"`
|
|
Path string `json:"path"`
|
|
Size int64 `json:"size"`
|
|
}
|
|
|
|
// serveHomeFileUpload stores the request body as a new file inside the
|
|
// folder named by ?path=, which must lie inside the operator's home. The name
|
|
// travels in X-Pipelines-Upload-Name; an existing file is never overwritten.
|
|
func serveHomeFileUpload(w http.ResponseWriter, r *http.Request) {
|
|
home, err := OperatorHomeDirectory()
|
|
if err != nil {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
serveHomeFileUploadTo(w, r, home)
|
|
}
|
|
|
|
func serveHomeFileUploadTo(w http.ResponseWriter, r *http.Request, home string) {
|
|
fail := func(status int, message string) { http.Error(w, message, status) }
|
|
rawName := r.Header.Get(chatUploadHeader)
|
|
if rawName == "" {
|
|
fail(http.StatusBadRequest, "missing "+chatUploadHeader)
|
|
return
|
|
}
|
|
if r.Header.Get("Sec-Fetch-Site") == "cross-site" {
|
|
fail(http.StatusForbidden, "cross-site upload refused")
|
|
return
|
|
}
|
|
decoded, err := url.PathUnescape(rawName)
|
|
if err != nil {
|
|
decoded = rawName
|
|
}
|
|
name, err := uploadFileName(decoded)
|
|
if err != nil {
|
|
fail(http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if r.ContentLength > maxHomeFileUploadBytes {
|
|
fail(http.StatusRequestEntityTooLarge, "file exceeds the 100 MiB limit")
|
|
return
|
|
}
|
|
home = filepath.Clean(home)
|
|
realHome, err := filepath.EvalSymlinks(home)
|
|
if err != nil {
|
|
fail(http.StatusNotFound, "home directory unavailable")
|
|
return
|
|
}
|
|
target, err := expandHomePath(home, r.URL.Query().Get("path"))
|
|
if err != nil {
|
|
fail(http.StatusNotFound, "folder not found")
|
|
return
|
|
}
|
|
_, realDir, err := resolveInsideHome(realHome, target)
|
|
if err != nil {
|
|
if strings.Contains(err.Error(), "outside the home directory") {
|
|
fail(http.StatusForbidden, "folder access denied")
|
|
} else {
|
|
fail(http.StatusNotFound, "folder not found")
|
|
}
|
|
return
|
|
}
|
|
info, err := os.Stat(realDir)
|
|
if err != nil || !info.IsDir() {
|
|
fail(http.StatusNotFound, "folder not found")
|
|
return
|
|
}
|
|
if specialFilesystem(realDir) {
|
|
fail(http.StatusForbidden, "folder access denied")
|
|
return
|
|
}
|
|
file, err := os.OpenFile(filepath.Join(realDir, name), os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o644)
|
|
if err != nil {
|
|
switch {
|
|
case errors.Is(err, fs.ErrExist):
|
|
fail(http.StatusConflict, "a file named "+name+" already exists")
|
|
case errors.Is(err, fs.ErrPermission):
|
|
fail(http.StatusForbidden, "folder access denied")
|
|
default:
|
|
fail(http.StatusInternalServerError, "write file: "+err.Error())
|
|
}
|
|
return
|
|
}
|
|
// The process umask may narrow the create mode; the stored file is meant
|
|
// to be readable like one the operator saved themselves.
|
|
size, err := int64(0), file.Chmod(0o644)
|
|
if err == nil {
|
|
size, err = io.Copy(file, io.LimitReader(r.Body, maxHomeFileUploadBytes+1))
|
|
}
|
|
if err == nil && size > maxHomeFileUploadBytes {
|
|
err = errors.New("file exceeds the 100 MiB limit")
|
|
}
|
|
if err == nil {
|
|
err = file.Sync()
|
|
}
|
|
if closeErr := file.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if err != nil {
|
|
_ = os.Remove(file.Name())
|
|
status := http.StatusBadRequest
|
|
if size > maxHomeFileUploadBytes {
|
|
status = http.StatusRequestEntityTooLarge
|
|
}
|
|
fail(status, err.Error())
|
|
return
|
|
}
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.Header().Set("Cache-Control", "no-store")
|
|
_ = json.NewEncoder(w).Encode(homeFileUploadReply{Name: name, Path: filepath.Join(target, name), Size: size})
|
|
}
|
|
|
|
// uploadFileName accepts one plain file name: no directory parts, no NUL,
|
|
// not "." or "..", and short enough for common filesystems. The name is kept
|
|
// as given rather than rewritten, so the stored file is the one the operator
|
|
// asked for.
|
|
func uploadFileName(value string) (string, error) {
|
|
name := strings.TrimSpace(value)
|
|
switch {
|
|
case name == "" || name == "." || name == "..":
|
|
return "", errors.New("file name is missing")
|
|
case strings.ContainsAny(name, "/\\\x00"):
|
|
return "", errors.New("file name must not contain path separators")
|
|
case !utf8.ValidString(name):
|
|
return "", errors.New("file name is not valid UTF-8")
|
|
case len(name) > 255:
|
|
return "", errors.New("file name is too long")
|
|
}
|
|
return name, nil
|
|
}
|