Skip to content

Commit 9632d2b

Browse files
committed
Add principals regexp
Signed-off-by: Peter Verraedt <peter.verraedt@kuleuven.be>
1 parent da57fc2 commit 9632d2b

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

models/asymkey/ssh_key_authorized_keys.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"io"
1111
"os"
1212
"path/filepath"
13+
"regexp"
1314
"strings"
1415
"sync"
1516

@@ -50,12 +51,18 @@ func WriteAuthorizedStringForValidKey(key *PublicKey, w io.Writer) error {
5051
return err
5152
}
5253

54+
var principalRegexp = regexp.MustCompile(`^[^\n\r]+$`)
55+
5356
func writeAuthorizedStringForKey(key *PublicKey, w io.Writer) (keyValid bool, err error) {
5457
const tpl = AuthorizedStringCommentPrefix + "\n" + `command=%s,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict %s` + "\n"
5558

5659
var sshKey string
5760

5861
if key.Type == KeyTypePrincipal {
62+
if matched := principalRegexp.MatchString(key.Content); !matched {
63+
return false, fmt.Errorf("does not match %s", principalRegexp.String())
64+
}
65+
5966
sshKey = fmt.Sprintf("%s # user-%d", key.Content, key.OwnerID)
6067
} else {
6168
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key.Content))

0 commit comments

Comments
 (0)