Skip to content

Commit 28941d1

Browse files
committed
fix(site): preserve SSH hostnames that match legacy prefixes
1 parent 11a37b5 commit 28941d1

3 files changed

Lines changed: 122 additions & 2 deletions

File tree

‎site/src/modules/resources/SSHButton/SSHButton.stories.tsx‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,35 @@ export const OpenedSingleAgent: Story = {
6565
args: { workspace: MockWorkspace },
6666
};
6767

68+
const mockWorkspaceWithPrefixCollision: Workspace = {
69+
...MockWorkspace,
70+
name: "coder",
71+
latest_build: {
72+
...MockWorkspace.latest_build,
73+
workspace_name: "coder",
74+
},
75+
};
76+
77+
const mockDeploymentSSHWithPrefixCollision = {
78+
...MockDeploymentSSH,
79+
hostname_prefix: "coder.",
80+
hostname_suffix: "dev",
81+
};
82+
83+
/** The legacy prefix must not capture a workspace's suffix-based hostname. */
84+
export const OpenedWithLegacyPrefixCollision: Story = {
85+
...OpenedSingleAgent,
86+
args: { workspace: mockWorkspaceWithPrefixCollision },
87+
parameters: {
88+
queries: [
89+
{
90+
key: deploymentSSHConfigQueryKey,
91+
data: mockDeploymentSSHWithPrefixCollision,
92+
},
93+
],
94+
},
95+
};
96+
6897
/** Shared SSH hostnames must keep the agent and workspace owner explicit. */
6998
export const OpenedSharedSingleAgent: Story = {
7099
...OpenedSingleAgent,

‎site/src/modules/resources/SSHButton/SSHButton.test.tsx‎

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,88 @@ describe.each([
148148
await waitFor(() => expect(writeText).toHaveBeenCalledWith(command));
149149
});
150150
});
151+
152+
describe("legacy hostname prefix", () => {
153+
it.each([
154+
{
155+
name: "default prefix collision with a custom suffix",
156+
workspaceName: "coder",
157+
hostnamePrefix: "coder.",
158+
command: "ssh a-workspace-agent.coder.TestUser.dev",
159+
},
160+
{
161+
name: "custom prefix collision",
162+
workspaceName: "work-lab",
163+
hostnamePrefix: "work-",
164+
command: "ssh a-workspace-agent.work-lab.TestUser.dev",
165+
},
166+
{
167+
name: "case-insensitive prefix collision",
168+
workspaceName: "coder",
169+
hostnamePrefix: "CoDeR.",
170+
command: "ssh a-workspace-agent.coder.TestUser.dev",
171+
},
172+
{
173+
name: "case-insensitive hostname collision",
174+
workspaceName: "CoDeR",
175+
hostnamePrefix: "coder.",
176+
command: "ssh a-workspace-agent.CoDeR.TestUser.dev",
177+
},
178+
{
179+
name: "prefix spanning the workspace and suffix",
180+
workspaceName: "coder",
181+
hostnamePrefix: "coder.d",
182+
command: "ssh a-workspace-agent.coder.TestUser.dev",
183+
},
184+
{
185+
name: "noncolliding prefix",
186+
workspaceName: "mycoder",
187+
hostnamePrefix: "coder.",
188+
command: "ssh mycoder.dev",
189+
},
190+
{
191+
name: "disabled prefix",
192+
workspaceName: "coder",
193+
hostnamePrefix: "",
194+
command: "ssh coder.dev",
195+
},
196+
])(
197+
"copies the safe command for $name",
198+
async ({ workspaceName, hostnamePrefix, command }) => {
199+
const user = userEvent.setup();
200+
const writeText = vi
201+
.spyOn(navigator.clipboard, "writeText")
202+
.mockResolvedValue();
203+
const mockDeploymentSSH = {
204+
...MockDeploymentSSH,
205+
hostname_prefix: hostnamePrefix,
206+
hostname_suffix: "dev",
207+
};
208+
vi.spyOn(API, "getDeploymentSSHConfig").mockResolvedValue(
209+
mockDeploymentSSH,
210+
);
211+
const mockWorkspace: Workspace = {
212+
...MockWorkspace,
213+
name: workspaceName,
214+
latest_build: {
215+
...MockWorkspace.latest_build,
216+
workspace_name: workspaceName,
217+
},
218+
};
219+
renderWithAuth(
220+
<AgentSSHButton
221+
workspace={mockWorkspace}
222+
agentName={MockWorkspaceAgent.name}
223+
/>,
224+
);
225+
await user.click(
226+
await screen.findByRole("button", { name: "Connect via SSH" }),
227+
);
228+
const copyButtons = await screen.findAllByRole("button", {
229+
name: "Copy code",
230+
});
231+
await user.click(copyButtons[copyButtons.length - 1]);
232+
await waitFor(() => expect(writeText).toHaveBeenCalledWith(command));
233+
},
234+
);
235+
});

‎site/src/modules/resources/SSHButton/SSHButton.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,16 @@ export const AgentSSHButton: React.FC<AgentSSHButtonProps> = ({
3737
let connectCommand: string | undefined;
3838
if (!isLoading) {
3939
if (sshSuffix) {
40+
const shortHostname = `${workspace.name}.${sshSuffix}`;
41+
const hostnamePrefix = data?.hostname_prefix;
42+
// SSH selects legacy prefix blocks before suffix blocks.
43+
const matchesLegacyPrefix = hostnamePrefix
44+
? shortHostname.toLowerCase().startsWith(hostnamePrefix.toLowerCase())
45+
: false;
4046
// Shared workspaces need the full hostname; SSH has no workspace.owner form.
4147
connectCommand =
42-
isSingleAgent && user.id === workspace.owner_id
43-
? `ssh ${workspace.name}.${sshSuffix}`
48+
isSingleAgent && user.id === workspace.owner_id && !matchesLegacyPrefix
49+
? `ssh ${shortHostname}`
4450
: `ssh ${agentName}.${workspace.name}.${workspace.owner_name}.${sshSuffix}`;
4551
} else {
4652
// The CLI needs no SSH config, so it remains usable without a suffix.

0 commit comments

Comments
 (0)