Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions tailscale.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,9 @@ func TsnetEnableFunnelToLocalhostPlaintextHttp1(sd C.int, localhostPort C.int) C
if err != nil {
return s.recErr(err)
}
if len(st.CertDomains) == 0 {
return s.recErr(fmt.Errorf("libtailscale: no cert domains; enable HTTPS for the tailnet"))
}
domain := st.CertDomains[0]

hp := ipn.HostPort(net.JoinHostPort(domain, strconv.Itoa(443)))
Expand All @@ -716,9 +719,8 @@ func TsnetEnableFunnelToLocalhostPlaintextHttp1(sd C.int, localhostPort C.int) C
},
}

lc.SetServeConfig(ctx, sc)
if !sc.AllowFunnel[hp] {
return s.recErr(fmt.Errorf("libtailscale: failed to enable funnel"))
if err := lc.SetServeConfig(ctx, sc); err != nil {
return s.recErr(err)
}

return 0
Expand Down
6 changes: 6 additions & 0 deletions tailscale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func TestGetRemoteAddr(t *testing.T) {
tsnetctest.RunTestGetRemoteAddr(t)
}

// TestFunnelNoCertDomains checks that tailscale_enable_funnel_to_localhost_plaintext_http1
// returns an error, rather than panicking, when the node has no cert domains.
func TestFunnelNoCertDomains(t *testing.T) {
tsnetctest.RunTestFunnelNoCertDomains(t)
}

// TestSocketpairCloexec checks that socketpair returns both ends
// close-on-exec, so that spawned processes cannot inherit the
// listener and connection fds.
Expand Down
66 changes: 66 additions & 0 deletions tsnetctest/tsnetctest.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,43 @@ int close_conn() {
return 0;
}

// test_funnel_no_cert_domains checks that enabling funnel on a node
// whose tailnet has no HTTPS cert domains fails with an error instead
// of crashing the process.
int test_funnel_no_cert_domains() {
if (err == NULL) {
err = calloc(errlen, 1);
}
tailscale s = tailscale_new();
if (tailscale_set_control_url(s, control_url) != 0) {
return set_err(s, 'a');
}
if (tailscale_set_dir(s, tmps1) != 0) {
return set_err(s, 'b');
}
if (tailscale_set_logfd(s, -1) != 0) {
return set_err(s, 'c');
}
if (tailscale_up(s) != 0) {
return set_err(s, 'd');
}
int ret = tailscale_enable_funnel_to_localhost_plaintext_http1(s, 8080);
if (ret == 0) {
snprintf(err, errlen, "funnel enabled without cert domains");
return 1;
}
char msg[256] = {0};
tailscale_errmsg(s, msg, sizeof msg - 1);
if (msg[0] == '\0') {
snprintf(err, errlen, "funnel failed (%d) with empty errmsg", ret);
return 1;
}
if (tailscale_close(s) != 0) {
return set_err(s, 'e');
}
return 0;
}

tailscale sa, sb, sc;
char* tmpsa = NULL;
char* tmpsb = NULL;
Expand Down Expand Up @@ -394,6 +431,35 @@ func RunTestConn(t *testing.T) {
}
}

// RunTestFunnelNoCertDomains runs the C-side test_funnel_no_cert_domains.
func RunTestFunnelNoCertDomains(t *testing.T) {
netns.SetEnabled(false)
t.Cleanup(func() {
netns.SetEnabled(true)
})

derpLogf := logger.Discard
if *verboseDERP {
derpLogf = t.Logf
}
derpMap := integration.RunDERPAndSTUN(t, derpLogf, "127.0.0.1")
control := &testcontrol.Server{
DERPMap: derpMap,
}
control.HTTPTestServer = httptest.NewUnstartedServer(control)
control.HTTPTestServer.Start()
t.Cleanup(control.HTTPTestServer.Close)
C.control_url = C.CString(control.HTTPTestServer.URL)

tmp := filepath.Join(t.TempDir(), "s1")
os.MkdirAll(tmp, 0755)
C.tmps1 = C.CString(tmp)

if C.test_funnel_no_cert_domains() != 0 {
t.Fatal(C.GoString(C.err))
}
}

// RunTestGetRemoteAddr runs the C-side test_getremoteaddr.
func RunTestGetRemoteAddr(t *testing.T) {
// Corp#4520: don't use netns for tests.
Expand Down