Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate SSH key in front end using Js or react js?

I have a TextInput Area inside pasted some example ssh key to validate whether it is valid or not. Is there any npm package or regular expression? Example SSH key

   <TextArea
          id={'public-key'}
          labelText={_getPublicKeyLabel()}
          required
          aria-required="true"
          invalid={sshKeyInput.keyTouched && !_isPublicKeyValid()}
          invalidText={sshKeyInput.key.length > 0 ? 'please provide valid ssh key' : t('dashboard.ssh_key_modal.public_key_required')}
          rows={6} 
          light
          onChange={(ev) =>
            setSshKeyInput({
              ...sshKeyInput,
              key: ev.target.value,
              keyTouched: true,
            })
          }
        />

Exxample ssh-rsa AAAAB3NXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

If I enter this ssh key into text area how can we check whether it is valid or not. Please share me your thoughts or ideas

like image 514
Lavaraju Avatar asked Mar 01 '26 04:03

Lavaraju


1 Answers

The only thing that you can validate on the client-side is to make sure the string starts with ssh-rsa. The actual key itself contains arbitrary alphanumeric characters with no fixed patterns.

It is like a physical door key. Apart from the common structure like a key blade or a key head that makes you believe "oh that looks like some sort of key", you can't tell whether that key is real or forged until you actually insert the key into the keyhole (i.e. private key).

If I were you, I won't bother doing client-side validation on SSH key strings, because quite frankly, there isn't much you can do.

like image 183
Matthew Kwong Avatar answered Mar 03 '26 03:03

Matthew Kwong