-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding validation around binary types (#235)
* adding validation around binary types --------- Signed-off-by: Jordan Rash <[email protected]>
- Loading branch information
1 parent
3d24832
commit 467469a
Showing
3 changed files
with
74 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"slices" | ||
) | ||
|
||
func validateBinary(inFile string) (string, string, error) { | ||
f, err := os.Open(inFile) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
buf := make([]byte, 150) | ||
_, err = f.Read(buf) | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
err = f.Close() | ||
if err != nil { | ||
return "", "", err | ||
} | ||
|
||
switch { | ||
case slices.Equal(buf[:4], []byte{0x7f, 0x45, 0x4c, 0x46}): | ||
switch { | ||
case slices.Equal(buf[17:19], []byte{0x00, 0x3e}): | ||
return "linux", "amd64", nil | ||
case slices.Equal(buf[17:19], []byte{0x00, 0xb7}): | ||
return "linux", "arm64", nil | ||
default: | ||
return "", "", fmt.Errorf("unsupported elf file type") | ||
} | ||
case slices.Equal(buf[:2], []byte{0x4d, 0x5a}): | ||
switch { | ||
case slices.Equal(buf[132:134], []byte{0x64, 0x86}): | ||
return "windows", "amd64", nil | ||
case slices.Equal(buf[132:134], []byte{0x64, 0xaa}): | ||
return "windows", "arm64", nil | ||
default: | ||
return "", "", fmt.Errorf("unsupported windows file type") | ||
} | ||
case slices.Equal(buf[:4], []byte{0xcf, 0xfa, 0xed, 0xfe}): | ||
switch { | ||
case slices.Equal(buf[4:5], []byte{0x07}): | ||
return "darwin", "amd64", nil | ||
case slices.Equal(buf[4:5], []byte{0x0c}): | ||
return "darwin", "arm64", nil | ||
default: | ||
return "", "", fmt.Errorf("unsupported darwin file type") | ||
} | ||
default: | ||
return "", "", fmt.Errorf("unsupported binary file type") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters