8000 fix: change text detect to check first and last 512 bytes (#2310) · zarf-dev/zarf@cf1d1e4 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf1d1e4

Browse files
WeaponX314Racer159
andauthored
fix: change text detect to check first and last 512 bytes (#2310)
## Description Alters text detection logic to read the first and last 512 bytes. Tested with 5 files: - [NVIDIA installer](https://us.download.nvidia.com/XFree86/Linux-x86_64/535.154.05/NVIDIA-Linux-x86_64-535.154.05.run) Detected as application type when reading last 512. - 3 4k size files of junk text with a ZARF_CONST replacement, in straight text, yaml, and json All 3 detected as text/plain, ZARF_CONST was replaced. - 1 small 100 byte file with a ZARF_CONST replacement. Was still detected as text and ZARF_CONST was replaced. Existing unit tests succeeded. ## Related Issue Fixes #2308 <!-- or --> Relates to # ## Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Other (security config, docs update, etc) ## Checklist before merging - [x] Test, docs, adr added or updated as needed - [x] [Contributor Guide Steps](https://github.com/defenseunicorns/zarf/blob/main/CONTRIBUTING.md#developer-workflow) followed --------- Co-authored-by: Wayne Starr <Racer159@users.noreply.github.com>
1 parent 07541a6 commit cf1d1e4

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/pkg/utils/io.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,22 +475,41 @@ func IsTextFile(path string) (bool, error) {
475475
}
476476
defer f.Close() // Make sure to close the file when we're done
477477

478-
// Read the first 512 bytes of the file
479-
data := make([]byte, 512)
480-
n, err := f.Read(data)
481-
if err != nil && err != io.EOF {
478+
// Get file stat
479+
stat, err := f.Stat()
480+
if err != nil {
482481
return false, err
483482
}
484483

485-
// Use http.DetectContentType to determine the MIME type of the file
486-
mimeType := http.DetectContentType(data[:n])
484+
// Clip offset to minimum of 0
485+
lastOffset := max(0, stat.Size()-512)
486+
487+
// Take two passes checking front and back of the file
488+
offsetPasses := []int64{0, lastOffset}
489+
isTextCheck := []bool{false, false}
490+
for idx, offset := range offsetPasses {
491+
// Create 512 byte buffer
492+
data := make([]byte, 512)
493+
494+
n, err := f.ReadAt(data, offset)
495+
if err != nil && err != io.EOF {
496+
return false, err
497+
}
487498

488-
// Check if the MIME type indicates that the file is text
489-
hasText := strings.HasPrefix(mimeType, "text/")
490-
hasJSON := strings.Contains(mimeType, "json")
491-
hasXML := strings.Contains(mimeType, "xml")
499+
// Use http.DetectContentType to determine the MIME type of the file
500+
mimeType := http.DetectContentType(data[:n])
501+
502+
// Check if the MIME type indicates that the file is text
503+
hasText := strings.HasPrefix(mimeType, "text/")
504+
hasJSON := strings.Contains(mimeType, "json")
505+
hasXML := strings.Contains(mimeType, "xml")
506+
507+
// Save result
508+
isTextCheck[idx] = hasText || hasJSON || hasXML
509+
}
492510

493-
return hasText || hasJSON || hasXML, nil
511+
// Returns true if both front and back show they are text
512+
return isTextCheck[0] && isTextCheck[1], nil
494513
}
495514

496515
// IsTrashBin checks if the given directory path corresponds to an operating system's trash bin.

0 commit comments

Comments
 (0)
0