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
8 changes: 7 additions & 1 deletion packages/core/src/awsService/sagemaker/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,13 @@ export async function removeKnownHost(hostname: string): Promise<void> {
throw ToolkitError.chain(err, 'Failed to read known_hosts file')
}

const updatedLines = lines.filter((line) => !line.split(' ')[0].split(',').includes(hostname))
const updatedLines = lines.filter((line) => {
const entryHostname = line.split(' ')[0].split(',')
// Hostnames in the known_hosts file seem to be always lowercase, but keeping the case-sensitive check just in
// case. Originally we were only doing the case-sensitive check which caused users to get a host
// identification error when reconnecting to a Space after it was restarted.
return !entryHostname.includes(hostname) && !entryHostname.includes(hostname.toLowerCase())
})

if (updatedLines.length !== lines.length) {
try {
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/test/awsService/sagemaker/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,28 @@ describe('SageMaker Model', () => {
assertLogsContain(`Removed '${hostname}' from known_hosts`, false, 'debug')
})

it('removes case-sensitive hostname when entry in known_hosts is lowercase', async function () {
const mixedCaseHostname = 'Test.Host.Com'

sandbox.stub(fs, 'existsFile').resolves(true)

const inputContent = `test.host.com ssh-rsa AAAA\nsome.other.com ssh-rsa BBBB`
const expectedOutput = `some.other.com ssh-rsa BBBB`

sandbox.stub(fs, 'readFileText').resolves(inputContent)
const writeStub = sandbox.stub(fs, 'writeFile').resolves()

await removeKnownHost(mixedCaseHostname)

sinon.assert.calledWith(
writeStub,
path.join(os.homedir(), '.ssh', 'known_hosts'),
sinon.match((value: string) => value.trim() === expectedOutput),
{ atomic: true }
)
assertLogsContain(`Removed '${mixedCaseHostname}' from known_hosts`, false, 'debug')
})

it('handles hostname in comma-separated list', async function () {
sandbox.stub(fs, 'existsFile').resolves(true)

Expand Down
Loading