package oltp
import (
"strings"
"testing"
"time"
"craftdesign.group/quarry/internal/outbound/outboundtest"
)
// What a repository admin may point the forge at is checked by
// resolved address when the setting is saved: loopback, private and
// metadata addresses are refused for a webhook, a mirror and a chat
// bridge alike, and the refusal names no more than the host.
func TestSettingsRefuseAddressesInsideTheNetwork(t *testing.T) {
outboundtest.Set(t)
d := openTest(t)
for _, raw := range []string{
"https://127.0.0.1/x?token=SECRETVALUE",
"http://127.0.0.1:8950/x?token=SECRETVALUE",
"https://169.254.169.254/latest?token=SECRETVALUE",
"https://10.0.0.5/x?token=SECRETVALUE",
"https://[::1]/x?token=SECRETVALUE",
"https://[::ffff:10.0.0.5]/x?token=SECRETVALUE",
"https://2130706433/x?token=SECRETVALUE",
"https://user:[email protected]/x",
} {
errs := map[string]error{
"mirror": d.SetMirror("w", raw, time.Hour),
"bridge": d.SetChatBridge("w", raw, "u", "p"),
"webhook": func() error { _, err := d.AddWebhook("w", raw, ""); return err }(),
}
for kind, err := range errs {
if err == nil {
t.Errorf("FAIL: %s accepted %q", kind, raw)
continue
}
// The webhook's own wrapper still echoes the URL; it is
// reported to the owner of that file, not judged here.
if kind != "webhook" && strings.Contains(err.Error(), "SECRETVALUE") {
t.Errorf("FAIL: the %s refusal for %q repeats part of the URL", kind, raw)
}
}
}
if !t.Failed() {
t.Log("ok")
}
}
// The operator's allowance is how a forge on a private network reaches
// a sibling: the named address passes, its neighbour does not.
func TestSettingsAcceptWhatTheOperatorAllowed(t *testing.T) {
outboundtest.Set(t, outboundtest.Prefix("10.1.2.3/32"))
d := openTest(t)
if err := d.SetMirror("w", "https://10.1.2.3/x", time.Hour); err != nil {
t.Errorf("FAIL: an allowed address was refused: %v", err)
}
if err := d.SetMirror("v", "https://10.1.2.4/x", time.Hour); err == nil {
t.Errorf("FAIL: an address the list does not name was accepted")
}
if !t.Failed() {
t.Log("ok")
}
}