Skip to content

Commit 46065d3

Browse files
author
Alex Wilson
committed
#44 Performance issues parsing long SSH public keys
Reviewed by: Cody Peter Mello <[email protected]>
1 parent 4d01aed commit 46065d3

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

lib/formats/ssh.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ var PrivateKey = require('../private-key');
1414
var sshpriv = require('./ssh-private');
1515

1616
/*JSSTYLED*/
17-
var SSHKEY_RE = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/]+[=]*)([\n \t]+([^\n]+))?$/;
17+
var SSHKEY_RE = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/]+[=]*)([ \t]+([^ \t][^\n]*[\n]*)?)?$/;
1818
/*JSSTYLED*/
19-
var SSHKEY_RE2 = /^([a-z0-9-]+)[ \t]+([a-zA-Z0-9+\/ \t\n]+[=]*)(.*)$/;
19+
var SSHKEY_RE2 = /^([a-z0-9-]+)[ \t\n]+([a-zA-Z0-9+\/][a-zA-Z0-9+\/ \t\n=]*)([^a-zA-Z0-9+\/ \t\n=].*)?$/;
2020

2121
function read(buf, options) {
2222
if (typeof (buf) !== 'string') {
@@ -71,7 +71,7 @@ function read(buf, options) {
7171
* chars from the beginning up to this point in the the string.
7272
* Then offset in this and try to make up for missing = chars.
7373
*/
74-
var data = m[2] + m[3];
74+
var data = m[2] + (m[3] ? m[3] : '');
7575
var realOffset = Math.ceil(ret.consumed / 3) * 4;
7676
data = data.slice(0, realOffset - 2). /*JSSTYLED*/
7777
replace(/[^a-zA-Z0-9+\/=]/g, '') +

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "sshpk",
3-
"version": "1.13.1",
3+
"version": "1.13.2",
44
"description": "A library for finding and using SSH public keys",
55
"main": "lib/index.js",
66
"scripts": {

test/horrors.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,30 @@ test('line continuations, key from hell', function (t) {
8686
t.end();
8787
});
8888

89+
var KEY_NO_COMMENT = 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAA' +
90+
'IbmlzdHAyNTYAAABBBK9+hFGVZ9RT61pg8t7EGgkvduhPr/CBYfx+5rQFEROj8EjkoGIH2xy' +
91+
'pHOHBz0WikK5hYcwTM5YMvnNxuU0h4+c=';
92+
test('normal key, no comment', function (t) {
93+
var k = sshpk.parseKey(KEY_NO_COMMENT, 'ssh');
94+
t.strictEqual(k.type, 'ecdsa');
95+
t.strictEqual(k.fingerprint('sha256').toString(),
96+
'SHA256:Kyu0EMqH8fzfp9RXKJ6kmsk9qKGBqVRtlOuk6bXfCEU');
97+
t.strictEqual(k.comment, '(unnamed)');
98+
t.end();
99+
});
100+
101+
var KEY_COMMENT_EQ = 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAA' +
102+
'IbmlzdHAyNTYAAABBBK9+hFGVZ9RT61pg8t7EGgkvduhPr/CBYfx+5rQFEROj8EjkoGIH2xy' +
103+
'pHOHBz0WikK5hYcwTM5YMvnNxuU0h4+c= abc=def=a\n';
104+
test('comment contains =, trailing newline', function (t) {
105+
var k = sshpk.parseKey(KEY_COMMENT_EQ, 'ssh');
106+
t.strictEqual(k.type, 'ecdsa');
107+
t.strictEqual(k.fingerprint('sha256').toString(),
108+
'SHA256:Kyu0EMqH8fzfp9RXKJ6kmsk9qKGBqVRtlOuk6bXfCEU');
109+
t.strictEqual(k.comment, 'abc=def=a');
110+
t.end();
111+
});
112+
89113
var KEY_BREAK = 'ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzd' +
90114
'HAyNTYAAABBBK9+hFGVZ9RT61pg8t7\nEGgkvduhPr/CBYfx+5rQFEROj8EjkoGIH2xypHOH' +
91115
'Bz0WikK5hYcwTM5YMvnNxuU0h4+c=';

0 commit comments

Comments
 (0)