fix: check os.ReadFile error before using file contents

http.DetectContentType was called with potentially nil/empty contents
when ReadFile failed; the error was only checked afterward. Move the
error check immediately after ReadFile so bad data is never used.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
Manuel Rüger 2026-03-13 01:13:36 +01:00
parent 8205794e7b
commit 9e7f2cf9d5

View File

@ -102,6 +102,9 @@ func resolveLink(
}
linkContents, err := os.ReadFile(filepath)
if err != nil {
return "", karma.Format(err, "read file: %s", filepath)
}
contentType := http.DetectContentType(linkContents)
// Check if the MIME type starts with "text/"
@ -110,10 +113,6 @@ func resolveLink(
return "", nil
}
if err != nil {
return "", karma.Format(err, "read file: %s", filepath)
}
linkContents = bytes.ReplaceAll(
linkContents,
[]byte("\r\n"),