mirror of
https://github.com/gtsteffaniak/filebrowser.git
synced 2026-11-16 22:15:55 -05:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fe4ebbf86b |
No files matched your search
@@ -2,15 +2,6 @@
|
||||
|
||||
All notable changes to this project will be documented in this file. For commit guidelines, please refer to [Standard Version](https://github.com/conventional-changelog/standard-version).
|
||||
|
||||
## v2.1.6
|
||||
|
||||
**Security**:
|
||||
- [High] Archive extraction now bounds symlink target resolution under the extract destination and rejects absolute or `..` link text, including chained symlinks created during the same extract, closing a follow-up escape path related to GHSA-jg32-ppcc-ph62 (#3404)
|
||||
|
||||
**Bug Fixes**:
|
||||
- fix(utils): `IsTextFile` reads only an 8KB sample instead of the whole file, avoiding excessive memory use when classifying large files for the editor, file watcher, and subtitle checks (#3405)
|
||||
- fix(frontend): standard source usage bar (`source` and `source-alt` sidebar categories) now shows per-user scope quota (used vs cap) from `GET /api/settings/sources` instead of the parent source partition size when a scope quota is configured, and keeps that quota across realtime `sourceUpdate` events (#3402)
|
||||
|
||||
## v2.1.5
|
||||
|
||||
**Security**:
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -16,11 +15,21 @@ import (
|
||||
func IsTextFile(realPath string) (bool, error) {
|
||||
const sampleSize = 8192 // Read first 8KB to check
|
||||
const maxNullByteRatio = 0.05 // Reject if more than 5% null bytes
|
||||
// Read only the sample: never load the whole file, which can be several GB.
|
||||
sample, err := readSample(realPath, sampleSize)
|
||||
// Read only the first sampleSize bytes. The previous implementation
|
||||
// used os.ReadFile, which loaded the entire file into memory and
|
||||
// only then sliced off the first 8KB — so checking a multi-GB file
|
||||
// (e.g. on a file-watcher event) read the whole file for no reason.
|
||||
f, err := os.Open(realPath)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer f.Close()
|
||||
buf := make([]byte, sampleSize)
|
||||
n, err := io.ReadFull(f, buf)
|
||||
if err != nil && err != io.EOF && err != io.ErrUnexpectedEOF {
|
||||
return false, err
|
||||
}
|
||||
sample := buf[:n]
|
||||
// Empty files are considered text
|
||||
if len(sample) == 0 {
|
||||
return true, nil
|
||||
@@ -67,19 +76,3 @@ func IsTextFile(realPath string) (bool, error) {
|
||||
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// readSample returns at most n bytes from the start of the file.
|
||||
func readSample(realPath string, n int) ([]byte, error) {
|
||||
f, err := os.Open(realPath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
buf := make([]byte, n)
|
||||
read, err := io.ReadFull(f, buf)
|
||||
if err != nil && !errors.Is(err, io.EOF) && !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
return nil, err
|
||||
}
|
||||
return buf[:read], nil
|
||||
}
|
||||
@@ -3,7 +3,6 @@ package utils
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
@@ -28,6 +27,18 @@ func TestIsTextFile(t *testing.T) {
|
||||
content: append([]byte("%PDF-1.3\n"+asciiPageStream), 0xFF, 0xD8, 0xFF, 0xE0),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
// Only the first 8KB is inspected, so a large text file is text.
|
||||
name: "large text file larger than the sample",
|
||||
content: []byte(strings.Repeat("hello world\n", 10000)),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
// Likewise, binary content past the first 8KB does not matter.
|
||||
name: "binary content only after the sample",
|
||||
content: append([]byte(strings.Repeat("plain text\n", 750)), make([]byte, 64)...),
|
||||
want: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
@@ -47,39 +58,3 @@ func TestIsTextFile(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestIsTextFileReadsOnlySample guards against loading the whole file into memory:
|
||||
// on a multi-GB file (e.g. a video opened in the file watcher) that exhausts RAM.
|
||||
func TestIsTextFileReadsOnlySample(t *testing.T) {
|
||||
const fileSize = 256 << 20 // 256 MiB, sparse: no real disk usage
|
||||
|
||||
path := filepath.Join(t.TempDir(), "large.log")
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
t.Fatalf("creating sample: %v", err)
|
||||
}
|
||||
if _, err = f.WriteString(strings.Repeat("log line\n", 1024)); err != nil {
|
||||
t.Fatalf("writing sample: %v", err)
|
||||
}
|
||||
if err = f.Truncate(fileSize); err != nil {
|
||||
t.Fatalf("growing sample: %v", err)
|
||||
}
|
||||
if err = f.Close(); err != nil {
|
||||
t.Fatalf("closing sample: %v", err)
|
||||
}
|
||||
|
||||
var before, after runtime.MemStats
|
||||
runtime.ReadMemStats(&before)
|
||||
got, err := IsTextFile(path)
|
||||
runtime.ReadMemStats(&after)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("IsTextFile: %v", err)
|
||||
}
|
||||
if !got {
|
||||
t.Fatalf("IsTextFile(large text file) = false, want true")
|
||||
}
|
||||
if allocated := after.TotalAlloc - before.TotalAlloc; allocated > 1<<20 {
|
||||
t.Fatalf("IsTextFile allocated %d bytes for a %d-byte file, want only the sample", allocated, fileSize)
|
||||
}
|
||||
}
|
||||
@@ -1163,157 +1163,30 @@ func applyArchivedTimesAndPerm(path string, mode fs.FileMode, modTime time.Time)
|
||||
}
|
||||
}
|
||||
|
||||
// Bounds symlink target resolution during archive extraction (GHSA-jg32 / #3384).
|
||||
const (
|
||||
maxSymlinkResolveDepth = 40
|
||||
maxSymlinkResolveExpansions = 255
|
||||
)
|
||||
|
||||
// symlinkTargetStaysUnderDest rejects absolute targets and symlink text that would resolve
|
||||
// outside destRoot after following symlinks already created under the destination tree.
|
||||
// Lexical filepath.Clean alone is insufficient: a chained link can pass a prefix check yet
|
||||
// resolve outside dest when an intermediate path component is a symlink.
|
||||
// symlinkTargetStaysUnderDest rejects absolute targets and paths that would resolve outside destRoot.
|
||||
func symlinkTargetStaysUnderDest(destRoot, destPath, linkname string) error {
|
||||
linkname = strings.TrimSpace(linkname)
|
||||
if linkname == "" {
|
||||
return fmt.Errorf("empty symlink target")
|
||||
}
|
||||
if filepath.IsAbs(linkname) {
|
||||
return fmt.Errorf("symlink has absolute target")
|
||||
}
|
||||
if symlinkTargetHasDotDot(linkname) {
|
||||
return fmt.Errorf("symlink target escapes destination directory")
|
||||
}
|
||||
destResolved, err := filepath.EvalSymlinks(destRoot)
|
||||
destAbs, err := filepath.Abs(destRoot)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
linkDir, err := resolvedSymlinkParentDir(destPath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("symlink target escapes destination directory")
|
||||
}
|
||||
expansionsLeft := maxSymlinkResolveExpansions
|
||||
resolved, err := resolveSymlinkTargetAt(linkDir, linkname, 0, &expansionsLeft)
|
||||
resolved := filepath.Clean(filepath.Join(filepath.Dir(destPath), linkname))
|
||||
resAbs, err := filepath.Abs(resolved)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rel, err := filepath.Rel(filepath.Clean(destResolved), filepath.Clean(resolved))
|
||||
if err != nil {
|
||||
return fmt.Errorf("symlink target escapes destination directory")
|
||||
}
|
||||
sep := string(filepath.Separator)
|
||||
if rel == ".." || strings.HasPrefix(rel, ".."+sep) {
|
||||
if resAbs != destAbs && !strings.HasPrefix(resAbs, destAbs+sep) {
|
||||
return fmt.Errorf("symlink target escapes destination directory")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// symlinkTargetHasDotDot reports whether linkname contains a ".." path segment before cleaning.
|
||||
func symlinkTargetHasDotDot(linkname string) bool {
|
||||
linkname = filepath.ToSlash(strings.TrimSpace(linkname))
|
||||
for _, seg := range strings.Split(linkname, "/") {
|
||||
if seg == ".." {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// resolvedSymlinkParentDir returns the real directory that will host the new symlink at destPath.
|
||||
func resolvedSymlinkParentDir(destPath string) (string, error) {
|
||||
linkPath, err := resolvedLinkPath(destPath)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Dir(linkPath), nil
|
||||
}
|
||||
|
||||
func errSymlinkTargetEscape() error {
|
||||
return fmt.Errorf("symlink target escapes destination directory")
|
||||
}
|
||||
|
||||
func consumeSymlinkExpansion(expansionsLeft *int) error {
|
||||
if expansionsLeft == nil {
|
||||
return nil
|
||||
}
|
||||
if *expansionsLeft <= 0 {
|
||||
return errSymlinkTargetEscape()
|
||||
}
|
||||
*expansionsLeft--
|
||||
return nil
|
||||
}
|
||||
|
||||
// resolveSymlinkTargetAt walks linkname relative to linkDir, following existing symlinks at
|
||||
// each component without collapsing ".." lexically first (closes chained-link extraction bypass).
|
||||
// expansionsLeft is decremented on every symlink expansion and shared across recursion.
|
||||
func resolveSymlinkTargetAt(linkDir, linkname string, depth int, expansionsLeft *int) (string, error) {
|
||||
if depth > maxSymlinkResolveDepth {
|
||||
return "", errSymlinkTargetEscape()
|
||||
}
|
||||
if filepath.IsAbs(linkname) {
|
||||
return resolveSymlinkTargetAbsolute(linkname, depth, expansionsLeft)
|
||||
}
|
||||
linkDir = filepath.Clean(linkDir)
|
||||
cur := linkDir
|
||||
for _, part := range strings.Split(filepath.ToSlash(linkname), "/") {
|
||||
if part == "" || part == "." {
|
||||
continue
|
||||
}
|
||||
switch part {
|
||||
case "..":
|
||||
cur = filepath.Dir(cur)
|
||||
default:
|
||||
next := filepath.Join(cur, part)
|
||||
info, err := os.Lstat(next)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
cur = next
|
||||
continue
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
if info.Mode()&os.ModeSymlink != 0 {
|
||||
if err := consumeSymlinkExpansion(expansionsLeft); err != nil {
|
||||
return "", err
|
||||
}
|
||||
target, err := os.Readlink(next)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var nested string
|
||||
if filepath.IsAbs(target) {
|
||||
nested, err = resolveSymlinkTargetAbsolute(target, depth+1, expansionsLeft)
|
||||
} else {
|
||||
nested, err = resolveSymlinkTargetAt(filepath.Dir(next), target, depth+1, expansionsLeft)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
cur = nested
|
||||
continue
|
||||
}
|
||||
cur = next
|
||||
}
|
||||
}
|
||||
return filepath.Clean(cur), nil
|
||||
}
|
||||
|
||||
// resolveSymlinkTargetAbsolute walks an absolute path from the volume root so every path
|
||||
// component is resolved, including ancestor symlinks (a single Lstat on the full path would skip them).
|
||||
// The path must not be filepath.Clean'd before walking or ".." segments collapse before symlinks are seen.
|
||||
func resolveSymlinkTargetAbsolute(path string, depth int, expansionsLeft *int) (string, error) {
|
||||
if depth > maxSymlinkResolveDepth {
|
||||
return "", errSymlinkTargetEscape()
|
||||
}
|
||||
path = filepath.FromSlash(filepath.ToSlash(path))
|
||||
root := filepath.VolumeName(path) + string(filepath.Separator)
|
||||
if !strings.HasPrefix(path, root) {
|
||||
return "", errSymlinkTargetEscape()
|
||||
}
|
||||
rel := path[len(root):]
|
||||
return resolveSymlinkTargetAt(root, rel, depth, expansionsLeft)
|
||||
}
|
||||
|
||||
// extractArchivedDir creates a directory from an archive entry and reapplies mode and mtime.
|
||||
func extractArchivedDir(destPath string, mode fs.FileMode, modTime time.Time) error {
|
||||
perm := mode.Perm()
|
||||
|
||||
@@ -4,13 +4,11 @@ import (
|
||||
"archive/tar"
|
||||
"archive/zip"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
commonerrors "github.com/gtsteffaniak/filebrowser/backend/internal/errors"
|
||||
"github.com/gtsteffaniak/filebrowser/backend/pkg/indexing"
|
||||
@@ -565,317 +563,6 @@ func TestExtractZip_symlinkDestination(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func writeChainedSymlinkEscapeTar(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
writeTestTarGz(t, path, func(tw *tar.Writer) error {
|
||||
if err := tw.WriteHeader(&tar.Header{Name: "a/b/", Mode: 0o755, Typeflag: tar.TypeDir}); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tw.WriteHeader(&tar.Header{Name: "a/b/l1", Mode: 0o777, Typeflag: tar.TypeSymlink, Linkname: "../.."}); err != nil {
|
||||
return err
|
||||
}
|
||||
return tw.WriteHeader(&tar.Header{Name: "esc", Mode: 0o777, Typeflag: tar.TypeSymlink, Linkname: "a/b/l1/../x"})
|
||||
})
|
||||
}
|
||||
|
||||
func writeChainedSymlinkEscapeZip(t *testing.T, path string) {
|
||||
t.Helper()
|
||||
writeTestZip(t, path, func(zw *zip.Writer) error {
|
||||
hdr := &zip.FileHeader{Name: "a/b/l1", Method: zip.Store}
|
||||
hdr.SetMode(0o777 | os.ModeSymlink)
|
||||
w, err := zw.CreateHeader(hdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = w.Write([]byte("../..")); err != nil {
|
||||
return err
|
||||
}
|
||||
hdr = &zip.FileHeader{Name: "esc", Method: zip.Store}
|
||||
hdr.SetMode(0o777 | os.ModeSymlink)
|
||||
w, err = zw.CreateHeader(hdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write([]byte("a/b/l1/../x"))
|
||||
return err
|
||||
})
|
||||
}
|
||||
|
||||
func assertNoEscapingSymlinksUnderDest(t *testing.T, destDir string) {
|
||||
t.Helper()
|
||||
destResolved, err := filepath.EvalSymlinks(destDir)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sep := string(filepath.Separator)
|
||||
walkErr := filepath.WalkDir(destDir, func(path string, d os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if d.Type()&os.ModeSymlink == 0 {
|
||||
return nil
|
||||
}
|
||||
target, err := os.Readlink(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
linkDir, err := resolvedSymlinkParentDir(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
expansionsLeft := maxSymlinkResolveExpansions
|
||||
resolved, err := resolveSymlinkTargetAt(linkDir, target, 0, &expansionsLeft)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
rel, err := filepath.Rel(destResolved, resolved)
|
||||
if err != nil || rel == ".." || strings.HasPrefix(rel, ".."+sep) {
|
||||
t.Fatalf("symlink %q resolves outside destination to %q", path, resolved)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if walkErr != nil {
|
||||
t.Fatal(walkErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_rejectsChainedRelativeEscape(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(destDir, "a", "b"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
l1 := filepath.Join(destDir, "a", "b", "l1")
|
||||
if err := os.Symlink("../..", l1); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
escPath := filepath.Join(destDir, "esc")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, escPath, "a/b/l1/../x"); err == nil {
|
||||
t.Fatal("expected chained symlink target to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractZip_rejectsChainedSymlinkEscape(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
zipPath := filepath.Join(t.TempDir(), "chain.zip")
|
||||
writeChainedSymlinkEscapeZip(t, zipPath)
|
||||
err := extractZip(zipPath, destDir)
|
||||
if err == nil || !strings.Contains(err.Error(), "escapes destination") {
|
||||
t.Fatalf("expected rejection, got %v", err)
|
||||
}
|
||||
if _, statErr := os.Lstat(filepath.Join(destDir, "esc")); !os.IsNotExist(statErr) {
|
||||
t.Fatal("escaping symlink should not be created")
|
||||
}
|
||||
assertNoEscapingSymlinksUnderDest(t, destDir)
|
||||
}
|
||||
|
||||
func TestExtractTarGz_rejectsChainedSymlinkEscape(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
archivePath := filepath.Join(t.TempDir(), "chain.tar.gz")
|
||||
writeChainedSymlinkEscapeTar(t, archivePath)
|
||||
err := extractTarGz(archivePath, destDir, nil)
|
||||
if err == nil || !strings.Contains(err.Error(), "escapes destination") {
|
||||
t.Fatalf("expected rejection, got %v", err)
|
||||
}
|
||||
if _, statErr := os.Lstat(filepath.Join(destDir, "esc")); !os.IsNotExist(statErr) {
|
||||
t.Fatal("escaping symlink should not be created")
|
||||
}
|
||||
assertNoEscapingSymlinksUnderDest(t, destDir)
|
||||
}
|
||||
|
||||
func TestExtractZip_rejectsChainedSymlinkEscape_preExistingHop(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
if err := os.MkdirAll(filepath.Join(destDir, "a", "b"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink("../..", filepath.Join(destDir, "a", "b", "l1")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
zipPath := filepath.Join(t.TempDir(), "hop.zip")
|
||||
writeTestZip(t, zipPath, func(zw *zip.Writer) error {
|
||||
hdr := &zip.FileHeader{Name: "esc", Method: zip.Store}
|
||||
hdr.SetMode(0o777 | os.ModeSymlink)
|
||||
w, err := zw.CreateHeader(hdr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = w.Write([]byte("a/b/l1/../x"))
|
||||
return err
|
||||
})
|
||||
err := extractZip(zipPath, destDir)
|
||||
if err == nil || !strings.Contains(err.Error(), "escapes destination") {
|
||||
t.Fatalf("expected rejection, got %v", err)
|
||||
}
|
||||
if _, statErr := os.Lstat(filepath.Join(destDir, "esc")); !os.IsNotExist(statErr) {
|
||||
t.Fatal("escaping symlink should not be created")
|
||||
}
|
||||
assertNoEscapingSymlinksUnderDest(t, destDir)
|
||||
}
|
||||
|
||||
func TestResolveSymlinkTargetAt_rejectsSymlinkExpansionBomb(t *testing.T) {
|
||||
dest := t.TempDir()
|
||||
if err := os.Symlink(".", filepath.Join(dest, "l0")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prev := "l0"
|
||||
for i := 1; i <= 25; i++ {
|
||||
name := fmt.Sprintf("l%d", i)
|
||||
target := prev + "/" + prev
|
||||
if err := os.Symlink(target, filepath.Join(dest, name)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
prev = name
|
||||
}
|
||||
expansionsLeft := maxSymlinkResolveExpansions
|
||||
start := time.Now()
|
||||
_, err := resolveSymlinkTargetAt(dest, "l25/l25", 0, &expansionsLeft)
|
||||
elapsed := time.Since(start)
|
||||
if err == nil || !strings.Contains(err.Error(), "escapes destination") {
|
||||
t.Fatalf("expected rejection, got %v after %v", err, elapsed)
|
||||
}
|
||||
if elapsed > 2*time.Second {
|
||||
t.Fatalf("symlink expansion bomb took too long: %v", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_rejectsAbsolutePreExistingHop(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
outside := filepath.Join(filepath.Dir(destDir), "outside-abs-hop")
|
||||
if err := os.MkdirAll(outside, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(outside, filepath.Join(destDir, "bad")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newLink := filepath.Join(destDir, "newlink")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, newLink, "bad"); err == nil {
|
||||
t.Fatal("expected absolute pre-existing hop outside dest to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_hopTargetVerbatimReadlink(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
allowed := filepath.Join(destDir, "allowed.txt")
|
||||
if err := os.WriteFile(allowed, []byte("ok"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hop := filepath.Join(destDir, "hop")
|
||||
// Trailing space must be preserved (no TrimSpace on Readlink); the literal name is not allowed.txt.
|
||||
if err := os.Symlink("allowed.txt ", hop); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := os.Readlink(hop)
|
||||
if err != nil || got != "allowed.txt " {
|
||||
t.Fatalf("readlink verbatim target: %q err=%v", got, err)
|
||||
}
|
||||
expansionsLeft := maxSymlinkResolveExpansions
|
||||
resolved, err := resolveSymlinkTargetAt(destDir, "hop", 0, &expansionsLeft)
|
||||
if err != nil {
|
||||
t.Fatalf("resolve hop: %v", err)
|
||||
}
|
||||
want := filepath.Join(destDir, "allowed.txt ")
|
||||
if resolved != want {
|
||||
t.Fatalf("hop should resolve to literal spaced name %q, got %q", want, resolved)
|
||||
}
|
||||
if _, statErr := os.Stat(resolved); !os.IsNotExist(statErr) {
|
||||
t.Fatalf("spaced target should not alias allowed.txt without trimming: stat %v", statErr)
|
||||
}
|
||||
if _, statErr := os.Stat(allowed); statErr != nil {
|
||||
t.Fatalf("allowed.txt should exist: %v", statErr)
|
||||
}
|
||||
}
|
||||
|
||||
func setupAbsoluteHopThroughOutsidePivot(t *testing.T) (destDir, outside string, hopLeafAbs string) {
|
||||
t.Helper()
|
||||
destDir = t.TempDir()
|
||||
outside = filepath.Join(filepath.Dir(destDir), "abs-pivot-outside")
|
||||
if err := os.MkdirAll(outside, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pivot := filepath.Join(destDir, "pivot")
|
||||
if err := os.Symlink(outside, pivot); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
leafAbs, err := filepath.Abs(filepath.Join(pivot, "leaf"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return destDir, outside, leafAbs
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_rejectsAbsoluteHopThroughOutsidePivot_fileExists(t *testing.T) {
|
||||
destDir, outside, hopLeafAbs := setupAbsoluteHopThroughOutsidePivot(t)
|
||||
if err := os.WriteFile(filepath.Join(outside, "leaf"), []byte("secret"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.Symlink(hopLeafAbs, filepath.Join(destDir, "hop")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newLink := filepath.Join(destDir, "newlink")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, newLink, "hop"); err == nil {
|
||||
t.Fatal("expected rejection when hop resolves through outside pivot to existing file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_rejectsAbsoluteHopThroughOutsidePivot_fileMissing(t *testing.T) {
|
||||
destDir, _, hopLeafAbs := setupAbsoluteHopThroughOutsidePivot(t)
|
||||
if err := os.Symlink(hopLeafAbs, filepath.Join(destDir, "hop")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newLink := filepath.Join(destDir, "newlink")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, newLink, "hop"); err == nil {
|
||||
t.Fatal("expected rejection when hop resolves through outside pivot to missing leaf")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_rejectsAbsoluteHopWithDotDotAfterOutsidePivot(t *testing.T) {
|
||||
destDir, outside, _ := setupAbsoluteHopThroughOutsidePivot(t)
|
||||
sibling := filepath.Join(filepath.Dir(outside), "sibling-out")
|
||||
if err := os.MkdirAll(sibling, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pivot := filepath.Join(destDir, "pivot")
|
||||
sep := string(filepath.Separator)
|
||||
// Build without filepath.Clean/Join collapsing pivot before sibling resolution.
|
||||
hopTarget := pivot + sep + ".." + sep + filepath.Base(outside) + sep + ".." + sep + filepath.Base(sibling)
|
||||
if err := os.Symlink(hopTarget, filepath.Join(destDir, "hop")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newLink := filepath.Join(destDir, "newlink")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, newLink, "hop"); err == nil {
|
||||
t.Fatal("expected rejection for absolute hop with .. after ancestor symlink")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSymlinkTargetStaysUnderDest_allowsInDestAbsoluteHopChain(t *testing.T) {
|
||||
destDir := t.TempDir()
|
||||
inner := filepath.Join(destDir, "inner")
|
||||
if err := os.MkdirAll(inner, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
targetFile := filepath.Join(inner, "data.txt")
|
||||
if err := os.WriteFile(targetFile, []byte("ok"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
innerAbs, err := filepath.Abs(inner)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Symlink(innerAbs, filepath.Join(destDir, "pivot")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hopTarget, err := filepath.Abs(targetFile)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = os.Symlink(hopTarget, filepath.Join(destDir, "hop")); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newLink := filepath.Join(destDir, "ref")
|
||||
if err := symlinkTargetStaysUnderDest(destDir, newLink, "hop"); err != nil {
|
||||
t.Fatalf("in-dest absolute hop chain should be allowed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArchiveCreateDestination_RejectsEscapingSymlinkLeaf(t *testing.T) {
|
||||
t.Parallel()
|
||||
root := t.TempDir()
|
||||
|
||||
@@ -2,7 +2,6 @@ import { settingsApi } from "@/api/index.js"
|
||||
import i18n from "@/i18n/index.ts"
|
||||
import { notify } from "@/notify/index.ts"
|
||||
import { mutations, state } from "@/store/index.ts"
|
||||
import { getObjectProperty } from "@/utils/object.js"
|
||||
import { globalVars } from "@/utils/constants.js"
|
||||
|
||||
let eventSrc = null
|
||||
@@ -180,13 +179,6 @@ async function eventRouter (eventType, message) {
|
||||
// Parse the JSON string before passing to updateSourceInfo
|
||||
try {
|
||||
const parsedMessage = JSON.parse(message)
|
||||
// SSE entries (ReducedIndex) never include scopeQuota. Keep the quota already loaded
|
||||
// from /api/settings/sources instead of letting the update clear it.
|
||||
for (const [name, entry] of Object.entries(parsedMessage)) {
|
||||
if (entry && typeof entry === 'object' && entry.scopeQuota === undefined) {
|
||||
entry.scopeQuota = getObjectProperty(state.sources.info, name)?.scopeQuota
|
||||
}
|
||||
}
|
||||
mutations.updateSourceInfo(parsedMessage)
|
||||
} catch (err) {
|
||||
console.error('Error parsing sourceUpdate message:', err, message)
|
||||
|
||||
@@ -1,118 +0,0 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { stateMock } = vi.hoisted(() => ({
|
||||
stateMock: {
|
||||
sources: {
|
||||
hasSourceInfo: false,
|
||||
info: {
|
||||
data: {
|
||||
used: 0,
|
||||
total: 50 * 1024 ** 3,
|
||||
usedAlt: 0,
|
||||
totalRoot: 0,
|
||||
usedAltRoot: 0,
|
||||
usedPercentage: 0,
|
||||
status: "ready",
|
||||
name: "data",
|
||||
files: 0,
|
||||
folders: 0,
|
||||
lastIndex: 0,
|
||||
quickScanDurationSeconds: 0,
|
||||
fullScanDurationSeconds: 0,
|
||||
complexity: 0,
|
||||
scanners: [],
|
||||
readOnly: false,
|
||||
private: false,
|
||||
},
|
||||
},
|
||||
},
|
||||
realtimeActive: undefined,
|
||||
realtimeDownCount: 0,
|
||||
user: { username: "alice" },
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock("./state.ts", () => ({ state: stateMock }));
|
||||
vi.mock("./eventBus.ts", () => ({ emitStateChanged: vi.fn() }));
|
||||
vi.mock("@/api/viewToken.js", () => ({ setActiveViewGrantScope: vi.fn() }));
|
||||
vi.mock("@/api/index.js", () => ({ resourcesApi: {}, usersApi: {} }));
|
||||
vi.mock("@/api/settings.js", () => ({
|
||||
getEnforcedUserDefaults: vi.fn(),
|
||||
getShareDefaultsPolicy: vi.fn(),
|
||||
getSidebarLinkDefaultsPolicy: vi.fn(),
|
||||
getToolAccessDefaultsPolicy: vi.fn(),
|
||||
}));
|
||||
vi.mock("@/i18n/index.ts", () => ({ detectLocale: () => "en", setLocale: vi.fn() }));
|
||||
vi.mock("@/notify/index.ts", () => ({ notify: { show: vi.fn() } }));
|
||||
vi.mock("@/utils/index.ts", () => ({ url: {} }));
|
||||
vi.mock("@/utils/mimetype.js", () => ({ getTypeInfo: vi.fn() }));
|
||||
vi.mock("@/utils/object.js", async (importOriginal) => {
|
||||
const actual = await importOriginal();
|
||||
return actual;
|
||||
});
|
||||
vi.mock("@/utils/sort.ts", () => ({ sortedItems: vi.fn() }));
|
||||
vi.mock("@/utils/sidebarCategory.ts", () => ({ isSourceSidebarCategory: vi.fn() }));
|
||||
vi.mock("@/utils/pwaManifest.js", () => ({ updateManifestLink: vi.fn() }));
|
||||
vi.mock("@/utils/theme.js", () => ({ syncEventTheme: vi.fn() }));
|
||||
vi.mock("@/utils/uploadNavigationGuard.ts", () => ({
|
||||
cancelCloseUploadPrompt: vi.fn(),
|
||||
confirmCloseUploadPrompt: vi.fn(),
|
||||
hasCloseWithActiveUploadsPrompt: vi.fn(),
|
||||
}));
|
||||
vi.mock("./getters.ts", () => ({ getters: {} }));
|
||||
|
||||
import { mutations } from "./mutations.ts";
|
||||
|
||||
const SCOPE_LIMIT = 10 * 1024 * 1024;
|
||||
const SCOPE_USED = 2 * 1024 * 1024;
|
||||
|
||||
describe("mutations.updateSourceInfo scope quota", () => {
|
||||
beforeEach(() => {
|
||||
stateMock.sources.hasSourceInfo = false;
|
||||
stateMock.sources.info = {
|
||||
data: {
|
||||
used: 0,
|
||||
total: 50 * 1024 ** 3,
|
||||
usedAlt: 0,
|
||||
totalRoot: 0,
|
||||
usedAltRoot: 0,
|
||||
usedPercentage: 0,
|
||||
status: "ready",
|
||||
name: "data",
|
||||
files: 0,
|
||||
folders: 0,
|
||||
lastIndex: 0,
|
||||
quickScanDurationSeconds: 0,
|
||||
fullScanDurationSeconds: 0,
|
||||
complexity: 0,
|
||||
scanners: [],
|
||||
readOnly: false,
|
||||
private: false,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
it("merges scopeQuota from /api/settings/sources into store source info", () => {
|
||||
mutations.updateSourceInfo({
|
||||
data: {
|
||||
used: 100,
|
||||
total: 50 * 1024 ** 3,
|
||||
status: "ready",
|
||||
name: "data",
|
||||
scopeQuota: {
|
||||
quotaKind: "scope",
|
||||
limitBytes: SCOPE_LIMIT,
|
||||
usedBytes: SCOPE_USED,
|
||||
reservedBytes: 0,
|
||||
meter: "index_scope",
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const info = stateMock.sources.info.data;
|
||||
expect(info.scopeQuota?.limitBytes).toBe(SCOPE_LIMIT);
|
||||
expect(info.scopeQuota?.usedBytes).toBe(SCOPE_USED);
|
||||
expect(info.total).toBe(50 * 1024 ** 3);
|
||||
expect(stateMock.sources.hasSourceInfo).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -252,17 +252,9 @@ export const mutations = {
|
||||
readOnly: source.readOnly || false,
|
||||
private: source.private || false,
|
||||
indexingDisabled: source.indexingDisabled || false,
|
||||
scopeQuota: source.scopeQuota,
|
||||
};
|
||||
info = setObjectProperty(info, k, updated) as Record<string, SourceInfo>;
|
||||
if (
|
||||
updated.total > 0 ||
|
||||
updated.used > 0 ||
|
||||
updated.usedAlt > 0 ||
|
||||
(updated.scopeQuota?.limitBytes ?? 0) > 0
|
||||
) {
|
||||
hasAny = true;
|
||||
}
|
||||
if (updated.total > 0 || updated.used > 0 || updated.usedAlt > 0) hasAny = true;
|
||||
}
|
||||
}
|
||||
state.sources.info = info;
|
||||
@@ -327,19 +319,12 @@ export const mutations = {
|
||||
readOnly: prev ? prev.readOnly : false,
|
||||
private: prev ? prev.private : false,
|
||||
indexingDisabled: prev ? prev.indexingDisabled : false,
|
||||
scopeQuota: prev?.scopeQuota,
|
||||
};
|
||||
}
|
||||
// Sidebar usage bar uses hasSourceInfo + per-source used/total; must survive object replace
|
||||
sources.hasSourceInfo = user.scopes.some((s: UserScope) => {
|
||||
const e = sources.info[s.name];
|
||||
return (
|
||||
e &&
|
||||
(e.total > 0 ||
|
||||
e.used > 0 ||
|
||||
e.usedAlt > 0 ||
|
||||
(e.scopeQuota?.limitBytes ?? 0) > 0)
|
||||
);
|
||||
return e && (e.total > 0 || e.used > 0 || e.usedAlt > 0);
|
||||
});
|
||||
// Check if user has custom sidebar links with sources
|
||||
let targetSource = sources.current;
|
||||
|
||||
@@ -120,18 +120,6 @@ export interface Prompt {
|
||||
cancel?: unknown;
|
||||
}
|
||||
|
||||
/** Scope quota snapshot from GET /api/settings/sources (sidebar usage bar). */
|
||||
export interface ScopeQuotaInfo {
|
||||
quotaKind?: string;
|
||||
limitBytes: number;
|
||||
usedBytes?: number;
|
||||
reservedBytes?: number;
|
||||
meter?: string;
|
||||
configuredMeter?: string;
|
||||
effectiveMeter?: string;
|
||||
measurementStatus?: string;
|
||||
}
|
||||
|
||||
export interface SourceInfo {
|
||||
pathPrefix?: string | undefined;
|
||||
used: number;
|
||||
@@ -153,7 +141,6 @@ export interface SourceInfo {
|
||||
readOnly: boolean;
|
||||
private: boolean;
|
||||
indexingDisabled?: boolean | undefined;
|
||||
scopeQuota?: ScopeQuotaInfo | undefined;
|
||||
}
|
||||
|
||||
/** Raw shape of a single source entry as sent by /api/settings/sources or SSE updates. */
|
||||
@@ -176,7 +163,6 @@ export interface SourceInfoUpdate {
|
||||
readOnly?: boolean;
|
||||
private?: boolean;
|
||||
indexingDisabled?: boolean;
|
||||
scopeQuota?: ScopeQuotaInfo;
|
||||
}
|
||||
|
||||
export interface SourceFilePermissions {
|
||||
|
||||
Reference in new issue
Block a user