Mask the target URL in delivery errors, SSRF logs and log page data (closes #118)
All checks were successful
check / check (push) Successful in 3m30s
All checks were successful
check / check (push) Successful in 3m30s
A delivery target URL is itself a credential: a Slack incoming webhook URL is a bearer token. Three paths still reproduced it in full. Transport failures were the worst of them. net/http embeds the request URL in every *url.Error it returns, so any DNS, TLS, timeout or dial failure wrote the whole webhook URL into DeliveryResult.Error — on disk, in the per-webhook database, behind a json tag that a REST API would serialize. maskURL moves to url_mask.go and is exported as MaskURL, and maskURLError joins it: it rebuilds the *url.Error with the URL masked, keeping the operation and the wrapped cause, so a refused connection still reads differently from a DNS failure or a timeout and errors.Is/As/Timeout still work. It is applied where the errors are raised — executeHTTPRequest, shared by the Slack and HTTP targets, and the request-construction paths — so downstream wrapping is safe by construction. url.Parse embeds the URL too, so ValidateTargetURL's parse branch gets the same treatment; its error is logged and shown. The SSRF rejection log now records only the masked URL, and loadTargetMap hands the event log page TargetViews and a delivery projection instead of raw target rows, so the stored config blob has no path to that template either.
This commit is contained in:
@@ -92,7 +92,12 @@ func ValidateTargetURL(
|
||||
) error {
|
||||
parsed, err := url.Parse(targetURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid URL: %w", err)
|
||||
// url.Parse embeds the whole URL in its error, and
|
||||
// this one is logged and shown; mask it. Every other
|
||||
// branch below reports only the hostname.
|
||||
return fmt.Errorf(
|
||||
"invalid URL: %w", maskURLError(err),
|
||||
)
|
||||
}
|
||||
|
||||
err = validateScheme(parsed.Scheme)
|
||||
|
||||
@@ -3,7 +3,6 @@ package delivery
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
"sneak.berlin/go/webhooker/internal/database"
|
||||
@@ -17,9 +16,6 @@ import (
|
||||
// browser history, screenshots and screen shares.
|
||||
const configUnavailable = "(unavailable)"
|
||||
|
||||
// urlPathElision stands in for a URL's elided path.
|
||||
const urlPathElision = "/..."
|
||||
|
||||
// ConfigField is one labelled, display-safe value derived
|
||||
// from a target's stored configuration.
|
||||
type ConfigField struct {
|
||||
@@ -202,23 +198,5 @@ func databaseConfigFields(configJSON string) []ConfigField {
|
||||
// parse into a scheme and host yields the neutral
|
||||
// placeholder, never the raw string.
|
||||
func (c *SlackTargetConfig) MaskedWebhookURL() string {
|
||||
return maskURL(c.WebhookURL)
|
||||
}
|
||||
|
||||
// maskURL renders a URL as scheme plus host with everything
|
||||
// that can carry a secret removed.
|
||||
func maskURL(raw string) string {
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Scheme == "" ||
|
||||
parsed.Host == "" {
|
||||
return configUnavailable
|
||||
}
|
||||
|
||||
masked := parsed.Scheme + "://" + parsed.Host
|
||||
|
||||
if parsed.Path != "" && parsed.Path != "/" {
|
||||
masked += urlPathElision
|
||||
}
|
||||
|
||||
return masked
|
||||
return MaskURL(c.WebhookURL)
|
||||
}
|
||||
|
||||
@@ -363,7 +363,8 @@ func (t *httpTarget) doHTTPRequest(
|
||||
)
|
||||
if reqErr != nil {
|
||||
return 0, "", 0, fmt.Errorf(
|
||||
"creating request: %w", reqErr,
|
||||
"creating request: %w",
|
||||
maskURLError(reqErr),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -492,8 +493,19 @@ func applyRequestHeaders(
|
||||
// executeHTTPRequest sends an HTTP request using the provided
|
||||
// client. URLs are validated by the config parsers and the
|
||||
// SSRF-safe transport before reaching here.
|
||||
//
|
||||
// Transport failures are masked here, at the single point
|
||||
// where every target's request errors are born, because the
|
||||
// caller stores them in DeliveryResult.Error: an unmasked
|
||||
// *url.Error would write the target URL — the credential for
|
||||
// a Slack incoming webhook — into the per-webhook database.
|
||||
func executeHTTPRequest(
|
||||
client *http.Client, req *http.Request,
|
||||
) (*http.Response, error) {
|
||||
return client.Do(req) //#nosec G704 -- validated URL, SSRF-safe transport
|
||||
resp, err := client.Do(req) //#nosec G704 -- validated URL, SSRF-safe transport
|
||||
if err != nil {
|
||||
return nil, maskURLError(err)
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
@@ -125,7 +125,7 @@ func (t *slackTarget) attempt(
|
||||
if err != nil {
|
||||
return attemptResult{
|
||||
success: false,
|
||||
errMsg: err.Error(),
|
||||
errMsg: maskURLError(err).Error(),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
61
internal/delivery/url_mask.go
Normal file
61
internal/delivery/url_mask.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package delivery
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
// urlPathElision stands in for a URL's elided path.
|
||||
const urlPathElision = "/..."
|
||||
|
||||
// MaskURL renders a URL as scheme plus host with everything
|
||||
// that can carry a secret removed. A delivery target URL is
|
||||
// itself a credential — a Slack incoming webhook URL is a
|
||||
// bearer token — so the path, query and userinfo are never
|
||||
// reproduced, in a page, a log line or a stored error. A URL
|
||||
// that does not parse into a scheme and host yields the
|
||||
// neutral placeholder, never the raw string.
|
||||
func MaskURL(raw string) string {
|
||||
parsed, err := url.Parse(raw)
|
||||
if err != nil || parsed.Scheme == "" ||
|
||||
parsed.Host == "" {
|
||||
return configUnavailable
|
||||
}
|
||||
|
||||
masked := parsed.Scheme + "://" + parsed.Host
|
||||
|
||||
if parsed.Path != "" && parsed.Path != "/" {
|
||||
masked += urlPathElision
|
||||
}
|
||||
|
||||
return masked
|
||||
}
|
||||
|
||||
// maskURLError strips the credential from an error raised
|
||||
// against a request URL. The net/http and net/url packages
|
||||
// embed the full request URL in every *url.Error they return,
|
||||
// so an unmodified transport error persisted into
|
||||
// DeliveryResult.Error writes the credential to disk.
|
||||
//
|
||||
// The masked error keeps the operation and the wrapped cause,
|
||||
// so a DNS failure still reads differently from a refused
|
||||
// connection, a TLS handshake failure or a timeout, and Is,
|
||||
// As, Timeout and Temporary keep working on it. Only the
|
||||
// path, query and userinfo of the URL are dropped. Errors
|
||||
// that carry no URL are returned unchanged.
|
||||
//
|
||||
// Call it where the error is raised, before any wrapping: it
|
||||
// replaces the *url.Error itself, so any context wrapped
|
||||
// around it first would be discarded.
|
||||
func maskURLError(err error) error {
|
||||
var urlErr *url.Error
|
||||
if !errors.As(err, &urlErr) {
|
||||
return err
|
||||
}
|
||||
|
||||
return &url.Error{
|
||||
Op: urlErr.Op,
|
||||
URL: MaskURL(urlErr.URL),
|
||||
Err: urlErr.Err,
|
||||
}
|
||||
}
|
||||
196
internal/delivery/url_mask_test.go
Normal file
196
internal/delivery/url_mask_test.go
Normal file
@@ -0,0 +1,196 @@
|
||||
package delivery_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"gorm.io/gorm"
|
||||
"sneak.berlin/go/webhooker/internal/database"
|
||||
"sneak.berlin/go/webhooker/internal/delivery"
|
||||
)
|
||||
|
||||
// The path of a Slack incoming webhook URL is the credential:
|
||||
// whoever holds these segments can post to the channel
|
||||
// forever. None of them may reach a stored delivery error,
|
||||
// which lives on disk in the per-webhook database and is
|
||||
// serialized by the JSON tag on DeliveryResult.Error.
|
||||
const (
|
||||
maskSecretPath = "/services/T00000000/B00000000/" +
|
||||
"XXXXXXXXXXXXXXXXXXXXXXXX"
|
||||
)
|
||||
|
||||
// assertNoCredential fails if the whole path or any single
|
||||
// segment of it survived into the message, so a partial leak
|
||||
// fails the test too.
|
||||
func assertNoCredential(t *testing.T, msg string) {
|
||||
t.Helper()
|
||||
|
||||
segments := []string{
|
||||
maskSecretPath,
|
||||
"services",
|
||||
"T00000000",
|
||||
"B00000000",
|
||||
"XXXXXXXXXXXXXXXXXXXXXXXX",
|
||||
}
|
||||
|
||||
for _, segment := range segments {
|
||||
assert.NotContains(t, msg, segment)
|
||||
}
|
||||
}
|
||||
|
||||
// storedDeliveryError returns the error string persisted for a
|
||||
// delivery, which is what an operator and any future API read.
|
||||
func storedDeliveryError(
|
||||
t *testing.T, db *gorm.DB, deliveryID string,
|
||||
) string {
|
||||
t.Helper()
|
||||
|
||||
var result database.DeliveryResult
|
||||
|
||||
require.NoError(t, db.Where(
|
||||
"delivery_id = ?", deliveryID,
|
||||
).First(&result).Error)
|
||||
|
||||
return result.Error
|
||||
}
|
||||
|
||||
// deliverSlackTo runs a Slack delivery against webhookURL and
|
||||
// returns the error string it persisted.
|
||||
func deliverSlackTo(
|
||||
t *testing.T, webhookURL string,
|
||||
) string {
|
||||
t.Helper()
|
||||
|
||||
db := testWebhookDB(t)
|
||||
e := testEngine(t, 1)
|
||||
targetID := uuid.New().String()
|
||||
|
||||
slackCfg, err := json.Marshal(
|
||||
delivery.SlackTargetConfig{
|
||||
WebhookURL: webhookURL,
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
event := seedEvent(t, db, `{"test":true}`)
|
||||
|
||||
dlv := seedDelivery(
|
||||
t, db, event.ID, targetID,
|
||||
database.DeliveryStatusPending,
|
||||
)
|
||||
|
||||
d := buildSlackDelivery(
|
||||
dlv, event, targetID,
|
||||
"test-slack-mask", string(slackCfg),
|
||||
)
|
||||
|
||||
e.ExportDeliverSlack(context.TODO(), db, d)
|
||||
|
||||
assertDeliveryStatus(t, db, dlv.ID,
|
||||
database.DeliveryStatusFailed,
|
||||
)
|
||||
|
||||
return storedDeliveryError(t, db, dlv.ID)
|
||||
}
|
||||
|
||||
// TestDeliverSlack_TransportErrorMasksWebhookURL is the
|
||||
// load-bearing regression test: a transport failure must not
|
||||
// persist the webhook URL's credential into the database, and
|
||||
// must still say what went wrong and where.
|
||||
func TestDeliverSlack_TransportErrorMasksWebhookURL(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
// A server closed before use gives a deterministic
|
||||
// transport failure against a known host.
|
||||
ts := httptest.NewServer(http.NewServeMux())
|
||||
host := ts.URL
|
||||
|
||||
ts.Close()
|
||||
|
||||
errMsg := deliverSlackTo(t, host+maskSecretPath)
|
||||
|
||||
require.NotEmpty(t, errMsg)
|
||||
assertNoCredential(t, errMsg)
|
||||
|
||||
// The diagnostic value survives: the operation, the host
|
||||
// and the transport failure are all still reported, and
|
||||
// only the path is elided.
|
||||
assert.Contains(t, errMsg, "sending request")
|
||||
assert.Contains(t, errMsg, "Post")
|
||||
assert.Contains(t, errMsg, host+"/...")
|
||||
assert.Contains(t, errMsg, "connection refused")
|
||||
}
|
||||
|
||||
// TestDeliverSlack_UnparsableURLMasksWebhookURL covers the
|
||||
// other error path out of a Slack attempt: url.Parse also
|
||||
// embeds the whole URL in the error it returns.
|
||||
func TestDeliverSlack_UnparsableURLMasksWebhookURL(
|
||||
t *testing.T,
|
||||
) {
|
||||
t.Parallel()
|
||||
|
||||
errMsg := deliverSlackTo(
|
||||
t,
|
||||
"https://hooks.slack.com"+maskSecretPath+"\n",
|
||||
)
|
||||
|
||||
require.NotEmpty(t, errMsg)
|
||||
assertNoCredential(t, errMsg)
|
||||
assert.Contains(t, errMsg, "invalid control character")
|
||||
}
|
||||
|
||||
// TestDoHTTPRequest_TransportErrorMasksURL proves the HTTP
|
||||
// target's transport errors are masked too; its destination
|
||||
// URL can carry a token in a query string.
|
||||
func TestDoHTTPRequest_TransportErrorMasksURL(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
ts := httptest.NewServer(http.NewServeMux())
|
||||
host := ts.URL
|
||||
|
||||
ts.Close()
|
||||
|
||||
e := testEngine(t, 1)
|
||||
|
||||
cfg, err := e.ExportParseHTTPConfig(
|
||||
newHTTPTargetConfig(host + maskSecretPath),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
|
||||
statusCode, _, _, reqErr := e.ExportDoHTTPRequest(
|
||||
context.TODO(), cfg,
|
||||
&database.Event{Body: `{"test":true}`},
|
||||
)
|
||||
require.Error(t, reqErr)
|
||||
assert.Zero(t, statusCode)
|
||||
|
||||
assertNoCredential(t, reqErr.Error())
|
||||
assert.Contains(t, reqErr.Error(), host+"/...")
|
||||
assert.Contains(
|
||||
t, reqErr.Error(), "connection refused",
|
||||
)
|
||||
}
|
||||
|
||||
// TestValidateTargetURL_UnparsableURLIsMasked proves the SSRF
|
||||
// validator's error does not carry the submitted URL, which
|
||||
// the handler both logs and shows.
|
||||
func TestValidateTargetURL_UnparsableURLIsMasked(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
err := delivery.ValidateTargetURL(
|
||||
context.TODO(),
|
||||
"https://hooks.slack.com"+maskSecretPath+"\n",
|
||||
)
|
||||
require.Error(t, err)
|
||||
|
||||
assertNoCredential(t, err.Error())
|
||||
assert.Contains(t, err.Error(), "invalid URL")
|
||||
}
|
||||
Reference in New Issue
Block a user