Fixes
This commit is contained in:
1 parent
04794aea54
commit
cfd31f398c
61 files changed
+2062
-254
No files matched your search
@@ -0,0 +1,89 @@
|
||||
package feeds
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/descrybe/descrybe-v2/apps/api/internal/platformsettings"
|
||||
)
|
||||
|
||||
// Custom remote source headers are stored only as encrypted options; never in response DTOs.
|
||||
func validatedFeedHeaders(input map[string]string) (map[string]string, error) {
|
||||
if len(input) > 20 {
|
||||
return nil, ClientMsg("at most 20 HTTP headers")
|
||||
}
|
||||
clean := make(map[string]string, len(input))
|
||||
for name, value := range input {
|
||||
name = http.CanonicalHeaderKey(strings.TrimSpace(name))
|
||||
value = strings.TrimSpace(value)
|
||||
if !httpValidHeaderName(name) || strings.ContainsAny(value, "\r\n") || len(value) > 4096 {
|
||||
return nil, ClientMsg("invalid feed HTTP header")
|
||||
}
|
||||
switch strings.ToLower(name) {
|
||||
case "host", "connection", "content-length", "transfer-encoding", "cookie", "proxy-authorization", "proxy-connection", "upgrade":
|
||||
return nil, ClientMsg("unsafe feed HTTP header")
|
||||
}
|
||||
if value != "" {
|
||||
clean[name] = value
|
||||
}
|
||||
}
|
||||
return clean, nil
|
||||
}
|
||||
func httpValidHeaderName(name string) bool {
|
||||
if name == "" || len(name) > 100 {
|
||||
return false
|
||||
}
|
||||
for _, r := range name {
|
||||
if !(r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || r >= '0' && r <= '9' || strings.ContainsRune("!#$%&'*+-.^_`|~", r)) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
func secretEncryptionKey() []byte {
|
||||
material := os.Getenv("APP_ENCRYPTION_KEY")
|
||||
if material == "" {
|
||||
material = os.Getenv("CREDENTIALS_ENCRYPTION_KEY")
|
||||
}
|
||||
return platformsettings.DeriveKey(material, os.Getenv("TOKEN_SIGNING_SECRET"))
|
||||
}
|
||||
func encryptFeedHeaders(headers map[string]string) (string, error) {
|
||||
clean, err := validatedFeedHeaders(headers)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if len(clean) == 0 {
|
||||
return "", nil
|
||||
}
|
||||
key := secretEncryptionKey()
|
||||
if len(key) != 32 {
|
||||
return "", errors.New("APP_ENCRYPTION_KEY is required for authenticated feed URLs")
|
||||
}
|
||||
b, err := json.Marshal(clean)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return platformsettings.EncryptSecret(key, string(b))
|
||||
}
|
||||
func decryptFeedHeaders(options any) (map[string]string, error) {
|
||||
opt, ok := options.(map[string]any)
|
||||
if !ok {
|
||||
return nil, nil
|
||||
}
|
||||
encrypted, _ := opt["request_headers_encrypted"].(string)
|
||||
if encrypted == "" {
|
||||
return nil, nil
|
||||
}
|
||||
plain, err := platformsettings.DecryptSecret(secretEncryptionKey(), encrypted)
|
||||
if err != nil {
|
||||
return nil, errors.New("cannot decrypt feed credentials; ensure same APP_ENCRYPTION_KEY on API and worker")
|
||||
}
|
||||
out := map[string]string{}
|
||||
if err := json.Unmarshal([]byte(plain), &out); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return validatedFeedHeaders(out)
|
||||
}
|
||||
Reference in new issue
Block a user