Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ide/dlight.nativeexecution.nb/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
# under the License.

is.autoload=true
javac.source=1.8
javac.release=11
javac.compilerargs=-Xlint -Xlint:-serial
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,22 @@
*/
package org.netbeans.modules.nativeexecution.ui;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.regex.Pattern;
import org.netbeans.modules.nativeexecution.api.util.Authentication;
import org.openide.util.Exceptions;

/**
*
* @author akrasny
*/
public class SSHKeyFileFilter implements FileFilter {

private static final Pattern p = Pattern.compile("-+ *BEGIN.*PRIVATE.*KEY *-+.*"); // NOI18N
private static final Charset cs = StandardCharsets.US_ASCII;
private static final Pattern pattern = Pattern.compile("-+ *BEGIN.*PRIVATE.*KEY *-+.*"); // NOI18N
private static final SSHKeyFileFilter instance = new SSHKeyFileFilter();

private SSHKeyFileFilter() {
Expand All @@ -63,28 +60,28 @@ public boolean accept(File file) {
return false;
}

FileInputStream fis = null;
byte[] buffer = new byte[30];

// fast sanity check
try {
fis = new FileInputStream(file);
fis.read(buffer, 0, 30);

Matcher m = p.matcher(new String(buffer, 0, 30, cs));
if (!m.matches()) {
// header contains the name of the tool so we don't know how long it is
// read until new line but with a limit and avoid buffering the key
int limit = 40;

try (InputStream is = new BufferedInputStream(Files.newInputStream(file.toPath()), limit)) {

StringBuilder head = new StringBuilder(limit);
int c;
for (int n = 0; n < limit; n++) {
c = is.read();
if (c == -1 || c == '\n' || c == '\r') {
break;
}
head.append((char) c);
}
if (!pattern.matcher(head).matches()) {
return false;
}
} catch (IOException ex) {
return false;
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
}

return Authentication.isValidSSHKeyFile(file.getAbsolutePath());
Expand Down
Loading