-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from zlamalp/sshkeys
feat(registrar): support better SSH keys input validation
- Loading branch information
Showing
6 changed files
with
249 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
...cz/metacentrum/perun/wui/registrar/widgets/items/validators/SshKeysTextAreaValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package cz.metacentrum.perun.wui.registrar.widgets.items.validators; | ||
|
||
import com.google.gwt.regexp.shared.MatchResult; | ||
import com.google.gwt.regexp.shared.RegExp; | ||
import cz.metacentrum.perun.wui.registrar.widgets.items.TextArea; | ||
import org.gwtbootstrap3.client.ui.constants.ValidationState; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Pavel Zlámal <[email protected]> | ||
*/ | ||
public class SshKeysTextAreaValidator extends TextAreaValidator { | ||
|
||
RegExp regExp = RegExp.compile("^(" + | ||
"(ssh-(rsa|dss|ed25519)([email protected])?)|" + | ||
"(sk-(ssh-ed25519|ecdsa-sha2-nistp256)(-cert-v01)[email protected])|" + | ||
"(ecdsa-sha2-nistp(256|384|521)([email protected])?))" + | ||
" (([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?)( [^,\n]+)?$"); | ||
|
||
@Override | ||
public boolean validateLocal(TextArea textArea) { | ||
|
||
if (textArea.isRequired() && isNullOrEmpty(textArea.getValue())) { | ||
setResult(Result.EMPTY); | ||
textArea.setRawStatus(getTransl().cantBeEmpty(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (!textArea.getBox().isValid()) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getErrorMsgOrDefault(textArea), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (textArea.getValue() != null && textArea.getValue().length() > textArea.MAX_LENGTH) { | ||
setResult(Result.TOO_LONG); | ||
textArea.setStatus(getTransl().tooLong(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (textArea.getValue() != null && !textArea.getValue().isEmpty()) { | ||
|
||
String sshKeys = textArea.getValue(); | ||
|
||
if (sshKeys.contains(",,")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getTransl().tooMuchCommas(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (sshKeys.contains("\n\n")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getTransl().tooMuchNewlines(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (sshKeys.contains(",") && sshKeys.contains("\n")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getTransl().mixingNewlinesWithCommas(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (sshKeys.contains(", ") || sshKeys.contains(" ,") || sshKeys.contains("\n ") || sshKeys.contains(" \n")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getTransl().sshKeyNoSpaceAroundKeySeparator(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
// FIXME - this doesn't make sense anymore, as we have multiple different SSH keys prefixes, which needs to be checked. | ||
/* | ||
if (sshKeys.indexOf("ssh-") != sshKeys.lastIndexOf("ssh-")) { | ||
// there are at least two keys | ||
if (!sshKeys.contains(",ssh-") && !sshKeys.contains("\nssh-")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textArea.setStatus(getTransl().sshKeyMissingDelimiter(), ValidationState.ERROR); | ||
return false; | ||
} | ||
} | ||
*/ | ||
|
||
// normalize value just in case | ||
sshKeys = sshKeys.replaceAll("(\n)+", ","); | ||
sshKeys = sshKeys.replaceAll("(,)+", ","); | ||
List<String> keys = Arrays.stream(sshKeys.split(",")).collect(Collectors.toList()); | ||
|
||
for (String key : keys) { | ||
MatchResult matcher = regExp.exec(key); | ||
if (matcher == null) { | ||
int length = Math.min(key.length(), 30); | ||
textArea.setRawStatus(getTransl().sshKeyFormat(key.substring(0, length)+((length == 30) ? "..." : "")), ValidationState.ERROR); | ||
setResult(Result.INVALID_FORMAT); | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
|
||
textArea.setStatus(ValidationState.SUCCESS); | ||
return true; | ||
|
||
} | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
...z/metacentrum/perun/wui/registrar/widgets/items/validators/SshKeysTextFieldValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package cz.metacentrum.perun.wui.registrar.widgets.items.validators; | ||
|
||
import com.google.gwt.regexp.shared.MatchResult; | ||
import com.google.gwt.regexp.shared.RegExp; | ||
import cz.metacentrum.perun.wui.registrar.widgets.items.TextField; | ||
import org.gwtbootstrap3.client.ui.constants.ValidationState; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Pavel Zlámal <[email protected]> | ||
*/ | ||
public class SshKeysTextFieldValidator extends TextFieldValidator { | ||
|
||
RegExp regExp = RegExp.compile("^(" + | ||
"(ssh-(rsa|dss|ed25519)([email protected])?)|" + | ||
"(sk-(ssh-ed25519|ecdsa-sha2-nistp256)(-cert-v01)[email protected])|" + | ||
"(ecdsa-sha2-nistp(256|384|521)([email protected])?))" + | ||
" (([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?)( [^,\n]+)?$"); | ||
|
||
@Override | ||
public boolean validateLocal(TextField textField) { | ||
|
||
if (textField.isRequired() && isNullOrEmpty(textField.getValue())) { | ||
setResult(Result.EMPTY); | ||
textField.setRawStatus(getTransl().cantBeEmpty(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (!textField.getBox().isValid()) { | ||
setResult(Result.INVALID_FORMAT); | ||
textField.setStatus(getErrorMsgOrDefault(textField), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (textField.getValue() != null && textField.getValue().length() > textField.MAX_LENGTH) { | ||
setResult(Result.TOO_LONG); | ||
textField.setStatus(getTransl().tooLong(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (textField.getValue() != null && !textField.getValue().isEmpty()) { | ||
|
||
String sshKeys = textField.getValue(); | ||
|
||
if (sshKeys.contains(",,")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textField.setStatus(getTransl().tooMuchCommasTextField(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
if (sshKeys.contains(", ") || sshKeys.contains(" ,")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textField.setStatus(getTransl().sshKeyNoSpaceAroundCommasTextField(), ValidationState.ERROR); | ||
return false; | ||
} | ||
|
||
// FIXME - this doesn't make sense anymore, as we have multiple different SSH keys prefixes, which needs to be checked. | ||
/* | ||
if (sshKeys.indexOf("ssh-") != sshKeys.lastIndexOf("ssh-")) { | ||
// there are at least two keys | ||
if (!sshKeys.contains(",ssh-")) { | ||
setResult(Result.INVALID_FORMAT); | ||
textField.setStatus(getTransl().sshKeyMissingCommaDelimiterTextField(), ValidationState.ERROR); | ||
return false; | ||
} | ||
} | ||
*/ | ||
|
||
// normalize value just in case | ||
sshKeys = sshKeys.replaceAll("(,)+", ","); | ||
List<String> keys = Arrays.stream(sshKeys.split(",")).collect(Collectors.toList()); | ||
|
||
for (String key : keys) { | ||
MatchResult matcher = regExp.exec(key); | ||
if (matcher == null) { | ||
int length = Math.min(key.length(), 30); | ||
textField.setRawStatus(getTransl().sshKeyFormat(key.substring(0, length)+((length == 30) ? "..." : "")), ValidationState.ERROR); | ||
setResult(Result.INVALID_FORMAT); | ||
return false; | ||
} | ||
} | ||
|
||
} | ||
|
||
textField.setStatus(ValidationState.SUCCESS); | ||
return true; | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters