Keep step-up elevation for the signed-in session and stop gating terminals
The proxy forwards an HMAC-derived session id (X-Pipelines-Session) on /connect and POST /files; the server trusts it only on token-authenticated requests and keys elevation by it, so reconnects stay elevated up to a 24 h cap. Terminals rely on their su -l login. The app no longer forgets the elevation on disconnect and asks again only on a fresh refusal.
This commit is contained in:
@@ -82,7 +82,7 @@ void main() {
|
||||
),
|
||||
);
|
||||
await expectLater(
|
||||
gateway.call('terminal_open'),
|
||||
gateway.call('chat_shell_run'),
|
||||
throwsA(
|
||||
isA<ProtocolError>().having(
|
||||
(e) => e.code,
|
||||
@@ -91,7 +91,7 @@ void main() {
|
||||
),
|
||||
),
|
||||
);
|
||||
expect(machine.calls, ['terminal_open']);
|
||||
expect(machine.calls, ['chat_shell_run']);
|
||||
});
|
||||
|
||||
test('other errors pass through untouched and never prompt', () async {
|
||||
@@ -161,35 +161,33 @@ void main() {
|
||||
expect(elevation.token, 'tok-1');
|
||||
});
|
||||
|
||||
test('the token expires with the window and on disconnect', () async {
|
||||
final machine = FakeMachine();
|
||||
var now = DateTime(2026, 11, 10, 12);
|
||||
final states = StreamController<ConnectionState>.broadcast();
|
||||
final elevation = MachineElevation(
|
||||
name: 'm',
|
||||
gateway: machine,
|
||||
states: states.stream,
|
||||
clock: () => now,
|
||||
);
|
||||
await elevation.submit('right');
|
||||
now = now.add(const Duration(minutes: 14));
|
||||
expect(elevation.token, 'tok-1');
|
||||
now = now.add(const Duration(minutes: 2));
|
||||
expect(elevation.token, isNull);
|
||||
now = now.subtract(const Duration(minutes: 16));
|
||||
await elevation.submit('right');
|
||||
states.add(const ConnectionState(ConnectionKind.disconnected));
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
expect(elevation.token, isNull);
|
||||
await elevation.dispose();
|
||||
await states.close();
|
||||
});
|
||||
test(
|
||||
'the token expires with the window; only a refusal forgets it',
|
||||
() async {
|
||||
final machine = FakeMachine();
|
||||
var now = DateTime(2026, 11, 10, 12);
|
||||
final elevation = MachineElevation(
|
||||
name: 'm',
|
||||
gateway: machine,
|
||||
clock: () => now,
|
||||
);
|
||||
await elevation.submit('right');
|
||||
now = now.add(const Duration(minutes: 14));
|
||||
expect(elevation.token, 'tok-1');
|
||||
now = now.add(const Duration(minutes: 2));
|
||||
expect(elevation.token, isNull);
|
||||
await elevation.submit('right');
|
||||
expect(elevation.token, 'tok-1');
|
||||
elevation.clear();
|
||||
expect(elevation.token, isNull);
|
||||
},
|
||||
);
|
||||
|
||||
test('the client carries the server error code', () async {
|
||||
final channel = FakeChannel()..readyCompleter.complete();
|
||||
final client = PipelinesClient(channelFactory: (_) => channel);
|
||||
await client.connect(Uri.parse('ws://example.test'));
|
||||
final call = client.call('terminal_open');
|
||||
final call = client.call('chat_shell_run');
|
||||
await Future<void>.delayed(Duration.zero);
|
||||
final id = (jsonDecode(channel.outgoing.single as String) as Map)['id'];
|
||||
channel.incoming.add(
|
||||
|
||||
@@ -0,0 +1,135 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/servers/elevation/elevating_gateway.dart';
|
||||
import 'package:pipelines_app/core/servers/elevation/machine_elevation.dart';
|
||||
import 'package:pipelines_app/core/transport/pipelines_client.dart';
|
||||
|
||||
import '../../transport/pipelines_client_test.dart' show FakeChannel, FakeSink;
|
||||
|
||||
/// A machine whose elevation belongs to the signed-in web session, as the
|
||||
/// server keeps it: every connection it hands out shares [elevated].
|
||||
class SessionServer {
|
||||
bool elevated = false;
|
||||
final channels = <ScriptedChannel>[];
|
||||
final requests = <String>[];
|
||||
|
||||
ScriptedChannel connect() {
|
||||
final channel = ScriptedChannel(this)..readyCompleter.complete();
|
||||
channels.add(channel);
|
||||
return channel;
|
||||
}
|
||||
|
||||
JsonReply answer(Map<String, dynamic> request) {
|
||||
final type = request['type'] as String;
|
||||
requests.add(type);
|
||||
if (type == 'elevate') {
|
||||
elevated = true;
|
||||
return {
|
||||
'type': 'elevated',
|
||||
'elevation': {'seconds': 86400, 'token': 'tok'},
|
||||
};
|
||||
}
|
||||
if (type == 'chat_shell_run' && !elevated) {
|
||||
return {'type': 'error', 'code': elevationRequiredCode, 'error': 'no'};
|
||||
}
|
||||
return {'type': type};
|
||||
}
|
||||
}
|
||||
|
||||
typedef JsonReply = Map<String, dynamic>;
|
||||
|
||||
class ScriptedChannel extends FakeChannel {
|
||||
ScriptedChannel(this.server);
|
||||
final SessionServer server;
|
||||
|
||||
@override
|
||||
FakeSink get sink => ScriptedSink(this);
|
||||
}
|
||||
|
||||
class ScriptedSink extends FakeSink {
|
||||
ScriptedSink(this.channel) : super(channel.outgoing, channel.incoming);
|
||||
final ScriptedChannel channel;
|
||||
|
||||
@override
|
||||
void add(dynamic event) {
|
||||
super.add(event);
|
||||
final request = jsonDecode(event as String) as Map<String, dynamic>;
|
||||
final reply = channel.server.answer(request)..['id'] = request['id'];
|
||||
scheduleMicrotask(() => channel.incoming.add(jsonEncode(reply)));
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> connected(PipelinesClient client) async {
|
||||
if (client.state.kind == ConnectionKind.connected) return;
|
||||
await client.states.firstWhere((s) => s.kind == ConnectionKind.connected);
|
||||
}
|
||||
|
||||
void main() {
|
||||
test('a reconnect re-uses the session elevation without asking', () async {
|
||||
final server = SessionServer();
|
||||
final client = PipelinesClient(
|
||||
channelFactory: (_) => server.connect(),
|
||||
backoffBase: const Duration(milliseconds: 1),
|
||||
);
|
||||
await client.connect(Uri.parse('ws://machine.test'));
|
||||
var prompts = 0;
|
||||
final elevation = MachineElevation(
|
||||
name: 'machine',
|
||||
gateway: client,
|
||||
prompt: (m) async {
|
||||
prompts++;
|
||||
return await m.submit('right') == null;
|
||||
},
|
||||
);
|
||||
final gateway = ElevatingGateway(client, elevation);
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect(prompts, 1);
|
||||
// The connection drops; the client reconnects on a new socket.
|
||||
await server.channels.single.incoming.close();
|
||||
await connected(client);
|
||||
expect(server.channels, hasLength(2));
|
||||
expect(elevation.token, 'tok', reason: 'kept across the reconnect');
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect((await gateway.call('chat_shell_run'))['type'], 'chat_shell_run');
|
||||
expect(prompts, 1);
|
||||
expect(server.requests.where((t) => t == 'elevate'), hasLength(1));
|
||||
await client.dispose();
|
||||
});
|
||||
|
||||
test(
|
||||
'one prompt per run per machine; a fresh refusal asks once more',
|
||||
() async {
|
||||
final server = SessionServer();
|
||||
final client = PipelinesClient(channelFactory: (_) => server.connect());
|
||||
await client.connect(Uri.parse('ws://machine.test'));
|
||||
var prompts = 0;
|
||||
final gateway = ElevatingGateway(
|
||||
client,
|
||||
MachineElevation(
|
||||
name: 'machine',
|
||||
gateway: client,
|
||||
prompt: (m) async {
|
||||
prompts++;
|
||||
return await m.submit('right') == null;
|
||||
},
|
||||
),
|
||||
);
|
||||
for (var i = 0; i < 5; i++) {
|
||||
await gateway.call('chat_shell_run');
|
||||
}
|
||||
expect(prompts, 1);
|
||||
// The session ended on the server (expired, signed out elsewhere).
|
||||
server.elevated = false;
|
||||
for (var i = 0; i < 3; i++) {
|
||||
await gateway.call('chat_shell_run');
|
||||
}
|
||||
expect(prompts, 2);
|
||||
// Terminals never ask: the server does not gate them.
|
||||
expect((await gateway.call('terminal_open'))['type'], 'terminal_open');
|
||||
expect(prompts, 2);
|
||||
await client.dispose();
|
||||
},
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user