Skip to content

Commit e084a73

Browse files
committed
Make Include regexp stricter to avoid deleting user data
1 parent fda935f commit e084a73

File tree

2 files changed

+87
-5
lines changed

2 files changed

+87
-5
lines changed

cli/configssh.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ var (
5454
// Find the semantically correct include statement. Since the user can
5555
// modify their configuration as they see fit, there could be:
5656
// - Leading indentation (space, tab)
57-
// - Trailing indentation (space, tab), followed by e.g. a comment or
58-
// another file to Include (we don't want to support this, but
59-
// explicitly blocking it adds complexity)
60-
// - Select newline after Include statement for removal purposes
61-
sshCoderIncludedRe = regexp.MustCompile(`(?m)^[\t ]*((?i)Include) coder([\t ].*)?[\r]?[\n]?$`)
57+
// - Trailing indentation (space, tab)
58+
// - Select newline after Include statement for cleaner removal
59+
// In the following cases, we will not recognize the Include statement
60+
// and leave as-is (i.e. they're not supported):
61+
// - User adds another file to the Include statement
62+
// - User adds a comment on the same line as the Include statement
63+
sshCoderIncludedRe = regexp.MustCompile(`(?m)^[\t ]*((?i)Include) coder([\t ])?[\r]?[\n]?$`)
6264
)
6365

6466
// sshCoderConfigOptions represents options that can be stored and read

cli/configssh_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,86 @@ func TestConfigSSH_FileWriteAndOptionsFlow(t *testing.T) {
267267
{match: "Continue?", write: "yes"},
268268
},
269269
},
270+
{
271+
name: "Included file must be named exactly coder, otherwise leave as-is",
272+
writeConfig: writeConfig{
273+
ssh: strings.Join([]string{
274+
"Host test",
275+
" HostName test",
276+
"",
277+
"Include coders",
278+
"",
279+
}, "\n"),
280+
},
281+
wantConfig: wantConfig{
282+
ssh: strings.Join([]string{
283+
"Include coder",
284+
"",
285+
"Host test",
286+
" HostName test",
287+
"",
288+
"Include coders",
289+
"",
290+
}, "\n"),
291+
},
292+
matches: []match{
293+
{match: "Continue?", write: "yes"},
294+
},
295+
},
296+
{
297+
name: "Second file added, Include(s) left as-is, new one on top",
298+
writeConfig: writeConfig{
299+
ssh: strings.Join([]string{
300+
"Host test",
301+
" HostName test",
302+
"",
303+
"Include coder other",
304+
"Include other coder",
305+
"",
306+
}, "\n"),
307+
},
308+
wantConfig: wantConfig{
309+
ssh: strings.Join([]string{
310+
"Include coder",
311+
"",
312+
"Host test",
313+
" HostName test",
314+
"",
315+
"Include coder other",
316+
"Include other coder",
317+
"",
318+
}, "\n"),
319+
},
320+
matches: []match{
321+
{match: "Continue?", write: "yes"},
322+
},
323+
},
324+
{
325+
name: "Comment added, Include left as-is, new one on top",
326+
writeConfig: writeConfig{
327+
ssh: strings.Join([]string{
328+
"Host test",
329+
" HostName test",
330+
"",
331+
"Include coder # comment",
332+
"",
333+
}, "\n"),
334+
},
335+
wantConfig: wantConfig{
336+
ssh: strings.Join([]string{
337+
"Include coder",
338+
"",
339+
"Host test",
340+
" HostName test",
341+
"",
342+
"Include coder # comment",
343+
"",
344+
}, "\n"),
345+
},
346+
matches: []match{
347+
{match: "Continue?", write: "yes"},
348+
},
349+
},
270350
{
271351
name: "SSH Config does not need modification",
272352
writeConfig: writeConfig{

0 commit comments

Comments
 (0)