Skip to content

Commit

Permalink
Fix file parsing when file in zip archive is less than 2 bytes. Less …
Browse files Browse the repository at this point in the history
…verbose logging
  • Loading branch information
mapemapemape committed Nov 11, 2024
1 parent db18631 commit abf372e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
1 change: 0 additions & 1 deletion server/service/postArchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,6 @@ func (vars *apiMethodPostArchive) ServeHTTP(w http.ResponseWriter, req *http.Req
var size uint64 = 0

if filetype == "application/zip" {
log.Println("The archive is in Zip format")
match = regexp.MustCompile(`\.tgz$`)
newFile := match.ReplaceAll([]byte(archiveFile), []byte(".zip"))
os.Rename(archiveFile, string(newFile))
Expand Down
48 changes: 25 additions & 23 deletions server/service/processarchive.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ func processArchive(url string, db *sql.DB) (err error) {
log.Printf("Processing archive %s", url)

file := config.QueueDir + "/" + url
log.Println(file)

if !fileExists(file) {
log.Printf("File %s does not exist", file)
Expand Down Expand Up @@ -76,7 +75,6 @@ func processArchive(url string, db *sql.DB) (err error) {
log.Println(err)
}
for _, f := range files {
log.Println("Removing ", f)
err = os.Remove(f)
if err != nil {
log.Println(err)
Expand Down Expand Up @@ -174,13 +172,11 @@ func processArchive(url string, db *sql.DB) (err error) {

func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[string]int64, hostinfo int64, db *sql.DB, path string, tempdir string, de fs.DirEntry, err error) error {
if de.IsDir() {
log.Printf("Skipping directory %s", path)
return nil
}

// only process files under the "files" and "commands" directories
if !(strings.Contains(path, "/files/") || strings.Contains(path, "/commands/")) {
log.Printf("Skipping file %s", path)
return nil
}

Expand Down Expand Up @@ -235,7 +231,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s
}

if oldCrc.Valid && crc == oldCrc.Int32 {
log.Printf("Skipping file %s, no changes", fileName)
if hostinfo > 0 {
_, _ = db.Exec("UPDATE hostinfo SET lastseen = $1, clientversion = $2 "+
" WHERE certfp = $3 AND lastseen < $4", metadata["iso_received"], metadata["clientversion"],
Expand All @@ -262,7 +257,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s

// Set current to false for the previous version of this file
if _, ok := curfiles[fileName]; ok {
log.Printf("Setting current to false for %s", fileName)
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current",
curfiles[fileName])
delete(curfiles, fileName)
Expand All @@ -284,7 +278,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s

// clear the "current" flag for files that weren't in this package
var notCurrent []int64
log.Println("Clear the current flag for deleted files")
for _, fileId := range curfiles {
_, _ = db.Exec("UPDATE files SET current=false WHERE fileid = $1 AND current", fileId)
notCurrent = append(notCurrent, fileId)
Expand All @@ -294,7 +287,6 @@ func processFile(unchangedFiles *int, metadata map[string]string, curfiles map[s
// their "current" flag cleared, and can be removed from the
// in-memory search cache.
for _, id := range notCurrent {
log.Printf("id: %d", id)
removeFileFromFastSearch(id)
}

Expand Down Expand Up @@ -463,26 +455,36 @@ func unZip(dst string, fn string) error {
return err
}

var isUTF16 bytes.Buffer
_, err = io.CopyN(&isUTF16, fileInArchive, 2)
if err != nil {
return err
}
bom := isUTF16.Bytes()

pr := bytes.NewReader(bom)

if len(bom) == 2 && bom[0] == 0xFF && bom[1] == 0xFE { //UTF-16LE
scanner := bufio.NewScanner(transform.NewReader(io.MultiReader(pr, fileInArchive), unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
for scanner.Scan() {
dstFile.WriteString(scanner.Text() + "\r\n")
if f.UncompressedSize64 < 2 {
// file is too small to have a BOM, just copy
_, err = io.Copy(dstFile, fileInArchive)
if err != nil {
return err
}
dstFile.Close()
} else {
if _, err := io.Copy(dstFile, io.MultiReader(pr, fileInArchive)); err != nil {
// this file might be UTF-16, check for BOM
var isUTF16 bytes.Buffer
_, err = io.CopyN(&isUTF16, fileInArchive, 2)
if err != nil {
return err
}
bom := isUTF16.Bytes()

pr := bytes.NewReader(bom)

if len(bom) == 2 && bom[0] == 0xFF && bom[1] == 0xFE { //UTF-16LE
scanner := bufio.NewScanner(transform.NewReader(io.MultiReader(pr, fileInArchive), unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewDecoder()))
for scanner.Scan() {
dstFile.WriteString(scanner.Text() + "\r\n")
}
} else {
if _, err := io.Copy(dstFile, io.MultiReader(pr, fileInArchive)); err != nil {
return err
}
}
dstFile.Close()
}
dstFile.Close()
err = os.Chtimes(filePath, f.Modified, f.Modified)
if err != nil {
log.Printf("Error setting mod time on %s: %s", filePath, err)
Expand Down

0 comments on commit abf372e

Please sign in to comment.