diff options
| author | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-07-22 23:51:18 +0300 |
| commit | 849951be1d1a7ee9f9302006ccb187bf5b4e36f3 (patch) | |
| tree | 496c924a03a9ea6212e29bb4699e268066ebad81 /internal/io/fs/validatedreadtarget_test.go | |
| parent | bf78b3abffee6d49c08ca2980156afc455994969 (diff) | |
feat: DTail fork — server/client feature development
Squashed development of the snonux/dtail fork's product code (internal/, cmd/)
since diverging from mimecast/dtail. Major areas:
- Read/output path: the former "turbo" channel-less path is now the single,
default server-side read/output path for cat/grep/tail and MapReduce; the old
channel-based path and its config/env toggles were removed.
- MapReduce: single aggregate implementation (server + serverless) fed directly
by a processor pipeline, with input-exhausted finalization via the shutdown
coordinator; high-concurrency and data-race fixes.
- Journal source reads (journal:unit.service) via journalctl, Linux-gated behind
a journal-v1 capability.
- Auth-key fast reconnect: in-memory per-user public-key cache with TTL/max-keys,
registered over an authenticated session (AUTHKEY), checked before
authorized_keys.
- Interactive query reload (--interactive-query) with SESSION START/UPDATE
generation boundaries and capability negotiation.
- Client-side deadlines: --timeout / --shutdownAfter as context deadlines;
follow shutdown handling.
- Client logging: diagnostics-only daily log by default, opt-in payload tee via
--log-payload.
- Numerous correctness fixes (buffer-pool double-recycle races, EOF-sentinel
leaks, glob-expansion cap, TOCTOU in CSV parsing) with accompanying unit tests.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Diffstat (limited to 'internal/io/fs/validatedreadtarget_test.go')
| -rw-r--r-- | internal/io/fs/validatedreadtarget_test.go | 218 |
1 files changed, 218 insertions, 0 deletions
diff --git a/internal/io/fs/validatedreadtarget_test.go b/internal/io/fs/validatedreadtarget_test.go new file mode 100644 index 0000000..30b9c8f --- /dev/null +++ b/internal/io/fs/validatedreadtarget_test.go @@ -0,0 +1,218 @@ +package fs + +import ( + "context" + "io" + "os" + "path/filepath" + "reflect" + "strings" + "testing" + + "github.com/mimecast/dtail/internal/io/dlog" + "github.com/mimecast/dtail/internal/lcontext" + "github.com/mimecast/dtail/internal/regex" +) + +func TestValidatedCatFileStartWithProcessorOptimizedReadsAllLines(t *testing.T) { + resetCommonLogger(t) + + filePath := writeProcessorTestFile(t, "alpha\nbeta\n") + target := mustValidatedReadTarget(t, filePath) + re := regex.NewNoop() + + cat := NewValidatedCatFile(filePath, target, "glob-id", make(chan string, 1), defaultMaxLineLength) + processor := &captureProcessor{} + + if err := cat.readFile.StartWithProcessorOptimized( + context.Background(), + lcontext.LContext{}, + processor, + re, + ); err != nil { + t.Fatalf("validated optimized reader start failed: %v", err) + } + + want := []string{"alpha\n", "beta\n"} + if !reflect.DeepEqual(processor.lines, want) { + t.Fatalf("unexpected processed lines: got=%v want=%v", processor.lines, want) + } +} + +func TestValidatedReadTargetOpenRejectsEscapingSymlinkSwap(t *testing.T) { + resetCommonLogger(t) + + baseDir := t.TempDir() + rootDir := filepath.Join(baseDir, "root") + outsideDir := filepath.Join(baseDir, "outside") + if err := os.MkdirAll(rootDir, 0755); err != nil { + t.Fatalf("mkdir root dir: %v", err) + } + if err := os.MkdirAll(outsideDir, 0755); err != nil { + t.Fatalf("mkdir outside dir: %v", err) + } + + filePath := filepath.Join(rootDir, "app.log") + if err := os.WriteFile(filePath, []byte("alpha\n"), 0600); err != nil { + t.Fatalf("write app log: %v", err) + } + escapePath := filepath.Join(outsideDir, "secret.log") + if err := os.WriteFile(escapePath, []byte("secret\n"), 0600); err != nil { + t.Fatalf("write secret log: %v", err) + } + + target := mustValidatedReadTarget(t, filePath) + + if err := os.Remove(filePath); err != nil { + t.Fatalf("remove app log: %v", err) + } + relativeEscape, err := filepath.Rel(rootDir, escapePath) + if err != nil { + t.Fatalf("relative escape path: %v", err) + } + if err := os.Symlink(relativeEscape, filePath); err != nil { + t.Fatalf("symlink escape path: %v", err) + } + + if _, err := target.Open(); err == nil { + t.Fatal("expected rooted open to reject escaping symlink swap") + } +} + +func TestValidatedReadTargetOpenRejectsSameRootSymlinkSwap(t *testing.T) { + resetCommonLogger(t) + + baseDir := t.TempDir() + filePath := filepath.Join(baseDir, "app.log") + if err := os.WriteFile(filePath, []byte("alpha\n"), 0600); err != nil { + t.Fatalf("write app log: %v", err) + } + otherPath := filepath.Join(baseDir, "other.log") + if err := os.WriteFile(otherPath, []byte("other\n"), 0600); err != nil { + t.Fatalf("write other log: %v", err) + } + + target := mustValidatedReadTarget(t, filePath) + + if err := os.Remove(filePath); err != nil { + t.Fatalf("remove app log: %v", err) + } + if err := os.Symlink(filepath.Base(otherPath), filePath); err != nil { + t.Fatalf("symlink other log: %v", err) + } + + _, err := target.Open() + if err == nil { + t.Fatal("expected rooted open to reject same-root symlink swap") + } + if !strings.Contains(err.Error(), "symlink") { + t.Fatalf("expected symlink rejection, got %v", err) + } +} + +func TestNewValidatedJournalTargetRejectsAmbiguousUnits(t *testing.T) { + tests := []struct { + name string + spec string + }{ + { + name: "empty unit", + spec: "journal:", + }, + { + name: "leading dash", + spec: "journal:--output=json", + }, + { + name: "space", + spec: "journal:ssh.service --output=json", + }, + { + name: "newline", + spec: "journal:ssh.service\n--output=json", + }, + { + name: "tab", + spec: "journal:ssh.service\t--output=json", + }, + { + name: "control character", + spec: "journal:ssh.service\x00", + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if _, err := NewValidatedJournalTarget(tt.spec); err == nil { + t.Fatalf("NewValidatedJournalTarget(%q) succeeded, want error", tt.spec) + } + }) + } +} + +func TestNewValidatedJournalTargetAcceptsLiteralShellMetacharacters(t *testing.T) { + spec := "journal:ssh.service;touch$HOME`whoami`|cat" + + target, err := NewValidatedJournalTarget(spec) + if err != nil { + t.Fatalf("NewValidatedJournalTarget(%q) error = %v", spec, err) + } + if target.Kind != JournalKind { + t.Fatalf("target.Kind = %v, want %v", target.Kind, JournalKind) + } +} + +func TestValidatedTailFileTruncatedReopenDetectsTruncation(t *testing.T) { + resetCommonLogger(t) + + filePath := writeProcessorTestFile(t, "alpha\nbeta\n") + target := mustValidatedReadTarget(t, filePath) + + tail := NewValidatedTailFile(filePath, target, "glob-id", make(chan string, 1), defaultMaxLineLength) + fd, err := target.Open() + if err != nil { + t.Fatalf("open validated target: %v", err) + } + defer fd.Close() + + if _, err := fd.Seek(0, io.SeekEnd); err != nil { + t.Fatalf("seek end: %v", err) + } + if err := os.Truncate(filePath, 1); err != nil { + t.Fatalf("truncate file: %v", err) + } + + isTruncated, err := tail.readFile.truncated(fd) + if !isTruncated { + t.Fatal("expected truncation to be detected") + } + if err == nil || !strings.Contains(err.Error(), "truncated") { + t.Fatalf("expected truncation error, got %v", err) + } +} + +func mustValidatedReadTarget(t *testing.T, path string) ValidatedReadTarget { + t.Helper() + + absolutePath, err := filepath.Abs(path) + if err != nil { + t.Fatalf("abs path: %v", err) + } + + target, err := NewValidatedReadTarget(absolutePath) + if err != nil { + t.Fatalf("create validated target: %v", err) + } + + return target +} + +func resetCommonLogger(t *testing.T) { + t.Helper() + + originalLogger := dlog.Common + dlog.Common = &dlog.DLog{} + t.Cleanup(func() { + dlog.Common = originalLogger + }) +} |
