Fix linter errors

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2023-10-04 22:43:21 +02:00
parent 4aa5f14b3f
commit a585adcd13
No known key found for this signature in database
GPG Key ID: D91C3E91E4CAD6F5
6 changed files with 23 additions and 14 deletions

11
api.go
View File

@ -22,7 +22,7 @@ type apiResponse struct {
Error string `json:"error,omitempty"`
ExpiresAt *time.Time `json:"expires_at,omitempty"`
Secret string `json:"secret,omitempty"`
SecretId string `json:"secret_id,omitempty"`
SecretID string `json:"secret_id,omitempty"`
}
type apiRequest struct {
@ -83,7 +83,7 @@ func (a apiServer) handleCreate(res http.ResponseWriter, r *http.Request) {
a.jsonResponse(res, http.StatusCreated, apiResponse{
ExpiresAt: expiresAt,
Success: true,
SecretId: id,
SecretID: id,
})
}
@ -124,10 +124,13 @@ func (a apiServer) errorResponse(res http.ResponseWriter, status int, err error,
})
}
func (a apiServer) jsonResponse(res http.ResponseWriter, status int, response apiResponse) {
func (apiServer) jsonResponse(res http.ResponseWriter, status int, response apiResponse) {
res.Header().Set("Content-Type", "application/json")
res.Header().Set("Cache-Control", "no-store, max-age=0")
res.WriteHeader(status)
json.NewEncoder(res).Encode(response)
if err := json.NewEncoder(res).Encode(response); err != nil {
logrus.WithError(err).Error("encoding JSON response")
http.Error(res, `{"error":"could not encode response"}`, http.StatusInternalServerError)
}
}

View File

@ -38,7 +38,7 @@ func loadCustomize(filename string) (cust customize, err error) {
return cust, nil
}
cf, err := os.Open(filename)
cf, err := os.Open(filename) //#nosec:G304 // Loading a custom file is the intention here
if err != nil {
if errors.Is(err, fs.ErrNotExist) {
logrus.Warn("customize file given but not found")
@ -46,7 +46,11 @@ func loadCustomize(filename string) (cust customize, err error) {
}
return cust, errors.Wrap(err, "opening customize file")
}
defer cf.Close()
defer func() {
if err := cf.Close(); err != nil {
logrus.WithError(err).Error("closing customize file (leaked fd)")
}
}()
if err = yaml.NewDecoder(cf).Decode(&cust); err != nil {
return cust, errors.Wrap(err, "decoding customize file")

View File

@ -155,10 +155,12 @@ func assetDelivery(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", mime.TypeByExtension(ext))
w.Header().Set("X-Content-Type-Options", "nosniff")
w.Write(assetData)
if _, err = w.Write(assetData); err != nil {
logrus.WithError(err).Error("writing asset data")
}
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
func handleIndex(w http.ResponseWriter, _ *http.Request) {
inlineContentNonce := make([]byte, scriptNonceSize)
if _, err := rand.Read(inlineContentNonce); err != nil {
logrus.WithError(err).Error("generating script nonce")

View File

@ -6,7 +6,7 @@ import (
"time"
)
var errSecretNotFound = errors.New("Secret not found")
var errSecretNotFound = errors.New("secret not found")
type storage interface {
Create(secret string, expireIn time.Duration) (string, error)
@ -20,6 +20,6 @@ func getStorageByType(t string) (storage, error) {
case "redis":
return newStorageRedis()
default:
return nil, fmt.Errorf("Storage type %q not found", t)
return nil, fmt.Errorf("storage type %q not found", t)
}
}

View File

@ -53,14 +53,14 @@ func (s storageRedis) ReadAndDestroy(id string) (string, error) {
if errors.Is(err, redis.Nil) {
return "", errSecretNotFound
}
return "", err
return "", errors.Wrap(err, "getting key")
}
err = s.conn.Del(context.Background(), s.redisKey(id)).Err()
return string(secret), errors.Wrap(err, "deleting key")
return secret, errors.Wrap(err, "deleting key")
}
func (s storageRedis) redisKey(id string) string {
func (storageRedis) redisKey(id string) string {
prefix := redisDefaultPrefix
if prfx := os.Getenv("REDIS_KEY"); prfx != "" {
prefix = prfx

View File

@ -30,7 +30,7 @@ func assetSRIHash(assetName string) string {
}
h := sha512.New384()
h.Write(data)
_, _ = h.Write(data)
sum := h.Sum(nil)
sri := "sha384-" + base64.StdEncoding.EncodeToString(sum)