policy/v2: label SSH rule errors once with [ssh]

Wrap in the SSH loop, so shared tag and group checks get it too.
Tailscale's marker; replaces the per-message ssh: prefix.

Fixes #3335
This commit is contained in:
Kristoffer Dalby committed 2026-11-05 13:43:48 +01:00
1 parent f8d3703a80
commit 87b110b335
4 files changed
+108 -22

No files matched your search

+1 -1
View File
@@ -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)
- SSH policy validation errors now prefix their messages with `ssh:` so failures like `ssh: users must be specified` make it clear that the problem comes from an SSH rule violation [#3343](https://github.com/juanfont/headscale/pull/3343)
- 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)
- 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
+1 -1
View File
@@ -483,7 +483,7 @@ func TestSSHPolicyRules(t *testing.T) {
]
}`,
expectErr: true,
errorMessage: `ssh: "invalid" is not a valid action`,
errorMessage: `"invalid" is not a valid action`,
},
{
name: "invalid-check-period",
+20 -12
View File
@@ -45,20 +45,20 @@ var ErrUndefinedTagReference = errors.New("does not exist")
var (
ErrSSHTagSourceToUserDest = errors.New("tags in SSH source cannot access user-owned devices")
ErrSSHUserDestRequiresSameUser = errors.New("user destination requires source to contain only that same user")
ErrSSHAutogroupSelfRequiresUserSource = errors.New("ssh: autogroup:self destination requires source to contain only users or groups, not tags or autogroup:tagged")
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("ssh: checkPeriod is only valid with action \"check\"")
ErrSSHCheckPeriodOnNonCheck = errors.New("checkPeriod is only valid with action \"check\"")
ErrInvalidLocalpart = errors.New("invalid localpart format, must be localpart:*@<domain>")
ErrSSHUsersMustBeSpecified = errors.New("ssh: users must be specified")
ErrSSHUsersMustBeSpecified = errors.New("users must be specified")
ErrSSHUserInvalid = errors.New("is not valid")
ErrSSHAcceptEnvEmpty = errors.New("ssh: acceptEnv values cannot be empty")
ErrSSHActionMustBeSpecified = errors.New("ssh: action must be specified")
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")
ErrSSHCheckTagSource = errors.New(`[ssh] "check" action does not support tags in src`)
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")
)
@@ -1779,7 +1779,7 @@ func (a *SSHAction) UnmarshalJSON(b []byte) error {
case "check":
*a = SSHActionCheck
default:
return fmt.Errorf("ssh: %q %w", str, ErrSSHActionInvalid)
return fmt.Errorf("%q %w", str, ErrSSHActionInvalid)
}
return nil
@@ -2393,7 +2393,7 @@ func validateSSHSrcDstCombination(sources SSHSrcAliases, destinations SSHDstAlia
}
// Rule: Username destination requires source to be that same single user only
if srcHasGroups || len(srcUsernames) != 1 || !srcUsernames[string(*v)] {
return fmt.Errorf("ssh: %w %q; use autogroup:self instead for same-user SSH access",
return fmt.Errorf("%w %q; use autogroup:self instead for same-user SSH access",
ErrSSHUserDestRequiresSameUser, *v)
}
case *AutoGroup:
@@ -2650,6 +2650,8 @@ func (p *Policy) validate() error {
}
for _, ssh := range p.SSHs {
ruleErrs := len(errs)
// Empty action and users survive parse; surface them here.
if ssh.Action == "" {
errs = append(errs, ErrSSHActionMustBeSpecified)
@@ -2665,7 +2667,7 @@ func (p *Policy) validate() error {
for _, user := range ssh.Users {
switch user {
case "", "*":
errs = append(errs, fmt.Errorf("ssh: user %q %w", user, ErrSSHUserInvalid))
errs = append(errs, fmt.Errorf("user %q %w", user, ErrSSHUserInvalid))
}
}
@@ -2741,7 +2743,7 @@ func (p *Policy) validate() error {
case *Host:
// Hosts-table aliases are valid on ACL dst but
// rejected here for SSH dst.
errs = append(errs, fmt.Errorf("ssh: %w %q", ErrSSHDestinationHostAlias, string(*dst)))
errs = append(errs, fmt.Errorf("%w %q", ErrSSHDestinationHostAlias, string(*dst)))
}
}
@@ -2763,6 +2765,12 @@ func (p *Policy) validate() error {
}
}
}
// Shared checks (tags, groups) cannot tell which section they
// serve; this label is the marker Tailscale uses for SSH rules.
for i := ruleErrs; i < len(errs); i++ {
errs[i] = fmt.Errorf("[ssh] %w", errs[i])
}
}
for _, grant := range p.Grants {
@@ -3106,11 +3114,11 @@ func (p SSHCheckPeriod) Validate() error {
}
if p.Duration < 0 {
return fmt.Errorf("ssh: checkPeriod %s %w", p.Duration, ErrSSHCheckPeriodNegative)
return fmt.Errorf("checkPeriod %s %w", p.Duration, ErrSSHCheckPeriodNegative)
}
if p.Duration > SSHCheckPeriodMax {
return fmt.Errorf("ssh: checkPeriod %s %w", p.Duration, ErrSSHCheckPeriodAboveMax)
return fmt.Errorf("checkPeriod %s %w", p.Duration, ErrSSHCheckPeriodAboveMax)
}
return nil
+86 -8
View File
@@ -725,7 +725,7 @@ func TestUnmarshalPolicy(t *testing.T) {
]
}
`,
wantErr: `ssh: user "*" is not valid`,
wantErr: `user "*" is not valid`,
},
{
name: "ssh-with-check-period",
@@ -4758,6 +4758,84 @@ func TestSSHRuleSaaSValidation(t *testing.T) {
}
}
// TestSSHRuleErrorsLabelled verifies every error raised for an SSH rule
// carries the [ssh] label, including errors from checks shared with ACLs
// and grants (#3335).
func TestSSHRuleErrorsLabelled(t *testing.T) {
tests := []struct {
name string
rules string
wantErr string
}{
{
name: "users missing",
rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"]}`,
wantErr: `[ssh] users must be specified`,
},
{
name: "action missing",
rules: `{"src": ["odin@"], "dst": ["autogroup:self"], "users": ["root"]}`,
wantErr: `[ssh] action must be specified`,
},
{
name: "wildcard user",
rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"], "users": ["*"]}`,
wantErr: `[ssh] user "*" is not valid`,
},
{
name: "empty acceptEnv",
rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"], "users": ["root"], "acceptEnv": [""]}`,
wantErr: `[ssh] acceptEnv values cannot be empty`,
},
{
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"`,
},
{
name: "checkPeriod above max",
rules: `{"action": "check", "src": ["odin@"], "dst": ["autogroup:self"], "users": ["root"], "checkPeriod": "200h"}`,
wantErr: `[ssh] checkPeriod 200h0m0s is above the max (168h)`,
},
{
name: "unknown tag in src",
rules: `{"action": "accept", "src": ["tag:nope"], "dst": ["tag:server"], "users": ["root"]}`,
wantErr: `[ssh] tag not found: "tag:nope"`,
},
{
name: "unknown tag in dst",
rules: `{"action": "accept", "src": ["odin@"], "dst": ["tag:nope"], "users": ["root"]}`,
wantErr: `[ssh] tag not found: "tag:nope"`,
},
{
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"`,
},
{
name: "check with tag src",
rules: `{"action": "check", "src": ["tag:server"], "dst": ["tag:server"], "users": ["root"]}`,
wantErr: `[ssh] "check" action does not support tags in src`,
},
{
name: "error in second rule",
rules: `{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"], "users": ["root"]},
{"action": "accept", "src": ["odin@"], "dst": ["autogroup:self"]}`,
wantErr: `[ssh] users must be specified`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := unmarshalPolicy([]byte(`{
"tagOwners": {"tag:server": ["odin@"]},
"ssh": [` + tt.rules + `]
}`))
require.EqualError(t, err, tt.wantErr)
})
}
}
// TestSSHActionInvalidUnmarshal verifies the SaaS-aligned wording for
// non-empty unknown actions surfaces at JSON parse time.
func TestSSHActionInvalidUnmarshal(t *testing.T) {
@@ -4787,25 +4865,25 @@ func TestSSHActionInvalidUnmarshal(t *testing.T) {
name: "uppercase rejected",
input: `"ACCEPT"`,
wantErr: ErrSSHActionInvalid,
wantMsg: `ssh: "ACCEPT" is not a valid action`,
wantMsg: `"ACCEPT" is not a valid action`,
},
{
name: "mixedcase rejected",
input: `"Accept"`,
wantErr: ErrSSHActionInvalid,
wantMsg: `ssh: "Accept" is not a valid action`,
wantMsg: `"Accept" is not a valid action`,
},
{
name: "whitespace trimmed then mixedcase rejected",
input: `" Accept"`,
wantErr: ErrSSHActionInvalid,
wantMsg: `ssh: "Accept" is not a valid action`,
wantMsg: `"Accept" is not a valid action`,
},
{
name: "unknown action rejected",
input: `"deny"`,
wantErr: ErrSSHActionInvalid,
wantMsg: `ssh: "deny" is not a valid action`,
wantMsg: `"deny" is not a valid action`,
},
}
@@ -4906,7 +4984,7 @@ func TestSSHUserTrimEndToEnd(t *testing.T) {
_, err := unmarshalPolicy([]byte(policy))
require.Error(t, err)
require.ErrorIs(t, err, ErrSSHUserInvalid)
require.Contains(t, err.Error(), `ssh: user "" is not valid`)
require.Contains(t, err.Error(), `user "" is not valid`)
})
}
@@ -5046,14 +5124,14 @@ func TestSSHCheckPeriodInvalidDuration(t *testing.T) {
}
// TestSSHCheckPeriodNegativeMessage verifies the SaaS body for the
// negative-duration case (`ssh: checkPeriod -1m0s must be a positive duration`).
// negative-duration case (`checkPeriod -1m0s must be a positive duration`).
func TestSSHCheckPeriodNegativeMessage(t *testing.T) {
p := SSHCheckPeriod{Duration: -time.Minute}
err := p.Validate()
require.Error(t, err)
require.ErrorIs(t, err, ErrSSHCheckPeriodNegative)
require.Contains(t, err.Error(), "ssh: checkPeriod -1m0s must be a positive duration")
require.Contains(t, err.Error(), "checkPeriod -1m0s must be a positive duration")
}
func TestUnmarshalGrants(t *testing.T) {