mirror of
https://github.com/Dvorinka/Primora.git
synced 2026-10-28 15:51:23 +00:00
security: harden agent surface — vault_exec allow-list, SSRF, error hygiene
Stress-test remediation across MCP, CLI, and backend:
- primora_vault_exec rewritten as an allow-list (was deny-list):
colon-form single-token commands bypassed it, leaking every vault
secret; verbatim env-name injection allowed NODE_OPTIONS=--require
host RCE. Now fixed read-mostly command set, arg/path deny regex,
strict env-name grammar, loader-hook name blocklist. All prior
bypasses verified denied; allowed commands still inject.
- SSRF: webhook/job URL validation now blocks cloud metadata IPs
(169.254.169.254, fd00:ec2::254) inside the link-local allowance
and non-canonical IP encodings (decimal/hex bare hosts) smuggled
as "docker service names".
- Path params encodeURIComponent in all client consumers — encodeURI
left ?#& intact, smuggling query segments through {name} params.
- primora_upload_object strict-validates contentBase64 — Buffer.from
silently dropped invalid chars.
- MCP no-creds startup degrades instead of crashing mid-handshake.
- GET /api/v1/context — API-key self-discovery; fixes the CLI dead
end (login --api-key validates the key + seeds org/project,
use/context/whoami work under key auth).
- CLI: unknown commands / bare group exit 1 with hints; mri numeric
coercion reversed via raw-argv restore (007 stays "007");
login --json; orgs:rm / projects:rm / buckets:rm behind --yes;
fetch-failed errors name the cause.
- Error hygiene: pgx.ErrNoRows -> 404, pgconn codes -> clean
400/409 without SQLSTATE leaks, validator failures formatted,
services.InputError maps input faults to 400 (webhook URL/event
errors were 500s), presign on local driver -> 400.
- Webhook events accept wildcard patterns (*, ns.*, custom.*)
matched via SQL LIKE — client-published events can be webhooked.
Generated with [Devin](https://devin.ai)
Co-Authored-By: Devin <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
1 parent
3b90dae767
commit
2d6135121e
28 files changed
+705
-32
No files matched your search
@@ -14,6 +14,7 @@ import (
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -165,7 +166,7 @@ func normalizeWebhookURL(raw string) (string, error) {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
u, err := url.Parse(trimmed)
|
||||
if err != nil || u.Host == "" {
|
||||
return "", errors.New("webhook url must be an https URL")
|
||||
return "", inputErrorf("webhook url must be an https URL")
|
||||
}
|
||||
if u.Scheme == "https" {
|
||||
return trimmed, nil
|
||||
@@ -173,17 +174,35 @@ func normalizeWebhookURL(raw string) (string, error) {
|
||||
if u.Scheme == "http" && isPrivateWebhookHost(u.Hostname()) {
|
||||
return trimmed, nil
|
||||
}
|
||||
return "", errors.New("webhook url must be https (http allowed only for private/self-hosted targets)")
|
||||
return "", inputErrorf("webhook url must be https (http allowed only for private/self-hosted targets)")
|
||||
}
|
||||
|
||||
var decimalOrHexIPRe = regexp.MustCompile(`^(0x[0-9a-f]+|[0-9]+)$`)
|
||||
|
||||
// metadataAddrs are link-local addresses that serve cloud instance
|
||||
// metadata — "private" by range but a privileged SSRF target.
|
||||
var metadataAddrs = map[string]bool{
|
||||
"169.254.169.254": true, // AWS/GCP/Azure
|
||||
"fd00:ec2::254": true, // AWS IPv6 metadata
|
||||
}
|
||||
|
||||
func isPrivateWebhookHost(host string) bool {
|
||||
h := strings.ToLower(strings.Trim(host, "[]"))
|
||||
if metadataAddrs[h] {
|
||||
return false
|
||||
}
|
||||
if h == "localhost" || h == "host.docker.internal" ||
|
||||
strings.HasSuffix(h, ".local") || strings.HasSuffix(h, ".internal") || strings.HasSuffix(h, ".lan") {
|
||||
return true
|
||||
}
|
||||
if !strings.Contains(h, ".") && !strings.Contains(h, ":") {
|
||||
return true // bare hostname — docker service name, unqualified LAN host
|
||||
// Bare hostname — docker service name, unqualified LAN host. A
|
||||
// pure decimal/hex integer is never a service name: it's a
|
||||
// non-canonical IP encoding (2130706433 = 127.0.0.1, 0x7f000001).
|
||||
if decimalOrHexIPRe.MatchString(h) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
ip := net.ParseIP(h)
|
||||
if ip == nil {
|
||||
@@ -192,16 +211,40 @@ func isPrivateWebhookHost(host string) bool {
|
||||
return ip.IsLoopback() || ip.IsPrivate() || ip.IsLinkLocalUnicast()
|
||||
}
|
||||
|
||||
// webhookEventNamespaces are the valid `<ns>.*` subscription prefixes —
|
||||
// derived from the known system event types.
|
||||
func webhookEventNamespaces() map[string]bool {
|
||||
ns := map[string]bool{}
|
||||
for t := range webhookEventTypes {
|
||||
if i := strings.Index(t, "."); i > 0 {
|
||||
ns[t[:i]] = true
|
||||
}
|
||||
}
|
||||
return ns
|
||||
}
|
||||
|
||||
// validateWebhookEvents accepts known system types, `*`, `<ns>.*` namespace
|
||||
// patterns, and `custom.*` wildcard patterns — the same grammar functions
|
||||
// use for event_pattern. Matching happens via SQL LIKE at dispatch.
|
||||
func validateWebhookEvents(events []string) ([]string, error) {
|
||||
out := make([]string, 0, len(events))
|
||||
seen := map[string]bool{}
|
||||
namespaces := webhookEventNamespaces()
|
||||
for _, e := range events {
|
||||
e = strings.TrimSpace(e)
|
||||
if e == "" {
|
||||
continue
|
||||
}
|
||||
if !webhookEventTypes[e] {
|
||||
return nil, fmt.Errorf("unknown event type %q", e)
|
||||
valid := webhookEventTypes[e] || e == "*"
|
||||
if !valid && strings.HasSuffix(e, ".*") {
|
||||
valid = namespaces[strings.TrimSuffix(e, ".*")]
|
||||
}
|
||||
if !valid && strings.HasPrefix(e, "custom.") {
|
||||
// `*` isn't in the type grammar — probe it as a literal.
|
||||
valid = customEventTypeRe.MatchString(strings.ReplaceAll(e, "*", "x"))
|
||||
}
|
||||
if !valid {
|
||||
return nil, inputErrorf("invalid event type %q — use a known type, <namespace>.*, custom.<pattern>, or *", e)
|
||||
}
|
||||
if !seen[e] {
|
||||
seen[e] = true
|
||||
|
||||
Reference in new issue
Block a user