Skip to content

Commit

Permalink
Check plugin binary response valid binary
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Sep 9, 2024
1 parent 916397e commit 956fc79
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pkg/config/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,15 @@ func LoadPluginPath() string {
}

func checkBinary(p string) (string, error) {
if _, err := os.Stat(p); err != nil {
return "", fmt.Errorf("failed to stat file: %w", err)
f, err := os.ReadFile(p)
if err != nil {
return "", fmt.Errorf("failed to open file: %w", err)
}

// check binary type
mineType := http.DetectContentType(f)
if !strings.EqualFold(mineType, "application/octet-stream") {
return "", fmt.Errorf("invalid file type (correct: application/octet-stream got: %s)", mineType)
}

// need permission of execute
Expand Down Expand Up @@ -272,11 +279,12 @@ func fetchHTTP(u *url.URL) (string, error) {
}
defer resp.Body.Close()

if resp.StatusCode == http.StatusOK {
_, err := io.Copy(f, resp.Body)
if err != nil {
return "", fmt.Errorf("failed to write file (path: %s): %w", fp, err)
}
if resp.StatusCode != http.StatusOK {
return "", fmt.Errorf("failed to get config via HTTP(S): status code is not 200 (status code: %d)", resp.StatusCode)
}

if _, err := io.Copy(f, resp.Body); err != nil {
return "", fmt.Errorf("failed to write file (path: %s): %w", fp, err)
}

return fp, nil
Expand Down

0 comments on commit 956fc79

Please sign in to comment.