Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix: capture output filenames collide within the same second, silently overwriting earlier images (PIN-050)

Luigi Agostid3aa825c1fb2
diff --git a/internal/handlers/binary_export.go b/internal/handlers/binary_export.go
index 4f6af958..79641c0d 100644
--- a/internal/handlers/binary_export.go
+++ b/internal/handlers/binary_export.go
@@ -52,6 +52,12 @@ const maxUniqueNameAttempts = 512
//
// The caller owns the base name, including its timestamp format, so this closes the
// collision without renaming anything a user might already be globbing for.
+//
+// SCOPE, recorded so it is not mistaken for complete: every auto-naming writer in this
+// package routes through here, but the five CLI sites in internal/cli/actions still build
+// a name and write it unconditionally. They are deferred to PIN-158 rather than given a
+// copy of this loop — a second owner of the rule is the drift worth avoiding, and sharing
+// it means moving this primitive down to a leaf package the CLI can import.
func createUniqueFile(dir, base, ext string) (*os.File, string, error) {
for attempt := 0; attempt < maxUniqueNameAttempts; attempt++ {
name := base + ext
diff --git a/internal/handlers/binary_export_test.go b/internal/handlers/binary_export_test.go
index 8e26e2a8..ef60a719 100644
--- a/internal/handlers/binary_export_test.go
+++ b/internal/handlers/binary_export_test.go
@@ -310,3 +310,50 @@ func TestARefusedRecordStopLeavesNoReservedFile(t *testing.T) {
t.Errorf("a refused stop left %s behind", e.Name())
}
}
+
+// The census the card asks a reviewer to run. Every auto-naming site in internal/handlers
+// must reach an exclusive create; a survivor is either a caller-supplied path or is named
+// here as deliberate, so a new one cannot land silently.
+func TestNoHandlerAutoNamesAFileItThenOverwrites(t *testing.T) {
+ entries, err := os.ReadDir(".")
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ // file -> why its timestamp is not an overwrite risk.
+ accounted := map[string]string{
+ "binary_export.go": "exportTimestamp only builds the base name; createUniqueFile reserves the path",
+ "record_handlers.go": "the recording base name is reserved by createUniqueFile before it is returned",
+ }
+
+ var scanned int
+ for _, entry := range entries {
+ name := entry.Name()
+ if entry.IsDir() || !strings.HasSuffix(name, ".go") || strings.HasSuffix(name, "_test.go") {
+ continue
+ }
+ body, err := os.ReadFile(name) // #nosec G304 -- files listed from this package's own directory.
+ if err != nil {
+ t.Fatal(err)
+ }
+ scanned++
+ if !strings.Contains(string(body), "150405") {
+ continue
+ }
+ if accounted[name] == "" {
+ t.Errorf("%s builds a name from a second-resolution timestamp and is not accounted for; route it through createUniqueFile or record why it cannot collide", name)
+ }
+ }
+ if scanned < 2 {
+ t.Fatalf("scanned %d handler files; this census would pass vacuously", scanned)
+ }
+ for name := range accounted {
+ body, err := os.ReadFile(name) // #nosec G304 -- fixed list in this package.
+ if err != nil {
+ t.Fatalf("%s is accounted for but unreadable: %v", name, err)
+ }
+ if !strings.Contains(string(body), "150405") {
+ t.Errorf("%s no longer builds a timestamped name; drop its entry from this census", name)
+ }
+ }
+}