Skip to content

fix(cli/ssh): prevent reads/writes to stdin/stdout in stdio mode #12045

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2024

Conversation

mafredri
Copy link
Member

@mafredri mafredri commented Feb 7, 2024

This PR prevents writes to stdout in stdio mode (assuming no developer error that uses os.Stdout).

For absolute safety, we also swap stdin with an EOF reader that logs errors on attempts to read. We can't make reads panic (or similar) since there's a high chance that such an issue could slip unnoticed into a release and trigger at runtime.

Fixes #11530


The added test, without the fix, would catch the issue at two stages:

    ssh_test.go:400: monitorServerOutput: "Planning workspace..." (0x506c616e6e696e6720776f726b73706163652e2e2e)
    ssh_test.go:409:
        	Error Trace:	/home/coder/coder/cli/ssh_test.go:409
        	            				/home/coder/coder/cli/ssh_test.go:1479
        	            				/usr/local/go/src/runtime/asm_amd64.s:1650
        	Error:      	Not equal:
        	            	expected: "SSH-2.0-Go"
        	            	actual  : "Planning workspace..."

        	            	Diff:
        	            	--- Expected
        	            	+++ Actual
        	            	@@ -1 +1 @@
        	            	-SSH-2.0-Go
        	            	+Planning workspace...
        	Test:       	TestSSH/Stdio_StartStoppedWorkspace_NoStdinOrStdout
        	Messages:   	invalid header
    ssh_test.go:400: monitorServerOutput: "==> ⧗ Queued" (0x3d3d3e20e2a79720517565756564)

    [...]

    ssh_test.go:463:
        	Error Trace:	/home/coder/coder/cli/ssh_test.go:463
        	Error:      	Received unexpected error:
        	            	ssh: handshake failed: ssh: overflow reading version string
        	Test:       	TestSSH/Stdio_StartStoppedWorkspace_NoStdinOrStdout

@mafredri mafredri force-pushed the mafredri/fix-cli-ssh-stdio-output branch from 3793256 to 3be345d Compare February 7, 2024 12:10
@mafredri mafredri marked this pull request as ready for review February 7, 2024 12:18
@mafredri mafredri changed the title fix(cli/ssh): prevent stdin/stdout reads/writes in stdio mode fix(cli/ssh): prevent reads/writes to stdin/stdout in stdio mode Feb 7, 2024
@mafredri mafredri force-pushed the mafredri/fix-cli-ssh-stdio-output branch from 3be345d to dd1f7c4 Compare February 7, 2024 12:32
Copy link
Contributor

@spikecurtis spikecurtis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the commentary and naming could be improved with some inline suggestions, but LGTM.

cli/ssh_test.go Outdated

clientOutput, clientInput := io.Pipe()
serverOutput, serverInput := io.Pipe()
monitorServerOutput, monitorServerInput := io.Pipe()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's pretty confusing what's happening with these 3 pipes, so I think an ASCII diagram would go a long way.

Copy link
Member Author

@mafredri mafredri Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I neither know mermaid or how to turn it into ascii, so I pasted it in there 😅

flowchart LR
	A[ProxyCommand] --> B[captureProxyCommandStdoutW]
	B --> C[captureProxyCommandStdoutR]
	C --> VA[Validate output]
	C --> D[proxyCommandStdoutW]
	D --> E[proxyCommandStdoutR]
	E --> F[SSH Client]
Loading

cli/ssh_test.go Outdated
})

// Here we start a monitor for the input going to the server
// (i.e. client stdout) to ensure that the output is clean.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this comment is incorrect. This doesn't monitor data being sent to the server (to monitor that we'd need to be in the tailnet network path and we are not), but rather data being sent to the stdio client.

Underscores a need for a diagram about the pipes.

Copy link
Member Author

@mafredri mafredri Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stdio client is a representation of a raw network connection, so I don't consider the statement incorrect. Lossy, perhaps.

Addendum: By "client stdout" I'm referring to the SSH command. Using "client" terminology for "stdio client" would cause more confusion IMO. Hence I prefer referring to the "stdio client" as "the connection" instead.

Yeah so after I rewrote the comment I realized what you meant, and you're right, it was incorrect, mb. 😄

cli/ssh_test.go Outdated
// Ideally we would do further verification, but that would
// involve parsing the SSH protocol to look for output that
// doesn't belong. This at least ensures that no garbage is
// being sent to the server before trying to connect.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// being sent to the server before trying to connect.
// being sent to the client before trying to connect.

@mafredri mafredri merged commit e659957 into main Feb 8, 2024
@mafredri mafredri deleted the mafredri/fix-cli-ssh-stdio-output branch February 8, 2024 11:09
@github-actions github-actions bot locked and limited conversation to collaborators Feb 8, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SSH failed: kex_exchange_identification: banner line contains invalid characters
3 participants