Skip to content
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

Check if binary exists before passing it to node-pty #44440

Merged
merged 3 commits into from
Jul 19, 2024
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
26 changes: 26 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions web/packages/teleterm/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,14 @@
"homepage": "https://goteleport.com",
"dependencies": {
"@grpc/grpc-js": "1.10.10",
"@types/which": "^3.0.4",
"node-forge": "^1.3.1",
"node-pty": "1.1.0-beta14",
"ring-buffer-ts": "^1.2.0",
"split2": "4.2.0",
"strip-ansi": "^7.1.0",
"tar-fs": "^3.0.6",
"which": "^4.0.0",
Copy link
Contributor

Choose a reason for hiding this comment

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

I know it's a small package, but maybe we could add @types/which?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done, mostly to check out the flow of adding a new package with pnpm. ;)

"winston": "^3.13.0"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,13 @@ export class PtyEventsStreamHandler {
})
);
});
this.ptyProcess.start(event.columns, event.rows);
this.logger.info(`stream has started`);
// PtyProcess.prototype.start always returns a fulfilled promise. If an error is caught during
// start, it's reported through PtyProcess.prototype.onStartError. Similarly, the information
// about a successful start is also conveyed through an emitted event rather than the method
// returning with no error. Hence why we can ignore what this promise returns.
void this.ptyProcess.start(event.columns, event.rows).then(() => {
Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using the syntax recommended by no-floating-promises to explicitly mark a promise that we ignore. https://typescript-eslint.io/rules/no-floating-promises/#ignorevoid

this.logger.info(`stream has started`);
});
}

private handleDataEvent(event: PtyEventData): void {
Expand Down
54 changes: 54 additions & 0 deletions web/packages/teleterm/src/sharedProcess/ptyHost/ptyProcess.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @jest-environment node
*/
/**
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import crypto from 'node:crypto';

import Logger, { NullService } from 'teleterm/logger';

import { PtyProcess } from './ptyProcess';

beforeAll(() => {
Logger.init(new NullService());
});

describe('PtyProcess', () => {
describe('start', () => {
it('emits a start error when attempting to execute a nonexistent command', async () => {
const path = `nonexistent-executable-${crypto.randomUUID()}`;
const pty = new PtyProcess({
path,
args: [],
env: { PATH: '/foo/bar' },
ptyId: '1234',
});

const startErrorCb = jest.fn();

pty.onStartError(startErrorCb);

await pty.start(80, 20);

expect(startErrorCb).toHaveBeenCalledWith(
expect.stringContaining(`not found: ${path}`)
);
});
});
});
26 changes: 18 additions & 8 deletions web/packages/teleterm/src/sharedProcess/ptyHost/ptyProcess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,22 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { readlink } from 'fs';

import { exec } from 'child_process';

import { promisify } from 'util';

import { EventEmitter } from 'events';
import { readlink } from 'node:fs';
import { exec } from 'node:child_process';
import { promisify } from 'node:util';
import { EventEmitter } from 'node:events';

import * as nodePTY from 'node-pty';
import which from 'which';

import Logger from 'teleterm/logger';

import { PtyProcessOptions, IPtyProcess } from './types';

type Status = 'open' | 'not_initialized' | 'terminated';

const pathEnvVar = process.platform === 'win32' ? 'Path' : 'PATH';

export class PtyProcess extends EventEmitter implements IPtyProcess {
private _buffered = true;
private _attachedBufferTimer;
Expand All @@ -54,8 +54,18 @@ export class PtyProcess extends EventEmitter implements IPtyProcess {
return this.options.ptyId;
}

start(cols: number, rows: number) {
/**
* start spawns a new PTY with the arguments given through the constructor.
* It emits TermEventEnum.StartError on error. start itself always returns a fulfilled promise.
*/
async start(cols: number, rows: number) {
try {
// which throws an error if the argument is not found in path.
// TODO(ravicious): Remove the manual check for the existence of the executable after node-pty
// makes its behavior consistent across platforms.
// https://github.com/microsoft/node-pty/issues/689
await which(this.options.path, { path: this.options.env[pathEnvVar] });

// TODO(ravicious): Set argv0 when node-pty adds support for it.
// https://github.com/microsoft/node-pty/issues/472
this._process = nodePTY.spawn(this.options.path, this.options.args, {
Expand Down
Loading