Open citrus-it opened 1 year ago
Broadly our approach is that the API should be ultimately responsible for avoiding broken input, but we try to do as much of the same validation client-side as we can, both for responsiveness and because we can usually provide better error messages than the API does. When the API includes a regex pattern in the OpenAPI spec, we get it for free on our end — we generate runtime parsers from those. In general we try to avoid doing our own validation that goes beyond what the API allows, so I would prefer to add it API side first and then use their regex.
As you suggest, right now there is no API-side validation on the public key:
Trying to think about the broadest possible validation that is still useful.
Ignoring the first part – I had initially thought we could check for ssh-(...)
but based on that ssh_util.py
file it looks like we can't rely on that.
What if we take that string, split the string by spaces and run atob()
on the second part. If it doesn't throw we know it's base64 decodable and likely formatted like a public key? That might catch most of the obvious mistakes a user might make.
Thoughts @iliana
I also think that the validation probably shouldn't prevent submission.
We could add a message that appears whenever the field is blurred, but that wouldn't catch a user entering it into the key field and submitting directly. It could run onBlur
and whenever something is pasted into an input. Or we block submission initially if it doesn't seem valid, and the user can just submit again to ignore it.
Soft validation seems useful, that sounds like a good idea.
https://www.ibm.com/docs/en/zos/2.5.0?topic=daemon-format-authorized-keys-file is a pretty good reference of what is expected by ~/.ssh/authorized_keys and also cloud-init (albeit not a canonical reference), but the tl;dr is that an SSH key is made up of up to four words:
ssh-ed25519
)So running atob()
on the second part is not necessarily correct either; if someone with an SSH CA comes along and tries to paste in their CA pubkey with the cert-authority
option, the second-field might be something like ssh-rsa
. But those people probably know they're operating outside the ordinary and if soft validation doesn't stop them, it'll be fine.
What might be most careful is something like:
atob()
the second one; if that fails, soft erroratob()
the second one; if that fails, atob()
the third one; if that fails, soft erroratob()
the third one, etc etc etc
This could be something that should be addressed in the API, but there appears to be no validation on what one can type into the SSH key box. As a minimum we should probably check that there are two or three words -
<type> <value> [comment]
so we don't end up with:cloud-init in guests will just reject this - it actually checks against a specific list of key types but we may not want to duplicate and maintain that list, and it will ultimately depend on what the guest is running for cloud-init (which might not even be cloud-init, just something that uses the same data source).