Skip to content
Merged
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
9 changes: 8 additions & 1 deletion hallpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,9 @@ func (at *accessTypes) DurationOptions() (ret []durationDropdown) {
cands = append(cands, d)
}
}
// why: never offer a duration above the absolute MaxTTL cap, even if a
// per-access-type Max is configured higher.
max = min(max, MaxTTL)
slices.Sort(cands)
cands = slices.Compact(cands)
for _, d := range cands {
Expand Down Expand Up @@ -435,6 +438,10 @@ func (tds timeDurationString) MarshalJSON() ([]byte, error) {

const grantApp = "github.com/tailscale/hallpass"

// MaxTTL is the absolute maximum duration any hallpass access grant may be
// issued for, regardless of per-access-type Max configuration.
const MaxTTL = 12 * time.Hour

func (s *Server) lookup(r *http.Request) (ret lookupInfo, err error) {
who, err := s.localClient.WhoIs(r.Context(), r.RemoteAddr)
if err != nil {
Expand Down Expand Up @@ -579,7 +586,7 @@ func (s *Server) authorize(ctx context.Context, li lookupInfo, req authorizeRequ
if !ok {
return fmt.Errorf("user %q not allowed to request access key %q", li.Who, req.AttrKey)
}
maxDur := cmp.Or(time.Duration(at.Max), 24*time.Hour)
maxDur := min(cmp.Or(time.Duration(at.Max), MaxTTL), MaxTTL)
if req.Duration > maxDur {
return fmt.Errorf("requested duration %v exceeds max %v for access type %q", req.Duration, maxDur, at.Name)
}
Expand Down
40 changes: 40 additions & 0 deletions hallpass_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,53 @@ import (
"io"
"log"
"reflect"
"slices"
"testing"
"time"

"tailscale.com/client/tailscale/apitype"
"tailscale.com/tailcfg"
)

func TestDurationOptionsCap(t *testing.T) {
at := &accessTypes{
Types: []accessTypeConfig{{
Name: "Short",
Attr: "custom:short",
Max: timeDurationString(15 * time.Minute),
Default: timeDurationString(15 * time.Minute),
}, {
Name: "Long",
Attr: "custom:long",
Max: timeDurationString(168 * time.Hour),
Default: timeDurationString(1 * time.Hour),
}},
}
var got []time.Duration
for _, opt := range at.DurationOptions() {
d, err := time.ParseDuration(opt.GoStr)
if err != nil {
t.Fatalf("parsing option %q: %v", opt.GoStr, err)
}
if d > MaxTTL {
t.Errorf("option %v exceeds MaxTTL %v", d, MaxTTL)
}
got = append(got, d)
}
want := []time.Duration{
15 * time.Minute,
30 * time.Minute,
1 * time.Hour,
2 * time.Hour,
4 * time.Hour,
8 * time.Hour,
12 * time.Hour,
}
if !slices.Equal(got, want) {
t.Errorf("DurationOptions() durations = %v, want %v", got, want)
}
}

func TestParseAccessTypeConfig(t *testing.T) {
defer log.SetOutput(log.Default().Writer())
log.SetOutput(io.Discard)
Expand Down
Loading