Skip to content

Commit bcebbd4

Browse files
committed
move malformed block check earlier
1 parent 572a938 commit bcebbd4

File tree

2 files changed

+47
-8
lines changed

2 files changed

+47
-8
lines changed

src/sshConfig.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,44 @@ Host coder-vscode.dev.coder.com--*
249249
})
250250
})
251251

252+
it("throws an error if there is a missing end block", async () => {
253+
// The below config is missing an end block.
254+
// This is a malformed config and should throw an error.
255+
const existentSSHConfig = `Host beforeconfig
256+
HostName before.config.tld
257+
User before
258+
259+
# --- START CODER VSCODE dev.coder.com ---
260+
Host coder-vscode.dev.coder.com--*
261+
ConnectTimeout 0
262+
LogLevel ERROR
263+
ProxyCommand some-command-here
264+
StrictHostKeyChecking no
265+
UserKnownHostsFile /dev/null
266+
267+
Host afterconfig
268+
HostName after.config.tld
269+
User after`
270+
271+
const sshConfig = new SSHConfig(sshFilePath, mockFileSystem)
272+
mockFileSystem.readFile.mockResolvedValueOnce(existentSSHConfig)
273+
await sshConfig.load()
274+
275+
// When we try to update the config, it should throw an error.
276+
await expect(
277+
sshConfig.update("dev.coder.com", {
278+
Host: "coder-vscode.dev.coder.com--*",
279+
ProxyCommand: "some-command-here",
280+
ConnectTimeout: "0",
281+
StrictHostKeyChecking: "no",
282+
UserKnownHostsFile: "/dev/null",
283+
LogLevel: "ERROR",
284+
}),
285+
).rejects.toThrow(
286+
`Malformed config: Unterminated START CODER VSCODE dev.coder.com block: Each START block must have an END block.`,
287+
)
288+
})
289+
252290
it("throws an error if there is a mismatched start and end block count", async () => {
253291
// The below config contains two start blocks and one end block.
254292
// This is a malformed config and should throw an error.

src/sshConfig.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,27 +126,28 @@ export class SSHConfig {
126126
const raw = this.getRaw()
127127
const startBlock = this.startBlockComment(label)
128128
const endBlock = this.endBlockComment(label)
129+
129130
const startBlockCount = countSubstring(startBlock, raw)
130131
const endBlockCount = countSubstring(endBlock, raw)
131-
const startBlockIndex = raw.indexOf(startBlock)
132-
const endBlockIndex = raw.indexOf(endBlock)
133-
const hasBlock = startBlockIndex > -1 && endBlockIndex > -1
134-
135-
if (!hasBlock) {
136-
return
137-
}
138-
139132
if (startBlockCount !== endBlockCount) {
140133
throw new SSHConfigBadFormat(
141134
`Malformed config: Unterminated START CODER VSCODE ${label ? label + " " : ""}block: Each START block must have an END block.`,
142135
)
143136
}
137+
144138
if (startBlockCount > 1 || endBlockCount > 1) {
145139
throw new SSHConfigBadFormat(
146140
`Malformed config: ssh config has ${startBlockCount} START CODER VSCODE ${label ? label + " " : ""}sections, please remove all but one.`,
147141
)
148142
}
149143

144+
const startBlockIndex = raw.indexOf(startBlock)
145+
const endBlockIndex = raw.indexOf(endBlock)
146+
const hasBlock = startBlockIndex > -1 && endBlockIndex > -1
147+
if (!hasBlock) {
148+
return
149+
}
150+
150151
if (startBlockIndex === -1) {
151152
throw new SSHConfigBadFormat("Start block not found")
152153
}

0 commit comments

Comments
 (0)