Skip to content

Commit 6eeef0e

Browse files
authored
Merge pull request #1281 from tgauth/update-ssh-server-grammar
update sshd config grammar
2 parents e32b149 + 466c3d7 commit 6eeef0e

File tree

4 files changed

+105
-42
lines changed

4 files changed

+105
-42
lines changed

grammars/tree-sitter-ssh-server-config/grammar.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
const PREC = {
77
MATCH: 2,
8-
OPERATOR: 1,
8+
OPERATOR: 1
99
}
1010

1111
module.exports = grammar({
@@ -14,10 +14,10 @@ module.exports = grammar({
1414
extras: $ => [' ', '\t', '\r'],
1515

1616
rules: {
17-
server_config: $ => seq(repeat(choice($.empty_line, $.comment, $.keyword)), repeat($.match)),
17+
server_config: $ => seq(repeat(choice($._empty_line, $.comment, $.keyword)), repeat($.match)),
1818

1919
// check for an empty line that is just a /n character
20-
empty_line: $ => '\n',
20+
_empty_line: $ => '\n',
2121
comment: $ => /#.*\n/,
2222

2323
keyword: $ => seq(
@@ -30,20 +30,31 @@ module.exports = grammar({
3030

3131
match: $ => seq(
3232
token(prec(PREC.MATCH, /match/i)),
33-
field('criteria', $.keyword),
33+
seq(repeat1($.criteria), $._empty_line),
3434
repeat1(choice($.comment, $.keyword)),
3535
),
3636

37-
arguments: $ => repeat1(choice($.boolean, $.number, $._quotedString, $._commaSeparatedString)),
37+
criteria: $ => seq(
38+
field('criteria', $.alpha),
39+
choice(seq(/[ \t]/, optional('=')), '='),
40+
field('argument', $._argument)
41+
),
3842

43+
_argument: $ => choice($.boolean, $.number, $.string, $._commaSeparatedString, $._doublequotedString, $._singlequotedString),
44+
arguments: $ => repeat1($._argument),
45+
46+
alpha: $ => /[a-zA-Z]+/i,
3947
alphanumeric: $ => /[a-zA-Z0-9]+/i,
4048
boolean: $ => choice('yes', 'no'),
4149
number: $ => /\d+/,
4250
operator: $ => token(prec(PREC.OPERATOR, /[-+\^]/)),
43-
string: $ => /[^\r\n,"]+/,
51+
string: $ => /[^\r\n,"'\s]+/, /* cannot contain spaces */
52+
53+
_quotedString: $ => /[^\r\n,"']+/, /* can contain spaces */
54+
_doublequotedString: $ => seq('"', alias($._quotedString, $.string), repeat(seq(',', alias($._quotedString, $.string))), '"'),
55+
_singlequotedString: $ => seq('\'', alias($._quotedString, $.string), repeat(seq(',', alias($._quotedString, $.string))), '\''),
4456

45-
_commaSeparatedString: $ => seq($.string, repeat(seq(',', $.string))),
46-
_quotedString: $ => seq('\"', $.string, '\"'),
57+
_commaSeparatedString: $ => prec(1, seq($.string, repeat1(seq(',', $.string))))
4758
}
4859

4960
});

grammars/tree-sitter-ssh-server-config/test/corpus/invalid_expressions.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ AuthorizedKeysFile"ARG"
88
(server_config
99
(ERROR
1010
(alphanumeric)
11-
(UNEXPECTED 'A'))
12-
(empty_line))
11+
(string)))
1312
=====
1413
missing argument after keyword
1514
=====
@@ -19,5 +18,4 @@ AuthorizedKeysFile
1918

2019
(server_config
2120
(ERROR
22-
(alphanumeric))
23-
(empty_line))
21+
(alphanumeric)))

grammars/tree-sitter-ssh-server-config/test/corpus/valid_expressions.txt

Lines changed: 80 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,9 @@ authorizedkeysfile "path to authorized keys file"
2929

3030
(server_config
3131
(match
32-
(keyword
33-
(alphanumeric)
34-
(arguments
35-
(string)))
32+
(criteria
33+
(alpha)
34+
(string))
3635
(keyword
3736
(alphanumeric)
3837
(arguments
@@ -52,10 +51,9 @@ authorizedkeysfile "path to authorized keys file"
5251
(arguments
5352
(boolean)))
5453
(match
55-
(keyword
56-
(alphanumeric)
57-
(arguments
58-
(string)))
54+
(criteria
55+
(alpha)
56+
(string))
5957
(keyword
6058
(alphanumeric)
6159
(arguments
@@ -229,19 +227,17 @@ passwordauthentication yes
229227

230228
(server_config
231229
(match
232-
(keyword
233-
(alphanumeric)
234-
(arguments
235-
(string)))
230+
(criteria
231+
(alpha)
232+
(string))
236233
(keyword
237234
(alphanumeric)
238235
(arguments
239236
(boolean))))
240237
(match
241-
(keyword
242-
(alphanumeric)
243-
(arguments
244-
(string)))
238+
(criteria
239+
(alpha)
240+
(string))
245241
(keyword
246242
(alphanumeric)
247243
(arguments
@@ -281,6 +277,11 @@ Subsystem sftp sftp-server.exe -f LOCAL0 -l DEBUG3
281277
(keyword
282278
(alphanumeric)
283279
(arguments
280+
(string)
281+
(string)
282+
(string)
283+
(string)
284+
(string)
284285
(string))))
285286
=====
286287
parse mini config
@@ -304,10 +305,9 @@ passwordauthentication yes
304305
(arguments
305306
(string)))
306307
(match
307-
(keyword
308-
(alphanumeric)
309-
(arguments
310-
(string)))
308+
(criteria
309+
(alpha)
310+
(string))
311311
(keyword
312312
(alphanumeric)
313313
(arguments
@@ -318,10 +318,9 @@ passwordauthentication yes
318318
(string)
319319
(string))))
320320
(match
321-
(keyword
322-
(alphanumeric)
323-
(arguments
324-
(string)))
321+
(criteria
322+
(alpha)
323+
(string))
325324
(keyword
326325
(alphanumeric)
327326
(arguments
@@ -354,8 +353,7 @@ parse empty line
354353

355354

356355
---
357-
(server_config
358-
(empty_line))
356+
(server_config)
359357
====
360358
parse repeatable keyword
361359
====
@@ -367,4 +365,59 @@ allowgroups administrators "openssh users"
367365
(alphanumeric)
368366
(arguments
369367
(string)
370-
(string))))
368+
(string))))
369+
====
370+
parse repeatable strings without quotes
371+
====
372+
allowgroups groupOne groupTwo
373+
374+
---
375+
(server_config
376+
(keyword
377+
(alphanumeric)
378+
(arguments
379+
(string)
380+
(string))))
381+
====
382+
parse comma separated string with quotes
383+
====
384+
385+
386+
---
387+
(server_config
388+
(keyword
389+
(alphanumeric)
390+
(arguments
391+
(string)
392+
(string))))
393+
====
394+
parse single-quoted argument
395+
====
396+
allowgroups 'openssh users'
397+
398+
---
399+
(server_config
400+
(keyword
401+
(alphanumeric)
402+
(arguments
403+
(string))))
404+
====
405+
parse multiple match criteria
406+
====
407+
match user User1,User2 Address 192.0.2.0/24
408+
passwordauthentication no
409+
410+
---
411+
(server_config
412+
(match
413+
(criteria
414+
(alpha)
415+
(string)
416+
(string))
417+
(criteria
418+
(alpha)
419+
(string))
420+
(keyword
421+
(alphanumeric)
422+
(arguments
423+
(boolean)))))

resources/sshdconfig/src/metadata.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4-
// keywords that can have multiple argments per line but cannot be repeated over multiple lines,
4+
// keywords that can have multiple arguments per line but cannot be repeated over multiple lines,
55
// as subsequent entries are ignored, should be represented as arrays
6-
pub const MULTI_ARG_KEYWORDS: [&str; 16] = [
6+
pub const MULTI_ARG_KEYWORDS: [&str; 17] = [
77
"authenticationmethods",
88
"authorizedkeysfile",
99
"casignaturealgorithms",
@@ -19,7 +19,8 @@ pub const MULTI_ARG_KEYWORDS: [&str; 16] = [
1919
"permituserenvironment",
2020
"persourcepenalties",
2121
"persourcepenaltyexemptlist",
22-
"pubkeyacceptedalgorithms"
22+
"pubkeyacceptedalgorithms",
23+
"rekeylimit" // first arg is bytes, second arg (optional) is amount of time
2324
];
2425

2526
// keywords that can be repeated over multiple lines and should be represented as arrays.

0 commit comments

Comments
 (0)