From e93046aa68251dc578dbaa092759e41eb906d01d Mon Sep 17 00:00:00 2001 From: Kristoffer Dalby Date: Wed, 4 Nov 2026 13:22:10 +0000 Subject: [PATCH] policy/v2: word SSH rule errors as Tailscale does Check src and dst kinds in validate, not in JSON decoding, so they get the label. Texts from SaaS acl/validate answers. Updates #3335 --- CHANGELOG.md | 2 +- hscontrol/policy/v2/types.go | 62 ++++++++++++++----------------- hscontrol/policy/v2/types_test.go | 61 ++++++++++++++++++++++++------ 3 files changed, 77 insertions(+), 48 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5db247379..9bde4d073 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -197,7 +197,7 @@ the ACME DNS-01 challenge through a built-in DNS provider: `acmedns`, - PostgreSQL connection settings are assembled into a `postgres://` URL with proper escaping, so a password (or host, user, database name) containing spaces, quotes or other reserved characters no longer truncates the DSN [#3477](https://github.com/juanfont/headscale/pull/3477) - Expiring or deleting a non-existent pre-auth key now returns an error instead of silently succeeding [#3324](https://github.com/juanfont/headscale/pull/3324) - Improve systemd service file hardening [#3341](https://github.com/juanfont/headscale/pull/3341) -- Errors in SSH rules start with `[ssh]`, the label Tailscale uses, for example `[ssh] users must be specified` [#3343](https://github.com/juanfont/headscale/pull/3343) +- Errors in SSH rules start with `[ssh]` and use Tailscale's wording, for example `[ssh] users must be specified`; an undefined group now reads `group not found` [#3343](https://github.com/juanfont/headscale/pull/3343) - Fix `headscale users destroy`/`rename` reporting "multiple users match query" when no user matches; an ambiguous match now lists the matching users [#3476](https://github.com/juanfont/headscale/pull/3476) - Deleting a user that still owns nodes now lists the nodes (ID and hostname) that must be deleted first [#3475](https://github.com/juanfont/headscale/pull/3475) - `headscale preauthkeys create --user` accepts a user name as well as an ID diff --git a/hscontrol/policy/v2/types.go b/hscontrol/policy/v2/types.go index a2045eebf..ceae72205 100644 --- a/hscontrol/policy/v2/types.go +++ b/hscontrol/policy/v2/types.go @@ -47,17 +47,21 @@ var ( ErrSSHUserDestRequiresSameUser = errors.New("user destination requires source to contain only that same user") ErrSSHAutogroupSelfRequiresUserSource = errors.New("autogroup:self destination requires source to contain only users or groups, not tags or autogroup:tagged") ErrSSHTagSourceToAutogroupMember = errors.New("tags in SSH source cannot access autogroup:member (user-owned devices)") - ErrSSHWildcardDestination = errors.New("wildcard (*) is not supported as SSH destination") ErrSSHCheckPeriodAboveMax = errors.New("is above the max (168h)") ErrSSHCheckPeriodNegative = errors.New("must be a positive duration") - ErrSSHCheckPeriodOnNonCheck = errors.New("checkPeriod is only valid with action \"check\"") + ErrSSHCheckPeriodOnNonCheck = errors.New(`"checkPeriod" can only be specified for the "check" action`) ErrInvalidLocalpart = errors.New("invalid localpart format, must be localpart:*@") ErrSSHUsersMustBeSpecified = errors.New("users must be specified") ErrSSHUserInvalid = errors.New("is not valid") ErrSSHAcceptEnvEmpty = errors.New("acceptEnv values cannot be empty") ErrSSHActionMustBeSpecified = errors.New("action must be specified") ErrSSHActionInvalid = errors.New("is not a valid action") - ErrSSHDestinationHostAlias = errors.New("invalid dst") + ErrSSHInvalidDst = errors.New("invalid dst") + ErrSSHGroupDst = errors.New("groups are not allowed dst") + ErrSSHSrcNotAllowed = errors.New("is not allowed") + ErrSSHSrcAutogroupNotAllowed = errors.New("is not allowed in src") + ErrSSHSrcIP = errors.New("IPs and CIDRs are not allowed") + ErrSSHSrcHost = errors.New("hosts are not allowed") ErrSSHCheckTagSource = errors.New(`"check" action does not support tags in src`) ErrTagNameMustStartWithLetter = errors.New("tag names must start with a letter, after 'tag:'") ErrGroupMembersCannotBeRecursive = errors.New("group members cannot be recursive") @@ -173,7 +177,7 @@ var ( ErrInvalidAlias = errors.New("invalid alias format") ErrInvalidAutoApprover = errors.New("invalid auto approver format") ErrInvalidOwner = errors.New("invalid owner format") - ErrGroupNotDefined = errors.New("group not defined in policy") + ErrGroupNotDefined = errors.New("group not found") ErrInvalidGroupMember = errors.New("invalid group member type") ErrGroupValueNotArray = errors.New("group value must be an array of users") ErrInvalidHostIP = errors.New("hostname contains invalid IP address") @@ -190,11 +194,7 @@ var ( ErrAutogroupNotSupportedACLSrc = errors.New("autogroup not supported for ACL sources") ErrAutogroupNotSupportedACLDst = errors.New("autogroup not supported for ACL destinations") ErrAutogroupDangerAllDst = errors.New("cannot use autogroup:danger-all as a dst") - ErrAutogroupNotSupportedSSHSrc = errors.New("autogroup not supported for SSH sources") - ErrAutogroupNotSupportedSSHDst = errors.New("autogroup not supported for SSH destinations") ErrHostNotDefined = errors.New("host not defined in policy") - ErrSSHSourceAliasNotSupported = errors.New("alias not supported for SSH source") - ErrSSHDestAliasNotSupported = errors.New("alias not supported for SSH destination") ErrUnknownField = errors.New("unknown field") ErrProtocolNoSpecificPorts = errors.New("protocol does not support specific ports") ErrTestEmptyAssertions = errors.New("test entry must have at least one of \"accept\" or \"deny\"") @@ -2327,12 +2327,8 @@ func validateAutogroupForSSHSrc(src *AutoGroup) error { return nil } - if src.Is(AutoGroupInternet) { - return ErrAutogroupInternetSrc - } - if !slices.Contains(autogroupForSSHSrc, *src) { - return fmt.Errorf("%w: %q, can be %v", ErrAutogroupNotSupportedSSHSrc, *src, autogroupForSSHSrc) + return fmt.Errorf("%q %w", *src, ErrSSHSrcAutogroupNotAllowed) } return nil @@ -2343,12 +2339,8 @@ func validateAutogroupForSSHDst(dst *AutoGroup) error { return nil } - if dst.Is(AutoGroupInternet) { - return ErrAutogroupInternetSrc - } - if !slices.Contains(autogroupForSSHDst, *dst) { - return fmt.Errorf("%w: %q, can be %v", ErrAutogroupNotSupportedSSHDst, *dst, autogroupForSSHDst) + return fmt.Errorf("%w %q", ErrSSHInvalidDst, *dst) } return nil @@ -2708,6 +2700,12 @@ func (p *Policy) validate() error { if err != nil { errs = append(errs, err) } + case Asterix: + errs = append(errs, fmt.Errorf("%q %w", src, ErrSSHSrcNotAllowed)) + case *Prefix: + errs = append(errs, fmt.Errorf("%q: %w", filterIP(netip.Prefix(*src)), ErrSSHSrcIP)) + case *Host: + errs = append(errs, fmt.Errorf("%q: %w", string(*src), ErrSSHSrcHost)) } } @@ -2740,10 +2738,16 @@ func (p *Policy) validate() error { if err != nil { errs = append(errs, err) } + case *Group: + errs = append(errs, ErrSSHGroupDst) + case Asterix: + errs = append(errs, fmt.Errorf("%w %q", ErrSSHInvalidDst, dst)) + case *Prefix: + errs = append(errs, fmt.Errorf("%w %q", ErrSSHInvalidDst, filterIP(netip.Prefix(*dst)))) case *Host: // Hosts-table aliases are valid on ACL dst but // rejected here for SSH dst. - errs = append(errs, fmt.Errorf("%w %q", ErrSSHDestinationHostAlias, string(*dst))) + errs = append(errs, fmt.Errorf("%w %q", ErrSSHInvalidDst, string(*dst))) } } @@ -3174,14 +3178,10 @@ func (a *SSHSrcAliases) UnmarshalJSON(b []byte) error { return err } + // Kinds an SSH rule refuses are reported by [Policy.validate]. *a = make([]Alias, len(aliases)) for i, alias := range aliases { - switch alias.Alias.(type) { - case *Username, *Group, *Tag, *AutoGroup: - (*a)[i] = alias.Alias - default: - return fmt.Errorf("%w: %T", ErrSSHSourceAliasNotSupported, alias.Alias) - } + (*a)[i] = alias.Alias } return nil @@ -3195,18 +3195,10 @@ func (a *SSHDstAliases) UnmarshalJSON(b []byte) error { return err } + // Kinds an SSH rule refuses are reported by [Policy.validate]. *a = make([]Alias, len(aliases)) for i, alias := range aliases { - switch alias.Alias.(type) { - case *Username, *Tag, *AutoGroup, *Host: - (*a)[i] = alias.Alias - case Asterix: - return fmt.Errorf("%w; use 'autogroup:member' for user-owned devices, "+ - "'autogroup:tagged' for tagged devices, or specific tags/users", - ErrSSHWildcardDestination) - default: - return fmt.Errorf("%w: %T", ErrSSHDestAliasNotSupported, alias.Alias) - } + (*a)[i] = alias.Alias } return nil diff --git a/hscontrol/policy/v2/types_test.go b/hscontrol/policy/v2/types_test.go index 94e97b814..5550deebb 100644 --- a/hscontrol/policy/v2/types_test.go +++ b/hscontrol/policy/v2/types_test.go @@ -368,7 +368,7 @@ func TestUnmarshalPolicy(t *testing.T) { ] } `, - wantErr: "alias not supported for SSH source: v2.Asterix", + wantErr: `[ssh] "*" is not allowed`, }, { name: "invalid-username", @@ -653,7 +653,7 @@ func TestUnmarshalPolicy(t *testing.T) { ] } `, - wantErr: `autogroup:internet can only be used in ACL destinations`, + wantErr: `[ssh] invalid dst "autogroup:internet"`, }, { name: "ssh-basic", @@ -787,7 +787,7 @@ func TestUnmarshalPolicy(t *testing.T) { ] } `, - wantErr: `group not defined in policy: "group:notdefined"`, + wantErr: `group not found: "group:notdefined"`, }, { name: "group-must-be-defined-acl-dst", @@ -806,7 +806,7 @@ func TestUnmarshalPolicy(t *testing.T) { ] } `, - wantErr: `group not defined in policy: "group:notdefined"`, + wantErr: `group not found: "group:notdefined"`, }, { name: "group-must-be-defined-acl-ssh-src", @@ -836,7 +836,7 @@ func TestUnmarshalPolicy(t *testing.T) { }, } `, - wantErr: `group not defined in policy: "group:notdefined"`, + wantErr: `group not found: "group:notdefined"`, }, { name: "group-must-be-defined-acl-autoapprover-route", @@ -849,7 +849,7 @@ func TestUnmarshalPolicy(t *testing.T) { }, } `, - wantErr: `group not defined in policy: "group:notdefined"`, + wantErr: `group not found: "group:notdefined"`, }, { name: "group-must-be-defined-acl-autoapprover-exitnode", @@ -860,7 +860,7 @@ func TestUnmarshalPolicy(t *testing.T) { }, } `, - wantErr: `group not defined in policy: "group:notdefined"`, + wantErr: `group not found: "group:notdefined"`, }, { name: "tag-must-be-defined-acl-src", @@ -1738,7 +1738,7 @@ func TestUnmarshalPolicy(t *testing.T) { }] } `, - wantErr: "wildcard (*) is not supported as SSH destination", + wantErr: `[ssh] invalid dst "*"`, }, { name: "ssh-group-to-tag-allowed", @@ -4735,7 +4735,7 @@ func TestSSHRuleSaaSValidation(t *testing.T) { ssh: baseSSH(func(s *SSH) { s.Destinations = SSHDstAliases{hp("srv")} }), - wantErr: ErrSSHDestinationHostAlias, + wantErr: ErrSSHInvalidDst, }, } @@ -4790,7 +4790,7 @@ func TestSSHRuleErrorsLabelled(t *testing.T) { { name: "checkPeriod on accept", rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"], "users": ["root"], "checkPeriod": "1h"}`, - wantErr: `[ssh] checkPeriod is only valid with action "check"`, + wantErr: `[ssh] "checkPeriod" can only be specified for the "check" action`, }, { name: "checkPeriod above max", @@ -4810,7 +4810,42 @@ func TestSSHRuleErrorsLabelled(t *testing.T) { { name: "unknown group in src", rules: `{"action": "accept", "src": ["group:nope"], "dst": ["tag:server"], "users": ["root"]}`, - wantErr: `[ssh] group not defined in policy: "group:nope"`, + wantErr: `[ssh] group not found: "group:nope"`, + }, + { + name: "wildcard src", + rules: `{"action": "accept", "src": ["*"], "dst": ["tag:server"], "users": ["root"]}`, + wantErr: `[ssh] "*" is not allowed`, + }, + { + name: "IP src", + rules: `{"action": "accept", "src": ["100.64.0.1"], "dst": ["tag:server"], "users": ["root"]}`, + wantErr: `[ssh] "100.64.0.1": IPs and CIDRs are not allowed`, + }, + { + name: "host src", + rules: `{"action": "accept", "src": ["srv"], "dst": ["tag:server"], "users": ["root"]}`, + wantErr: `[ssh] "srv": hosts are not allowed`, + }, + { + name: "autogroup:self src", + rules: `{"action": "accept", "src": ["autogroup:self"], "dst": ["tag:server"], "users": ["root"]}`, + wantErr: `[ssh] "autogroup:self" is not allowed in src`, + }, + { + name: "CIDR dst", + rules: `{"action": "accept", "src": ["odin@"], "dst": ["100.64.0.0/24"], "users": ["root"]}`, + wantErr: `[ssh] invalid dst "100.64.0.0/24"`, + }, + { + name: "group dst", + rules: `{"action": "accept", "src": ["odin@"], "dst": ["group:admins"], "users": ["root"]}`, + wantErr: `[ssh] groups are not allowed dst`, + }, + { + name: "autogroup:internet dst", + rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:internet"], "users": ["root"]}`, + wantErr: `[ssh] invalid dst "autogroup:internet"`, }, { name: "check with tag src", @@ -4829,6 +4864,8 @@ func TestSSHRuleErrorsLabelled(t *testing.T) { t.Run(tt.name, func(t *testing.T) { _, err := unmarshalPolicy([]byte(`{ "tagOwners": {"tag:server": ["odin@"]}, + "groups": {"group:admins": ["odin@"]}, + "hosts": {"srv": "100.64.0.9"}, "ssh": [` + tt.rules + `] }`)) require.EqualError(t, err, tt.wantErr) @@ -5577,7 +5614,7 @@ func TestUnmarshalGrants(t *testing.T) { ] } `, - wantErr: "group not defined in policy", + wantErr: "group not found", }, { name: "invalid-grant-undefined-source-tag",