Repository navigation
feat(webhooks): WEBHOOK_ALLOWED_CIDRS for operator-approved private receivers #278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
dominik-myszkowski
wants to merge
1
commit into
Calnode:main
Choose a base branch
from
dominik-myszkowski:feat/webhook-allowed-cidrs
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package netutil_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "io" | ||
| "log/slog" | ||
| "net" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/calnode/calnode/internal/netutil" | ||
| ) | ||
|
|
||
| func mustCIDRs(t *testing.T, ss ...string) []*net.IPNet { | ||
| t.Helper() | ||
| var out []*net.IPNet | ||
| for _, s := range ss { | ||
| _, n, err := net.ParseCIDR(s) | ||
| if err != nil { | ||
| t.Fatalf("parse %q: %v", s, err) | ||
| } | ||
| out = append(out, n) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func TestResolveSafeAllowing(t *testing.T) { | ||
| ctx := context.Background() | ||
| // Deliberately over-broad: an operator listing the metadata range, or everything, | ||
| // must still not reach the metadata service. | ||
| allowed := mustCIDRs(t, "10.200.50.0/24", "127.0.0.0/8", "169.254.0.0/16", "fe80::/10", "fd00::/8", "0.0.0.0/0") | ||
| cases := []struct { | ||
| host string | ||
| ok bool | ||
| }{ | ||
| {"10.200.50.7", true}, // the listed Docker network | ||
| {"127.0.0.1", true}, // listed loopback | ||
| {"10.0.0.1", true}, // covered by 0.0.0.0/0 | ||
| {"8.8.8.8", true}, // public, always allowed | ||
| {"169.254.169.254", false}, // metadata: listed, still refused | ||
| {"169.254.1.1", false}, // link-local | ||
| {"fe80::1", false}, // IPv6 link-local | ||
| {"fd00:ec2::254", false}, // AWS IPv6 metadata, inside listed fd00::/8 | ||
| {"0.0.0.0", false}, // unspecified | ||
| } | ||
| for _, tc := range cases { | ||
| _, err := netutil.ResolveSafeAllowing(ctx, tc.host, allowed) | ||
| if (err == nil) != tc.ok { | ||
| t.Errorf("ResolveSafeAllowing(%q) err = %v; want ok=%v", tc.host, err, tc.ok) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // With no list, the behaviour is exactly ResolveSafe's: every private range refused. | ||
| func TestResolveSafeAllowing_emptyIsResolveSafe(t *testing.T) { | ||
| ctx := context.Background() | ||
| for _, host := range []string{"10.200.50.7", "127.0.0.1", "192.168.1.1", "169.254.169.254", "fd00::1", "8.8.8.8"} { | ||
| _, errA := netutil.ResolveSafeAllowing(ctx, host, nil) | ||
| _, errB := netutil.ResolveSafe(ctx, host) | ||
| if (errA == nil) != (errB == nil) { | ||
| t.Errorf("%s: ResolveSafeAllowing(nil) err = %v, ResolveSafe err = %v; want the same verdict", host, errA, errB) | ||
| } | ||
| } | ||
| // A list that doesn't cover the address changes nothing for it. | ||
| if _, err := netutil.ResolveSafeAllowing(ctx, "10.0.0.1", mustCIDRs(t, "10.200.50.0/24")); err == nil { | ||
| t.Error("10.0.0.1 allowed by 10.200.50.0/24; want refused") | ||
| } | ||
| } | ||
|
|
||
| // The dial-time guard applies the same list: a listed private target is reached, an | ||
| // unlisted one is refused before any connection is made. | ||
| func TestSafeTransportAllowing(t *testing.T) { | ||
| srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { | ||
| io.WriteString(w, "ok") //nolint:errcheck | ||
| })) | ||
| defer srv.Close() | ||
| logger := slog.New(slog.NewTextHandler(io.Discard, nil)) | ||
|
|
||
| allowed := &http.Client{Transport: netutil.SafeTransportAllowing(logger, "test", mustCIDRs(t, "127.0.0.0/8"))} | ||
| resp, err := allowed.Get(srv.URL) | ||
| if err != nil { | ||
| t.Fatalf("listed loopback: %v", err) | ||
| } | ||
| resp.Body.Close() | ||
|
|
||
| for name, tr := range map[string]http.RoundTripper{ | ||
| "no list": netutil.SafeTransport(logger, "test"), | ||
| "other network": netutil.SafeTransportAllowing(logger, "test", mustCIDRs(t, "10.200.50.0/24")), | ||
| } { | ||
| if resp, err := (&http.Client{Transport: tr}).Get(srv.URL); err == nil { | ||
| resp.Body.Close() | ||
| t.Errorf("%s: loopback target reached; want refused", name) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The example receiver here is
http://, but the admin webhooks page rejects any URL that does not start withhttps://(frontend/src/routes/webhooks/+page.svelte:108), so this exact setup can only be created throughPOST /v1/webhookswith an API key. Either say that in this row or relax the UI check for receivers the operator has allowlisted.Technical details