Skip to content

Commit c6a4612

Browse files
authored
ipn/localapi: require Write access on /watch-ipn-bus with private keys (tailscale#10059)
Clients optionally request private key filtering. If they don't, we should require Write access for the user. Updates tailscale/corp#15506 Signed-off-by: Andrew Lytvynov <awly@tailscale.com>
1 parent 47019ce commit c6a4612

File tree

2 files changed

+107
-1
lines changed

2 files changed

+107
-1
lines changed

ipn/localapi/localapi.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,6 @@ func (h *Handler) serveWatchIPNBus(w http.ResponseWriter, r *http.Request) {
10491049
http.Error(w, "not a flusher", http.StatusInternalServerError)
10501050
return
10511051
}
1052-
w.Header().Set("Content-Type", "application/json")
10531052

10541053
var mask ipn.NotifyWatchOpt
10551054
if s := r.FormValue("mask"); s != "" {
@@ -1060,6 +1059,16 @@ func (h *Handler) serveWatchIPNBus(w http.ResponseWriter, r *http.Request) {
10601059
}
10611060
mask = ipn.NotifyWatchOpt(v)
10621061
}
1062+
// Users with only read access must request private key filtering. If they
1063+
// don't filter out private keys, require write access.
1064+
if (mask & ipn.NotifyNoPrivateKeys) == 0 {
1065+
if !h.PermitWrite {
1066+
http.Error(w, "watch IPN bus access denied, must set ipn.NotifyNoPrivateKeys when not running as admin/root or operator", http.StatusForbidden)
1067+
return
1068+
}
1069+
}
1070+
1071+
w.Header().Set("Content-Type", "application/json")
10631072
ctx := r.Context()
10641073
h.b.WatchNotifications(ctx, mask, f.Flush, func(roNotify *ipn.Notify) (keepGoing bool) {
10651074
js, err := json.Marshal(roNotify)

ipn/localapi/localapi_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ package localapi
55

66
import (
77
"bytes"
8+
"context"
89
"encoding/json"
10+
"errors"
11+
"fmt"
912
"io"
1013
"net/http"
1114
"net/http/httptest"
@@ -17,8 +20,13 @@ import (
1720
"tailscale.com/client/tailscale/apitype"
1821
"tailscale.com/ipn"
1922
"tailscale.com/ipn/ipnlocal"
23+
"tailscale.com/ipn/store/mem"
2024
"tailscale.com/tailcfg"
25+
"tailscale.com/tsd"
2126
"tailscale.com/tstest"
27+
"tailscale.com/types/logger"
28+
"tailscale.com/types/logid"
29+
"tailscale.com/wgengine"
2230
)
2331

2432
func TestValidHost(t *testing.T) {
@@ -212,3 +220,92 @@ func TestShouldDenyServeConfigForGOOSAndUserContext(t *testing.T) {
212220
})
213221
}
214222
}
223+
224+
func TestServeWatchIPNBus(t *testing.T) {
225+
tstest.Replace(t, &validLocalHostForTesting, true)
226+
227+
tests := []struct {
228+
desc string
229+
permitRead, permitWrite bool
230+
mask ipn.NotifyWatchOpt // extra bits in addition to ipn.NotifyInitialState
231+
wantStatus int
232+
}{
233+
{
234+
desc: "no-permission",
235+
permitRead: false,
236+
permitWrite: false,
237+
wantStatus: http.StatusForbidden,
238+
},
239+
{
240+
desc: "read-initial-state",
241+
permitRead: true,
242+
permitWrite: false,
243+
wantStatus: http.StatusForbidden,
244+
},
245+
{
246+
desc: "read-initial-state-no-private-keys",
247+
permitRead: true,
248+
permitWrite: false,
249+
mask: ipn.NotifyNoPrivateKeys,
250+
wantStatus: http.StatusOK,
251+
},
252+
{
253+
desc: "read-initial-state-with-private-keys",
254+
permitRead: true,
255+
permitWrite: true,
256+
wantStatus: http.StatusOK,
257+
},
258+
}
259+
260+
for _, tt := range tests {
261+
t.Run(tt.desc, func(t *testing.T) {
262+
h := &Handler{
263+
PermitRead: tt.permitRead,
264+
PermitWrite: tt.permitWrite,
265+
b: newTestLocalBackend(t),
266+
}
267+
s := httptest.NewServer(h)
268+
defer s.Close()
269+
c := s.Client()
270+
271+
ctx, cancel := context.WithCancel(context.Background())
272+
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/localapi/v0/watch-ipn-bus?mask=%d", s.URL, ipn.NotifyInitialState|tt.mask), nil)
273+
if err != nil {
274+
t.Fatal(err)
275+
}
276+
res, err := c.Do(req)
277+
if err != nil {
278+
t.Fatal(err)
279+
}
280+
defer res.Body.Close()
281+
// Cancel the context so that localapi stops streaming IPN bus
282+
// updates.
283+
cancel()
284+
body, err := io.ReadAll(res.Body)
285+
if err != nil && !errors.Is(err, context.Canceled) {
286+
t.Fatal(err)
287+
}
288+
if res.StatusCode != tt.wantStatus {
289+
t.Errorf("res.StatusCode=%d, want %d. body: %s", res.StatusCode, tt.wantStatus, body)
290+
}
291+
})
292+
}
293+
}
294+
295+
func newTestLocalBackend(t testing.TB) *ipnlocal.LocalBackend {
296+
var logf logger.Logf = logger.Discard
297+
sys := new(tsd.System)
298+
store := new(mem.Store)
299+
sys.Set(store)
300+
eng, err := wgengine.NewFakeUserspaceEngine(logf, sys.Set)
301+
if err != nil {
302+
t.Fatalf("NewFakeUserspaceEngine: %v", err)
303+
}
304+
t.Cleanup(eng.Close)
305+
sys.Set(eng)
306+
lb, err := ipnlocal.NewLocalBackend(logf, logid.PublicID{}, sys, 0)
307+
if err != nil {
308+
t.Fatalf("NewLocalBackend: %v", err)
309+
}
310+
return lb
311+
}

0 commit comments

Comments
 (0)