Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit ea3d044

Browse files
authored
Fix SSH public key validation (#5323)
1 parent 59146fa commit ea3d044

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/webportal/src/app/user/fabric/user-profile/ssh-list-dialog.jsx

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,23 @@ import {
1212
DialogFooter,
1313
TextField,
1414
} from 'office-ui-fabric-react';
15+
import sshpk from 'sshpk';
1516

1617
import t from '../../../components/tachyons.scss';
1718

19+
const validateSSHPublicKey = keyString => {
20+
try {
21+
const key = sshpk.parseKey(keyString, 'ssh');
22+
if (sshpk.Key.isKey(key)) {
23+
return true;
24+
} else {
25+
return false;
26+
}
27+
} catch (error) {
28+
return false;
29+
}
30+
};
31+
1832
const SSHListDialog = ({ sshKeys, onDismiss, onAddPublickeys }) => {
1933
const [error, setError] = useState('');
2034
const [inputTitleError, setInputTitleError] = useState('');
@@ -37,13 +51,8 @@ const SSHListDialog = ({ sshKeys, onDismiss, onAddPublickeys }) => {
3751
} else {
3852
surefireTitle = true;
3953
}
40-
if (
41-
value.trim() === '' ||
42-
!value.trim().match(/^ssh-rsa AAAA[0-9A-Za-z+/]+[=]{0,3}.*$/)
43-
) {
44-
setInputValueError(
45-
'Please input correct SSH Public key, it should be starting with ssh-rsa.',
46-
);
54+
if (value.trim() === '' || !validateSSHPublicKey(value.trim())) {
55+
setInputValueError('Please input correct SSH Public key.');
4756
} else {
4857
surefireValue = true;
4958
}
@@ -81,7 +90,7 @@ const SSHListDialog = ({ sshKeys, onDismiss, onAddPublickeys }) => {
8190
<div>
8291
<div className={t.mt1}>
8392
<TextField
84-
label='Title (Please give the SSH key a name):'
93+
label='Title (Please give the SSH public key a name):'
8594
required={true}
8695
errorMessage={inputTitleError}
8796
onChange={e => {
@@ -93,7 +102,7 @@ const SSHListDialog = ({ sshKeys, onDismiss, onAddPublickeys }) => {
93102
</div>
94103
<div className={t.mt1}>
95104
<TextField
96-
label='Value (SSH Public key, starts with ssh-rsa):'
105+
label='SSH public key:'
97106
required={true}
98107
errorMessage={inputValueError}
99108
onChange={e => {

0 commit comments

Comments
 (0)