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

fix: Close channel correctly when receiving SSH_Message_Channel_Close #117

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion lib/src/ssh_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:dartssh2/src/message/msg_userauth.dart';
import 'package:dartssh2/src/ssh_message.dart';
import 'package:dartssh2/src/socket/ssh_socket.dart';
import 'package:dartssh2/src/ssh_userauth.dart';
import 'package:meta/meta.dart';

/// https://datatracker.ietf.org/doc/html/rfc4252#section-8
typedef SSHPasswordRequestHandler = FutureOr<String?> Function();
Expand Down Expand Up @@ -486,6 +487,10 @@ class SSHClient {
}
}

/// Handles a raw SSH packet. This method is only exposed for testing purposes.
@visibleForTesting
void handlePacket(Uint8List packet) => _handlePacket(packet);

void _sendMessage(SSHMessage message) {
printTrace?.call('-> $socket: $message');
_transport.sendPacket(message.encode());
Expand Down Expand Up @@ -792,7 +797,7 @@ class SSHClient {
printTrace?.call('<- $socket: $message');
final channel = _channels[message.recipientChannel];
if (channel != null) {
channel.close();
channel.handleMessage(message);
_channels.remove(message.recipientChannel);
_channelIdAllocator.release(message.recipientChannel);
}
Expand Down
30 changes: 30 additions & 0 deletions test/src/channel/ssh_channel_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import 'package:dartssh2/dartssh2.dart';
import 'package:test/test.dart';

import '../../test_utils.dart';

void main() {
group('SSHChannel', () {
late SSHClient client;
late SSHSession session;

setUp(() async {
client = await getTestClient();
await client.authenticated;
session = await client.shell();
});

tearDown(() {
client.close();
});

test('stdout stream handles remote channel close correctly', () async {
final drainFuture = session.stdout.drain<void>();

final closeMessage = createChannelCloseMessage(0);
client.handlePacket(closeMessage);

await drainFuture;
}, timeout: const Timeout(Duration(seconds: 1)));
});
}
10 changes: 10 additions & 0 deletions test/test_utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:dartssh2/dartssh2.dart';
import 'package:dartssh2/src/message/msg_channel.dart';

/// A honeypot that accepts all passwords and public-keys
Future<SSHClient> getHoneypotClient() async {
Expand Down Expand Up @@ -40,3 +42,11 @@ Future<List<SSHKeyPair>> getTestKeyPairs() async {
String fixture(String path) {
return File('test/fixtures/$path').readAsStringSync();
}

/// Create a [SSH_Message_Channel_Close] message.
Uint8List createChannelCloseMessage(int recipientChannel) {
final message = SSH_Message_Channel_Close(
recipientChannel: recipientChannel,
);
return message.encode();
}