mirror of
https://github.com/tailscale/tailscale.git
synced 2026-11-20 00:31:17 -08:00
tka: test that ValidateDeeplink rejects each missing parameter
The os case failed before the previous commit: a link with no os parameter and an HMAC over an empty OS name validated successfully. Change-Id: I087c66749475ac4c60130d700f4dc74dde60987f Signed-off-by: Brad Fitzpatrick <[email protected]>
This commit is contained in:
1 parent
89b8a8e561
commit
4aebff0a31
1 file changed
+69
@@ -4,6 +4,8 @@
|
||||
package tka
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"testing"
|
||||
)
|
||||
|
||||
@@ -46,3 +48,70 @@ func TestGenerateDeeplink(t *testing.T) {
|
||||
t.Errorf("tlpub mismatch: %s != %s", res.TLPub, tlPub)
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateDeeplinkMissingParam(t *testing.T) {
|
||||
pub, _ := testingKey25519(t, 1)
|
||||
key := Key{Kind: Key25519, Public: pub, Votes: 2}
|
||||
c := newTestchain(t, `
|
||||
G1 -> L1
|
||||
|
||||
G1.template = genesis
|
||||
`, genesisTemplate(key),
|
||||
)
|
||||
a, _ := Open(c.Chonk())
|
||||
|
||||
tests := []struct {
|
||||
param string
|
||||
clear func(*NewDeeplinkParams)
|
||||
wantErr string
|
||||
}{
|
||||
{"nk", func(p *NewDeeplinkParams) { p.NodeKey = "" }, "missing nk (NodeKey) query parameter"},
|
||||
{"tp", func(p *NewDeeplinkParams) { p.TLPub = "" }, "missing tp (TLPub) query parameter"},
|
||||
{"dn", func(p *NewDeeplinkParams) { p.DeviceName = "" }, "missing dn (DeviceName) query parameter"},
|
||||
{"os", func(p *NewDeeplinkParams) { p.OSName = "" }, "missing os (OSName) query parameter"},
|
||||
{"em", func(p *NewDeeplinkParams) { p.LoginName = "" }, "missing em (EmailAddress) query parameter"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.param, func(t *testing.T) {
|
||||
params := NewDeeplinkParams{
|
||||
NodeKey: "nodekey:1234567890",
|
||||
TLPub: "tlpub:1234567890",
|
||||
DeviceName: "Example Device",
|
||||
OSName: "iOS",
|
||||
LoginName: "[email protected]",
|
||||
}
|
||||
tt.clear(¶ms)
|
||||
|
||||
// Build the link by hand, since NewDeeplink refuses empty
|
||||
// fields. The HMAC covers the empty field, so only the
|
||||
// missing parameter check can reject the link.
|
||||
v := url.Values{}
|
||||
for k, val := range map[string]string{
|
||||
"nk": params.NodeKey,
|
||||
"tp": params.TLPub,
|
||||
"dn": params.DeviceName,
|
||||
"os": params.OSName,
|
||||
"em": params.LoginName,
|
||||
} {
|
||||
if val != "" {
|
||||
v.Set(k, val)
|
||||
}
|
||||
}
|
||||
v.Set("hm", hex.EncodeToString(a.generateHMAC(params)))
|
||||
u := url.URL{
|
||||
Scheme: DeeplinkTailscaleURLScheme,
|
||||
Host: DeeplinkCommandSign,
|
||||
Path: "/v1/",
|
||||
RawQuery: v.Encode(),
|
||||
}
|
||||
|
||||
res := a.ValidateDeeplink(u.String())
|
||||
if res.IsValid {
|
||||
t.Fatalf("deeplink without %q validated successfully", tt.param)
|
||||
}
|
||||
if res.Error != tt.wantErr {
|
||||
t.Errorf("error = %q; want %q", res.Error, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user