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:
@@ -37,7 +37,6 @@ class AppSession extends ChangeNotifier {
|
||||
_elevation = MachineElevation(
|
||||
name: endpoint?.host.isNotEmpty == true ? endpoint!.host : 'this server',
|
||||
gateway: _client,
|
||||
states: _client.states,
|
||||
);
|
||||
_gateway = ElevatingGateway(_client, _elevation);
|
||||
}
|
||||
@@ -192,7 +191,6 @@ class AppSession extends ChangeNotifier {
|
||||
_disposed = true;
|
||||
_sendUnsubscribe();
|
||||
unawaited(_states.cancel());
|
||||
unawaited(_elevation.dispose());
|
||||
unawaited(_frames.cancel());
|
||||
unawaited(_client.dispose());
|
||||
super.dispose();
|
||||
|
||||
@@ -80,21 +80,14 @@ Future<ChatMachineConnection> connectChatMachine(
|
||||
);
|
||||
await client.connectAny(endpoints);
|
||||
// Each machine confirms its own password through its own connection.
|
||||
final elevation = MachineElevation(
|
||||
name: server.name,
|
||||
gateway: client,
|
||||
states: client.states,
|
||||
);
|
||||
final elevation = MachineElevation(name: server.name, gateway: client);
|
||||
return ChatMachineConnection(
|
||||
gateway: ElevatingGateway(client, elevation),
|
||||
headers: headers,
|
||||
endpoint: () => client.endpoint?.toString() ?? '',
|
||||
connected: () => client.state.kind == ConnectionKind.connected,
|
||||
states: client.states,
|
||||
close: () async {
|
||||
await elevation.dispose();
|
||||
await client.dispose();
|
||||
},
|
||||
close: client.dispose,
|
||||
retry: () => client.connectAny(endpoints),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -97,9 +97,9 @@ class _ElevationDialogState extends State<ElevationDialog> {
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(
|
||||
'Running commands, opening a terminal and changing files on '
|
||||
'${widget.name} need its password again. This connection stays '
|
||||
'confirmed for 15 minutes.',
|
||||
'Running commands and changing files on ${widget.name} need its '
|
||||
'password. You are asked once; it stays confirmed until you sign '
|
||||
'out (at most a day).',
|
||||
),
|
||||
const SizedBox(height: 12),
|
||||
field,
|
||||
|
||||
@@ -1,10 +1,8 @@
|
||||
import 'dart:async';
|
||||
|
||||
import '../../api_gateway.dart';
|
||||
import '../../error_reason.dart';
|
||||
import '../../transport/connection_state.dart';
|
||||
|
||||
/// The server refused a request until this connection re-confirms the
|
||||
/// The server refused a request until this machine's session confirms the
|
||||
/// password (step-up re-authentication).
|
||||
const elevationRequiredCode = 'elevation_required';
|
||||
|
||||
@@ -17,24 +15,21 @@ typedef ElevationPrompt = Future<bool> Function(MachineElevation machine);
|
||||
bool isElevationRequired(Object error) =>
|
||||
error is ProtocolError && error.code == elevationRequiredCode;
|
||||
|
||||
/// One machine connection's step-up state.
|
||||
/// One machine's step-up state for this app run.
|
||||
///
|
||||
/// Elevation belongs to the websocket connection that confirmed the
|
||||
/// password: it survives navigation, ends when the server's window expires
|
||||
/// and is forgotten when the connection drops (the server forgets it too).
|
||||
/// The server keys a confirmation by the signed-in web session, so it
|
||||
/// survives navigation, reconnects and new connections of the same session:
|
||||
/// the operator is asked at most once per machine while that session lasts.
|
||||
/// Only a fresh `elevation_required` refusal (the session ended, the cap
|
||||
/// passed, or an older server that elevates one connection) asks again.
|
||||
/// Each machine elevates separately through its own gateway.
|
||||
class MachineElevation {
|
||||
MachineElevation({
|
||||
required this.name,
|
||||
required this.gateway,
|
||||
Stream<ConnectionState>? states,
|
||||
this.prompt,
|
||||
DateTime Function()? clock,
|
||||
}) : _clock = clock ?? DateTime.now {
|
||||
_states = states?.listen((state) {
|
||||
if (state.kind != ConnectionKind.connected) clear();
|
||||
});
|
||||
}
|
||||
}) : _clock = clock ?? DateTime.now;
|
||||
|
||||
/// The prompt every elevation uses when none is given; the app installs
|
||||
/// its native password dialog here.
|
||||
@@ -47,7 +42,6 @@ class MachineElevation {
|
||||
final ApiGateway gateway;
|
||||
final ElevationPrompt? prompt;
|
||||
final DateTime Function() _clock;
|
||||
StreamSubscription<ConnectionState>? _states;
|
||||
String? _token;
|
||||
DateTime? _expires;
|
||||
Future<bool>? _pending;
|
||||
@@ -89,7 +83,7 @@ class MachineElevation {
|
||||
final expiresAt = DateTime.tryParse('${body['expires_at'] ?? ''}');
|
||||
_expires = seconds is num
|
||||
? _clock().add(Duration(seconds: seconds.toInt()))
|
||||
: expiresAt?.toLocal() ?? _clock().add(const Duration(minutes: 15));
|
||||
: expiresAt?.toLocal() ?? _clock().add(const Duration(hours: 24));
|
||||
final token = body['token'];
|
||||
_token = token is String ? token : null;
|
||||
return null;
|
||||
@@ -101,14 +95,9 @@ class MachineElevation {
|
||||
}
|
||||
}
|
||||
|
||||
/// Forgets the elevation (the connection dropped or the server refused).
|
||||
/// Forgets the elevation (the server refused it).
|
||||
void clear() {
|
||||
_token = null;
|
||||
_expires = null;
|
||||
}
|
||||
|
||||
Future<void> dispose() async {
|
||||
await _states?.cancel();
|
||||
_states = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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,14 +161,14 @@ void main() {
|
||||
expect(elevation.token, 'tok-1');
|
||||
});
|
||||
|
||||
test('the token expires with the window and on disconnect', () async {
|
||||
test(
|
||||
'the token expires with the window; only a refusal forgets it',
|
||||
() 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');
|
||||
@@ -176,20 +176,18 @@ void main() {
|
||||
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, 'tok-1');
|
||||
elevation.clear();
|
||||
expect(elevation.token, isNull);
|
||||
await elevation.dispose();
|
||||
await states.close();
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
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();
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -98,9 +98,9 @@ func main() {
|
||||
// harness; the summariser falls back to the prompt's first line, so title
|
||||
// generation never blocks or fails run creation.
|
||||
options := server.Options{AllowAuthoring: *allowAuthoring, ArtifactsRoot: artifactsRoot, RunTitles: summarise.GenerateTitle, StepVerdicts: summarise.GenerateVerdictReason, VerificationFixtures: server.VerificationFixturesFromEnvironment(os.Getenv), DetachSessions: detach, AccessToken: accessToken}
|
||||
// Shell commands, terminals and file changes need a recent confirmation of
|
||||
// the web sign-in password on the same connection (step-up
|
||||
// re-authentication). PIPELINES_ELEVATION_MINUTES overrides 15 minutes.
|
||||
// Shell commands and file changes need one confirmation of the web sign-in
|
||||
// password per web session (step-up re-authentication), lasting at most
|
||||
// 24 hours; PIPELINES_ELEVATION_MINUTES overrides the cap.
|
||||
options.WebAuthFile = server.DefaultWebAuthFile()
|
||||
options.ElevationTTL = server.ElevationTTLFromEnvironment(os.Getenv)
|
||||
recorded := ""
|
||||
|
||||
@@ -90,27 +90,53 @@ the server-wide window still applies.
|
||||
native app accept both names.
|
||||
|
||||
**Step-up re-authentication.** A session alone can no longer run a shell. The
|
||||
following now need a password confirmation on the same websocket connection
|
||||
within the last 15 minutes (`PIPELINES_ELEVATION_MINUTES`, 1–720):
|
||||
following need one password confirmation per signed-in web session:
|
||||
- `chat_shell_run`, `chat_shell_input`;
|
||||
- `rename_path`, `move_path`, `delete_path`;
|
||||
- `terminal_open`, `terminal_input`;
|
||||
- `POST /files`.
|
||||
|
||||
Terminals (`terminal_open`, `terminal_input`) are not gated: each terminal
|
||||
starts `su -l`, which asks for the OS account password itself.
|
||||
|
||||
The flow:
|
||||
- The client sends `{"type":"elevate","password":…}`. The Go server reads the
|
||||
auth file read-only: it must be a regular file, owned by the user, mode 0600.
|
||||
- On success the reply is `elevated` with `{expires_at, seconds, token}`.
|
||||
- A refused request gets `{"type":"error","code":"elevation_required"}`.
|
||||
- A wrong password gets `elevation_failed`. Each wrong password doubles that
|
||||
connection's wait (1 s, up to 1 min). 10 failures per 5 min across the server
|
||||
session's wait (1 s, up to 1 min). 10 failures per 5 min across the server
|
||||
block all attempts. Only one scrypt check runs at a time.
|
||||
|
||||
Uploads travel outside the websocket. They carry the token in
|
||||
`X-Pipelines-Elevation`. The token lives only in memory and dies when its
|
||||
connection closes or expires. A refused upload gets 403
|
||||
`{"code":"elevation_required"}`. The peer relay forwards the header, so each
|
||||
machine elevates through its own gateway with its own password.
|
||||
How long it lasts (2026-11-10 change):
|
||||
- The Go server cannot see the cookie, so the proxy tells it which session a
|
||||
request belongs to. On `/connect` upgrades and `POST /files` the proxy sets
|
||||
`X-Pipelines-Session` to `AuthStore.sessionId(token)`: an HMAC, under the
|
||||
cookie secret, of the token's generation, the revoke-all time and the token
|
||||
id. It is never the cookie. Any client-supplied copy of the header is dropped.
|
||||
- The server trusts the header only on a request that carried the configured
|
||||
server token (`request_boundary.go`); otherwise it is removed and ignored.
|
||||
- Elevation is keyed by that id. Every connection and reconnect of the same
|
||||
browser or app sign-in stays elevated, up to a cap of 24 hours
|
||||
(`PIPELINES_ELEVATION_MINUTES`, 1–43200).
|
||||
- The session ending ends it. Logout revokes the token id. A password change
|
||||
bumps the generation and secret. Revoke-all invalidates every older token.
|
||||
In each case the proxy closes the session's sockets and refuses its cookie,
|
||||
and the next sign-in derives a new id, so the old elevation is never named
|
||||
again. It expires in server memory at the cap; a server restart forgets it.
|
||||
- A connection without a trusted session id (no server token configured, or a
|
||||
direct local client) elevates only itself, until it closes, as before.
|
||||
|
||||
Uploads travel outside the websocket. An elevated session's upload is allowed
|
||||
through its `X-Pipelines-Session`. The `X-Pipelines-Elevation` token also still
|
||||
works. It lives only in memory and lasts as long as its elevation: a session's
|
||||
token survives reconnects, a connection-only token dies with its connection. A
|
||||
refused upload gets 403 `{"code":"elevation_required"}`.
|
||||
|
||||
Peer relay: the relay sends a remote machine only its own peer cookie and a
|
||||
fixed header list, never `X-Pipelines-Session`. The remote proxy derives its
|
||||
own id from that cookie, so each machine elevates through its own gateway with
|
||||
its own password. It stays elevated for as long as the relay's sign-in to that
|
||||
machine lasts.
|
||||
|
||||
When does the gate apply?
|
||||
- **Auth file exists**: the gate is on.
|
||||
@@ -119,9 +145,12 @@ When does the gate apply?
|
||||
- **Auth file exists but is unusable**: the gate stays closed (fail closed).
|
||||
|
||||
In the app, one native dialog per machine (Material, or Cupertino on Apple
|
||||
platforms) asks for the password once. The app then retries the refused request.
|
||||
Concurrent refusals share one dialog. The elevation is forgotten when the
|
||||
connection drops.
|
||||
platforms) asks for the password. The app then retries the refused request.
|
||||
Concurrent refusals share one dialog. The app no longer forgets the elevation
|
||||
when the connection drops. It asks again only when the server answers a fresh
|
||||
`elevation_required`: the session ended, the cap passed, or an older server
|
||||
that elevates one connection. So it asks at most once per machine per app run
|
||||
while the session is valid.
|
||||
|
||||
## What the operator must do on the live install
|
||||
|
||||
@@ -151,8 +180,12 @@ for on the terminal.
|
||||
|
||||
## Not covered / residual
|
||||
|
||||
- `terminal_output` (attaching to and reading an existing terminal) is not
|
||||
gated. Typing into one (`terminal_input`) is.
|
||||
- Terminals are not gated by elevation (since 2026-11-10). A terminal's `su -l`
|
||||
login is the barrier. Once a terminal is logged in, any connection of a
|
||||
signed-in session can read it (`terminal_output`) and type into it
|
||||
(`terminal_input`).
|
||||
- Session elevation lives in server memory keyed by the derived id. A revoked
|
||||
session's entry is never named again, but it stays in memory until its cap.
|
||||
- `/chat-uploads` (chat attachments into the chat's upload area) is not gated.
|
||||
- The server-wide login window can lock the real operator out during an attack.
|
||||
That is the trade-off that bounds guessing.
|
||||
@@ -212,3 +245,49 @@ logged-out cookie still signed in: false
|
||||
Every browser and device was signed out; sign in again with the same password.
|
||||
password occurrences in server.log / web.log: 0 / 0
|
||||
```
|
||||
|
||||
## Session-long elevation, terminals ungated (2026-11-10, later)
|
||||
|
||||
The operator asked to be asked for the password once, not for every command,
|
||||
and not before opening a terminal. The change is described under "Step-up
|
||||
re-authentication" above.
|
||||
|
||||
Checks:
|
||||
- `umask 022 && go test ./internal/server ./internal/protocol ./internal/servertoken ./cmd/...`:
|
||||
ok (server 97 s). New: `elevation_session_test.go`. It covers: elevation
|
||||
survives a new connection of the same session, a different or absent
|
||||
session is not elevated, the header is ignored without the proxy token,
|
||||
the boundary strips the header, and the cap expires. `elevation_test.go`
|
||||
now checks that terminals are not gated.
|
||||
- `cd web && node --test test/`: tests 51, pass 51. New: `test/sessionId.test.mjs`.
|
||||
It covers: the id is derived and stable per token, a forged header is dropped,
|
||||
the id is sent on `/connect` and `POST /files` but not on `GET /files`, and
|
||||
the id changes after another sign-in, a logout and a password change.
|
||||
- `cd app && tool/analyze.sh`: clean.
|
||||
- `tool/memwatch/full.sh 'test/core test/features/chat/files test/features/terminal test/features/settings/security'`:
|
||||
PASS, 217 passed. New: `test/core/servers/elevation/session_elevation_test.dart`.
|
||||
It covers: no prompt on reconnect, and one prompt per run per machine with a
|
||||
second prompt only after a fresh refusal.
|
||||
- `test/application` still has its 51 baseline failures (pending timers).
|
||||
|
||||
Scratch end-to-end:
|
||||
- an isolated `HOME`;
|
||||
- a scratch DB;
|
||||
- OS-chosen loopback ports;
|
||||
- the built server plus `web/serve.mjs` with a scratch server token;
|
||||
- a tagged operator session.
|
||||
|
||||
```
|
||||
login: 303 pipelines_session
|
||||
connection 1: chat_shell_run before elevate: {"type":"error","code":"elevation_required",...}
|
||||
connection 1: elevate: {"type":"elevated","elevation":{"seconds":86400,"token":"<redacted>"}}
|
||||
connection 1: chat_shell_run after elevate: chat_shell_run running
|
||||
connection 1 closed
|
||||
connection 2 (reconnect, no elevate sent): chat_shell_run: chat_shell_run running
|
||||
chat_shell_runs: [{"command":"echo first-connection-ok","status":"exited","exit_code":0,"output":"first-connection-ok"},{"command":"echo reconnected-no-elevate-ok","status":"exited","exit_code":0,"output":"reconnected-no-elevate-ok"}]
|
||||
fresh sign-in: chat_shell_run is still gated: elevation_required
|
||||
fresh sign-in, no elevate: terminal_open: terminal_open running
|
||||
terminal output: "Password:"
|
||||
terminal_close: terminal_close closed
|
||||
password occurrences in server.log / web.log: 0 / 0
|
||||
```
|
||||
|
||||
@@ -1,11 +1,14 @@
|
||||
package protocol
|
||||
|
||||
// Step-up re-authentication. A signed-in web session can read everything, but
|
||||
// running shell commands, typing into a shell, changing or uploading files and
|
||||
// opening a terminal additionally need a recent password confirmation on the
|
||||
// same connection. The server verifies the password against the web sign-in
|
||||
// password file; the elevation lasts for a bounded time (15 minutes by
|
||||
// default) and ends with the connection.
|
||||
// running shell commands, typing into a shell and changing or uploading files
|
||||
// additionally need a password confirmation. (Terminals are not gated: each
|
||||
// one asks for the OS account password through `su -l`.) The server verifies
|
||||
// the password against the web sign-in password file. A confirmation belongs
|
||||
// to the web session the proxy reports (SessionHeader): every connection of
|
||||
// that session stays elevated until the session ends or a long cap (24 hours
|
||||
// by default) passes. A connection without a trusted session id elevates only
|
||||
// itself, until it closes.
|
||||
const (
|
||||
// Elevate confirms the sign-in password (Message.Password). The reply is
|
||||
// Elevated, or Error with Code ErrorElevationFailed or
|
||||
@@ -25,6 +28,11 @@ const (
|
||||
// ElevationHeader carries Elevation.Token on POST /files. A refused upload
|
||||
// answers 403 with a JSON body {"code":"elevation_required","error":...}.
|
||||
ElevationHeader = "X-Pipelines-Elevation"
|
||||
// SessionHeader carries the web proxy's opaque id of the signed-in web
|
||||
// session on /connect upgrades and POST /files: an HMAC of the session
|
||||
// token's generation and id, never the cookie. The server trusts it only
|
||||
// on a request authenticated with the shared server token.
|
||||
SessionHeader = "X-Pipelines-Session"
|
||||
)
|
||||
|
||||
// Elevation is the elevated reply body.
|
||||
@@ -33,7 +41,7 @@ type Elevation struct {
|
||||
// clients whose clock differs from the server's.
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
Seconds int `json:"seconds"`
|
||||
// Token authorises HTTP uploads for as long as this connection stays
|
||||
// elevated. It is random, held only in memory and never logged.
|
||||
// Token authorises HTTP uploads for as long as this elevation lasts. It
|
||||
// is random, held only in memory and never logged.
|
||||
Token string `json:"token,omitempty"`
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
@@ -16,19 +17,29 @@ import (
|
||||
|
||||
// Step-up re-authentication (see internal/protocol/elevation.go). A web
|
||||
// session is enough to read, but the requests below act as the operator's
|
||||
// shell, so each connection must also confirm the sign-in password recently.
|
||||
// shell, so the operator must also confirm the sign-in password once.
|
||||
//
|
||||
// A confirmation is keyed by the web session the token-authenticated proxy
|
||||
// reports (trustedSession): every connection and reconnect of that session
|
||||
// stays elevated until the cap (DefaultElevationTTL) passes. The session ending
|
||||
// ends it too: the proxy closes the session's sockets and refuses its cookie,
|
||||
// and its derived id changes with every new sign-in and every password change
|
||||
// or revoke-all, so no later request can name the old elevation. A connection
|
||||
// without a trusted session id (no server token configured, a direct client)
|
||||
// is elevated by itself only, until it closes.
|
||||
//
|
||||
// The gate is on whenever the web sign-in password file exists; a server run
|
||||
// without the web proxy (tests, a bare loopback server) has nothing to verify
|
||||
// against and keeps its previous behaviour. A file that exists but cannot be
|
||||
// read safely keeps the gate on and every elevate fails (fail closed).
|
||||
|
||||
// DefaultElevationTTL is how long one confirmation lasts on a connection.
|
||||
const DefaultElevationTTL = 15 * time.Minute
|
||||
// DefaultElevationTTL caps how long one confirmation lasts.
|
||||
const DefaultElevationTTL = 24 * time.Hour
|
||||
|
||||
// Elevation attempt limits: a connection waits elevationBackoffBase doubled
|
||||
// for each consecutive wrong password (at most elevationBackoffMax), and the
|
||||
// whole server refuses attempts after elevationGlobalFailures wrong passwords
|
||||
// within elevationGlobalWindow.
|
||||
// Elevation attempt limits: a key waits elevationBackoffBase doubled for each
|
||||
// consecutive wrong password (at most elevationBackoffMax), and the whole
|
||||
// server refuses attempts after elevationGlobalFailures wrong passwords within
|
||||
// elevationGlobalWindow. At most elevationTokenLimit elevations live at once.
|
||||
const (
|
||||
elevationBackoffBase = time.Second
|
||||
elevationBackoffMax = time.Minute
|
||||
@@ -37,14 +48,34 @@ const (
|
||||
elevationTokenLimit = 256
|
||||
)
|
||||
|
||||
// Terminals are deliberately absent: terminal_open starts `su -l`, so the OS
|
||||
// account password is the barrier there.
|
||||
var elevationGated = map[string]bool{
|
||||
protocol.ChatShellRun: true,
|
||||
protocol.ChatShellInput: true,
|
||||
protocol.RenamePath: true,
|
||||
protocol.MovePath: true,
|
||||
protocol.DeletePath: true,
|
||||
protocol.TerminalOpen: true,
|
||||
protocol.TerminalInput: true,
|
||||
}
|
||||
|
||||
const (
|
||||
sessionKeyPrefix = "session:"
|
||||
connectionKeyPrefix = "conn:"
|
||||
)
|
||||
|
||||
// sessionElevationKey keys elevation by a trusted web session id.
|
||||
func sessionElevationKey(session string) string { return sessionKeyPrefix + session }
|
||||
|
||||
// connectionElevationKey keys elevation by one websocket connection.
|
||||
func connectionElevationKey(client string) string { return connectionKeyPrefix + client }
|
||||
|
||||
// elevationKeyFor is the key a connection elevates under: its web session
|
||||
// when the proxy reported one, else the connection itself.
|
||||
func elevationKeyFor(session, client string) string {
|
||||
if session != "" {
|
||||
return sessionElevationKey(session)
|
||||
}
|
||||
return connectionElevationKey(client)
|
||||
}
|
||||
|
||||
type elevationGate struct {
|
||||
@@ -55,7 +86,7 @@ type elevationGate struct {
|
||||
verify sync.Mutex
|
||||
|
||||
mu sync.Mutex
|
||||
until map[string]time.Time // client -> elevation end
|
||||
until map[string]time.Time // key -> elevation end
|
||||
tokens map[string]elevationToken
|
||||
failures map[string]elevationFailures
|
||||
recent []time.Time // wrong passwords, server-wide, oldest first
|
||||
@@ -63,7 +94,7 @@ type elevationGate struct {
|
||||
}
|
||||
|
||||
type elevationToken struct {
|
||||
client string
|
||||
key string
|
||||
until time.Time
|
||||
}
|
||||
|
||||
@@ -106,43 +137,43 @@ func (g *elevationGate) enabled() bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *elevationGate) elevated(client string) bool {
|
||||
func (g *elevationGate) elevated(key string) bool {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
until, ok := g.until[client]
|
||||
until, ok := g.until[key]
|
||||
if ok && !g.now().Before(until) {
|
||||
g.dropLocked(client)
|
||||
g.dropLocked(key)
|
||||
return false
|
||||
}
|
||||
return ok
|
||||
}
|
||||
|
||||
// allows reports whether client may perform a gated request now.
|
||||
func (g *elevationGate) allows(client string) bool {
|
||||
return g.elevated(client) || !g.enabled()
|
||||
// allows reports whether key may perform a gated request now.
|
||||
func (g *elevationGate) allows(key string) bool {
|
||||
return g.elevated(key) || !g.enabled()
|
||||
}
|
||||
|
||||
// refuse answers a gated request on a connection that is not elevated with a
|
||||
// typed error and reports true; any other request passes (false).
|
||||
func (g *elevationGate) refuse(request protocol.Message, client string, write func(protocol.Message) error) bool {
|
||||
if !elevationGated[request.Type] || g.allows(client) {
|
||||
// refuse answers a gated request whose key is not elevated with a typed error
|
||||
// and reports true; any other request passes (false).
|
||||
func (g *elevationGate) refuse(request protocol.Message, key string, write func(protocol.Message) error) bool {
|
||||
if !elevationGated[request.Type] || g.allows(key) {
|
||||
return false
|
||||
}
|
||||
_ = write(protocol.Message{
|
||||
Type: protocol.Error,
|
||||
ID: request.ID,
|
||||
Code: protocol.ErrorElevationRequired,
|
||||
Error: "Confirm your password to run commands, change files or use a terminal on this machine.",
|
||||
Error: "Confirm your password to run commands or change files on this machine.",
|
||||
})
|
||||
return true
|
||||
}
|
||||
|
||||
// handle answers an elevate request and reports true; other requests pass.
|
||||
func (g *elevationGate) handle(request protocol.Message, client string, write func(protocol.Message) error) bool {
|
||||
func (g *elevationGate) handle(request protocol.Message, key string, write func(protocol.Message) error) bool {
|
||||
if request.Type != protocol.Elevate {
|
||||
return false
|
||||
}
|
||||
reply, err := g.elevate(client, request.Password)
|
||||
reply, err := g.elevate(key, request.Password)
|
||||
if err != nil {
|
||||
code := protocol.ErrorElevationFailed
|
||||
if errors.Is(err, errWebAuthMissing) {
|
||||
@@ -155,11 +186,11 @@ func (g *elevationGate) handle(request protocol.Message, client string, write fu
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *elevationGate) elevate(client, password string) (*protocol.Elevation, error) {
|
||||
func (g *elevationGate) elevate(key, password string) (*protocol.Elevation, error) {
|
||||
if g == nil || g.authFile == "" {
|
||||
return nil, fmt.Errorf("this server has no web sign-in password to confirm: %w", errWebAuthMissing)
|
||||
}
|
||||
if wait := g.blocked(client); wait > 0 {
|
||||
if wait := g.blocked(key); wait > 0 {
|
||||
return nil, fmt.Errorf("too many wrong passwords; try again in %d seconds", int((wait+time.Second-1)/time.Second))
|
||||
}
|
||||
verifier, err := readWebAuth(g.authFile)
|
||||
@@ -178,13 +209,13 @@ func (g *elevationGate) elevate(client, password string) (*protocol.Elevation, e
|
||||
return nil, errors.New("the password could not be checked; see the server log")
|
||||
}
|
||||
if !ok {
|
||||
g.noteFailure(client)
|
||||
g.noteFailure(key)
|
||||
return nil, errors.New("wrong password")
|
||||
}
|
||||
return g.grant(client)
|
||||
return g.grant(key)
|
||||
}
|
||||
|
||||
func (g *elevationGate) blocked(client string) time.Duration {
|
||||
func (g *elevationGate) blocked(key string) time.Duration {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
@@ -192,7 +223,7 @@ func (g *elevationGate) blocked(client string) time.Duration {
|
||||
if len(g.recent) >= elevationGlobalFailures {
|
||||
return g.recent[0].Add(elevationGlobalWindow).Sub(now)
|
||||
}
|
||||
if entry, ok := g.failures[client]; ok && now.Before(entry.until) {
|
||||
if entry, ok := g.failures[key]; ok && now.Before(entry.until) {
|
||||
return entry.until.Sub(now)
|
||||
}
|
||||
return 0
|
||||
@@ -206,19 +237,26 @@ func (g *elevationGate) pruneLocked(now time.Time) {
|
||||
g.recent = g.recent[keep:]
|
||||
}
|
||||
|
||||
func (g *elevationGate) noteFailure(client string) {
|
||||
func (g *elevationGate) noteFailure(key string) {
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
g.recent = append(g.recent, now)
|
||||
entry := g.failures[client]
|
||||
// A session's failures outlive its connections; drop entries whose wait
|
||||
// is long over so the map stays bounded by recent activity.
|
||||
for other, entry := range g.failures {
|
||||
if !now.Before(entry.until.Add(elevationGlobalWindow)) {
|
||||
delete(g.failures, other)
|
||||
}
|
||||
}
|
||||
entry := g.failures[key]
|
||||
entry.count++
|
||||
delay := elevationBackoffBase << min(entry.count-1, 6)
|
||||
entry.until = now.Add(min(delay, elevationBackoffMax))
|
||||
g.failures[client] = entry
|
||||
g.failures[key] = entry
|
||||
}
|
||||
|
||||
func (g *elevationGate) grant(client string) (*protocol.Elevation, error) {
|
||||
func (g *elevationGate) grant(key string) (*protocol.Elevation, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return nil, err
|
||||
@@ -228,18 +266,18 @@ func (g *elevationGate) grant(client string) (*protocol.Elevation, error) {
|
||||
defer g.mu.Unlock()
|
||||
now := g.now()
|
||||
until := now.Add(g.ttl)
|
||||
g.dropLocked(client)
|
||||
delete(g.failures, client)
|
||||
for key, entry := range g.tokens {
|
||||
if !now.Before(entry.until) {
|
||||
delete(g.tokens, key)
|
||||
g.dropLocked(key)
|
||||
delete(g.failures, key)
|
||||
for other, end := range g.until {
|
||||
if !now.Before(end) {
|
||||
g.dropLocked(other)
|
||||
}
|
||||
}
|
||||
if len(g.tokens) >= elevationTokenLimit {
|
||||
return nil, errors.New("too many elevated connections on this server")
|
||||
if len(g.until) >= elevationTokenLimit {
|
||||
return nil, errors.New("too many elevated sessions on this server")
|
||||
}
|
||||
g.until[client] = until
|
||||
g.tokens[tokenKey(token)] = elevationToken{client: client, until: until}
|
||||
g.until[key] = until
|
||||
g.tokens[tokenKey(token)] = elevationToken{key: key, until: until}
|
||||
return &protocol.Elevation{
|
||||
ExpiresAt: until.UTC().Format(time.RFC3339),
|
||||
Seconds: int(g.ttl / time.Second),
|
||||
@@ -252,28 +290,30 @@ func tokenKey(token string) string {
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// dropLocked ends client's elevation and its upload token.
|
||||
func (g *elevationGate) dropLocked(client string) {
|
||||
delete(g.until, client)
|
||||
for key, entry := range g.tokens {
|
||||
if entry.client == client {
|
||||
delete(g.tokens, key)
|
||||
// dropLocked ends key's elevation and its upload token.
|
||||
func (g *elevationGate) dropLocked(key string) {
|
||||
delete(g.until, key)
|
||||
for hash, entry := range g.tokens {
|
||||
if entry.key == key {
|
||||
delete(g.tokens, hash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// forget ends a closed connection's elevation and attempt history.
|
||||
func (g *elevationGate) forget(client string) {
|
||||
if g == nil {
|
||||
// closed is called when a connection that elevated under key ends: a
|
||||
// connection's own elevation and attempt history end with it, a web session's
|
||||
// outlive it for that session's next connection.
|
||||
func (g *elevationGate) closed(key string) {
|
||||
if g == nil || !strings.HasPrefix(key, connectionKeyPrefix) {
|
||||
return
|
||||
}
|
||||
g.mu.Lock()
|
||||
defer g.mu.Unlock()
|
||||
g.dropLocked(client)
|
||||
delete(g.failures, client)
|
||||
g.dropLocked(key)
|
||||
delete(g.failures, key)
|
||||
}
|
||||
|
||||
// tokenValid reports whether token belongs to a still elevated connection.
|
||||
// tokenValid reports whether token belongs to a still live elevation.
|
||||
func (g *elevationGate) tokenValid(token string) bool {
|
||||
if token == "" {
|
||||
return false
|
||||
@@ -285,7 +325,7 @@ func (g *elevationGate) tokenValid(token string) bool {
|
||||
return false
|
||||
}
|
||||
if !g.now().Before(entry.until) {
|
||||
g.dropLocked(entry.client)
|
||||
g.dropLocked(entry.key)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
|
||||
@@ -10,12 +10,15 @@ import (
|
||||
)
|
||||
|
||||
// requireElevationHTTP guards an HTTP route that changes the operator's files.
|
||||
// The upload travels outside the websocket, so the elevated connection's token
|
||||
// (protocol.ElevationHeader) authorises it; it is valid only while that
|
||||
// connection stays open and elevated.
|
||||
// The upload travels outside the websocket. It is allowed when the web session
|
||||
// the proxy reports is elevated, or when it carries a live elevation's token
|
||||
// (protocol.ElevationHeader; a connection-only elevation's token ends with its
|
||||
// connection).
|
||||
func (g *elevationGate) requireElevationHTTP(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if g.tokenValid(r.Header.Get(protocol.ElevationHeader)) || !g.enabled() {
|
||||
session := trustedSession(r)
|
||||
if (session != "" && g.elevated(sessionElevationKey(session))) ||
|
||||
g.tokenValid(r.Header.Get(protocol.ElevationHeader)) || !g.enabled() {
|
||||
next(w, r)
|
||||
return
|
||||
}
|
||||
@@ -29,11 +32,15 @@ func (g *elevationGate) requireElevationHTTP(next http.HandlerFunc) http.Handler
|
||||
}
|
||||
}
|
||||
|
||||
// ElevationTTLFromEnvironment reads PIPELINES_ELEVATION_MINUTES (1 to 720);
|
||||
// maxElevationMinutes is the longest PIPELINES_ELEVATION_MINUTES accepted:
|
||||
// 30 days, the longest a remembered web session lasts.
|
||||
const maxElevationMinutes = 30 * 24 * 60
|
||||
|
||||
// ElevationTTLFromEnvironment reads PIPELINES_ELEVATION_MINUTES (1 to 43200);
|
||||
// anything else keeps DefaultElevationTTL.
|
||||
func ElevationTTLFromEnvironment(getenv func(string) string) time.Duration {
|
||||
minutes, err := strconv.Atoi(getenv("PIPELINES_ELEVATION_MINUTES"))
|
||||
if err != nil || minutes < 1 || minutes > 720 {
|
||||
if err != nil || minutes < 1 || minutes > maxElevationMinutes {
|
||||
return DefaultElevationTTL
|
||||
}
|
||||
return time.Duration(minutes) * time.Minute
|
||||
|
||||
@@ -0,0 +1,193 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/coder/websocket"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/servertoken"
|
||||
)
|
||||
|
||||
const (
|
||||
elevationTestPassword = "right password here"
|
||||
elevationTestToken = "proxy-token-0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
sessionA = "session-a-0123456789abcdef"
|
||||
sessionB = "session-b-0123456789abcdef"
|
||||
)
|
||||
|
||||
// elevationServer serves a gated handler; token is the shared proxy token
|
||||
// ("" for a server without one).
|
||||
func elevationServer(t *testing.T, ctx context.Context, token string) *httptest.Server {
|
||||
t.Helper()
|
||||
t.Setenv("HOME", t.TempDir()) // the gated handlers never touch the real home
|
||||
srv := httptest.NewServer(HandlerWithOptions(ctx, Options{WebAuthFile: writeTestWebAuth(t, elevationTestPassword), AccessToken: token}))
|
||||
t.Cleanup(srv.Close)
|
||||
return srv
|
||||
}
|
||||
|
||||
func proxiedHeaders(token, session string) http.Header {
|
||||
headers := http.Header{}
|
||||
if token != "" {
|
||||
headers.Set(servertoken.Header, token)
|
||||
}
|
||||
if session != "" {
|
||||
headers.Set(protocol.SessionHeader, session)
|
||||
}
|
||||
return headers
|
||||
}
|
||||
|
||||
func dialAs(t *testing.T, ctx context.Context, srv *httptest.Server, token, session string) *websocket.Conn {
|
||||
t.Helper()
|
||||
conn, _, err := websocket.Dial(ctx, "ws"+strings.TrimPrefix(srv.URL, "http")+"/connect",
|
||||
&websocket.DialOptions{HTTPHeader: proxiedHeaders(token, session)})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { conn.CloseNow() })
|
||||
return conn
|
||||
}
|
||||
|
||||
var gatedDelete = protocol.Message{Type: protocol.DeletePath, ID: "delete", DeletePath: &protocol.DeletePathRequest{Path: "~/absent-elevation-test"}}
|
||||
|
||||
func refused(t *testing.T, ctx context.Context, conn *websocket.Conn) bool {
|
||||
t.Helper()
|
||||
return elevationRequest(t, ctx, conn, gatedDelete).Code == protocol.ErrorElevationRequired
|
||||
}
|
||||
|
||||
func elevateOn(t *testing.T, ctx context.Context, conn *websocket.Conn) *protocol.Elevation {
|
||||
t.Helper()
|
||||
reply := elevationRequest(t, ctx, conn, protocol.Message{Type: protocol.Elevate, ID: "elevate", Password: elevationTestPassword})
|
||||
if reply.Type != protocol.Elevated || reply.Elevation == nil {
|
||||
t.Fatalf("elevate: %+v", reply)
|
||||
}
|
||||
return reply.Elevation
|
||||
}
|
||||
|
||||
func uploadAs(t *testing.T, srv *httptest.Server, token, session, elevation string) int {
|
||||
t.Helper()
|
||||
request, _ := http.NewRequest(http.MethodPost, srv.URL+"/files?path=~/absent-elevation-folder", strings.NewReader("x"))
|
||||
request.Header = proxiedHeaders(token, session)
|
||||
request.Header.Set(chatUploadHeader, "elevation-test.txt")
|
||||
if elevation != "" {
|
||||
request.Header.Set(protocol.ElevationHeader, elevation)
|
||||
}
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
return response.StatusCode
|
||||
}
|
||||
|
||||
func TestElevationLastsForTheWebSessionAcrossConnections(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
srv := elevationServer(t, ctx, elevationTestToken)
|
||||
first := dialAs(t, ctx, srv, elevationTestToken, sessionA)
|
||||
if !refused(t, ctx, first) {
|
||||
t.Fatal("unelevated session allowed")
|
||||
}
|
||||
if uploadAs(t, srv, elevationTestToken, sessionA, "") != http.StatusForbidden {
|
||||
t.Fatal("upload allowed before elevation")
|
||||
}
|
||||
elevateOn(t, ctx, first)
|
||||
first.Close(websocket.StatusNormalClosure, "")
|
||||
// A reconnect of the same session is elevated without asking again.
|
||||
second := dialAs(t, ctx, srv, elevationTestToken, sessionA)
|
||||
if refused(t, ctx, second) {
|
||||
t.Fatal("reconnected session lost its elevation")
|
||||
}
|
||||
if uploadAs(t, srv, elevationTestToken, sessionA, "") == http.StatusForbidden {
|
||||
t.Fatal("elevated session's upload refused")
|
||||
}
|
||||
// Another session, and a connection naming no session, are not.
|
||||
if !refused(t, ctx, dialAs(t, ctx, srv, elevationTestToken, sessionB)) {
|
||||
t.Fatal("a different session inherited elevation")
|
||||
}
|
||||
if !refused(t, ctx, dialAs(t, ctx, srv, elevationTestToken, "")) {
|
||||
t.Fatal("a connection without a session inherited elevation")
|
||||
}
|
||||
if uploadAs(t, srv, elevationTestToken, sessionB, "") != http.StatusForbidden {
|
||||
t.Fatal("a different session's upload allowed")
|
||||
}
|
||||
// A malformed id is not trusted (and so elevates nothing shared).
|
||||
if !refused(t, ctx, dialAs(t, ctx, srv, elevationTestToken, "short")) {
|
||||
t.Fatal("malformed session id trusted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionHeaderIgnoredWithoutTheProxyToken(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
defer cancel()
|
||||
srv := elevationServer(t, ctx, "")
|
||||
first := dialAs(t, ctx, srv, "", sessionA)
|
||||
token := elevateOn(t, ctx, first).Token
|
||||
if refused(t, ctx, first) {
|
||||
t.Fatal("elevated connection refused")
|
||||
}
|
||||
// Without a configured token the header names nothing: a second
|
||||
// connection claiming the same session is not elevated.
|
||||
if !refused(t, ctx, dialAs(t, ctx, srv, "", sessionA)) {
|
||||
t.Fatal("untrusted session header shared elevation")
|
||||
}
|
||||
if uploadAs(t, srv, "", sessionA, "") != http.StatusForbidden {
|
||||
t.Fatal("untrusted session header authorised an upload")
|
||||
}
|
||||
// The connection's own token still works until the connection closes.
|
||||
if uploadAs(t, srv, "", "", token) == http.StatusForbidden {
|
||||
t.Fatal("connection token refused")
|
||||
}
|
||||
first.Close(websocket.StatusNormalClosure, "")
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for uploadAs(t, srv, "", "", token) != http.StatusForbidden {
|
||||
if time.Now().After(deadline) {
|
||||
t.Fatal("connection token outlived its connection")
|
||||
}
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBoundaryTrustsSessionOnlyWithToken(t *testing.T) {
|
||||
var seen []string
|
||||
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
seen = append(seen, trustedSession(r)+"|"+r.Header.Get(protocol.SessionHeader))
|
||||
})
|
||||
for _, token := range []string{"", elevationTestToken} {
|
||||
request := httptest.NewRequest(http.MethodPost, "/files", nil)
|
||||
request.Header = proxiedHeaders(token, sessionA)
|
||||
guardRequests(next, token).ServeHTTP(httptest.NewRecorder(), request)
|
||||
}
|
||||
if len(seen) != 2 || seen[0] != "|" || seen[1] != sessionA+"|" {
|
||||
t.Fatalf("session trust = %q", seen)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionElevationCapAndClose(t *testing.T) {
|
||||
gate := newElevationGate(writeTestWebAuth(t, elevationTestPassword), time.Hour)
|
||||
now := time.Unix(1_800_000_000, 0)
|
||||
gate.now = func() time.Time { return now }
|
||||
key := sessionElevationKey(sessionA)
|
||||
reply, err := gate.elevate(key, elevationTestPassword)
|
||||
if err != nil || reply.Seconds != 3600 {
|
||||
t.Fatalf("elevate: %+v %v", reply, err)
|
||||
}
|
||||
// A session's connection closing keeps the session elevated.
|
||||
gate.closed(key)
|
||||
if !gate.elevated(key) || !gate.tokenValid(reply.Token) {
|
||||
t.Fatal("session elevation ended with a connection")
|
||||
}
|
||||
now = now.Add(time.Hour - time.Second)
|
||||
if !gate.elevated(key) {
|
||||
t.Fatal("session elevation ended before its cap")
|
||||
}
|
||||
now = now.Add(time.Second)
|
||||
if gate.elevated(key) || gate.tokenValid(reply.Token) {
|
||||
t.Fatal("session elevation outlived its cap")
|
||||
}
|
||||
}
|
||||
@@ -109,11 +109,12 @@ func TestElevationBackoffGlobalCapAndExpiry(t *testing.T) {
|
||||
t.Fatalf("global cap not applied: %v", err)
|
||||
}
|
||||
now = now.Add(elevationGlobalWindow)
|
||||
if _, err := gate.elevate("fresh", "right password here"); err != nil {
|
||||
fresh := connectionElevationKey("fresh")
|
||||
if _, err := gate.elevate(fresh, "right password here"); err != nil {
|
||||
t.Fatalf("global window did not drain: %v", err)
|
||||
}
|
||||
gate.forget("fresh")
|
||||
if gate.elevated("fresh") {
|
||||
gate.closed(fresh)
|
||||
if gate.elevated(fresh) {
|
||||
t.Fatal("closed connection stayed elevated")
|
||||
}
|
||||
}
|
||||
@@ -156,8 +157,6 @@ func TestGatedRequestsNeedElevationOnTheSameConnection(t *testing.T) {
|
||||
{Type: protocol.DeletePath, ID: "delete", DeletePath: &protocol.DeletePathRequest{Path: "~/absent-elevation-test"}},
|
||||
{Type: protocol.RenamePath, ID: "rename"},
|
||||
{Type: protocol.MovePath, ID: "move"},
|
||||
{Type: protocol.TerminalOpen, ID: "terminal"},
|
||||
{Type: protocol.TerminalInput, ID: "terminal-input"},
|
||||
}
|
||||
for _, request := range gated {
|
||||
reply := elevationRequest(t, ctx, conn, request)
|
||||
@@ -165,6 +164,16 @@ func TestGatedRequestsNeedElevationOnTheSameConnection(t *testing.T) {
|
||||
t.Fatalf("%s not refused: %+v", request.Type, reply)
|
||||
}
|
||||
}
|
||||
// Terminals are not gated (su -l asks for the account password); this
|
||||
// server has no store, so terminal_open fails on its own terms.
|
||||
for _, request := range []protocol.Message{
|
||||
{Type: protocol.TerminalOpen, ID: "terminal"},
|
||||
{Type: protocol.TerminalInput, ID: "terminal-input", TerminalRequest: &protocol.TerminalRequest{TerminalID: "absent", Data: "x"}},
|
||||
} {
|
||||
if reply := elevationRequest(t, ctx, conn, request); reply.Code == protocol.ErrorElevationRequired {
|
||||
t.Fatalf("%s gated: %+v", request.Type, reply)
|
||||
}
|
||||
}
|
||||
// Reads are not gated.
|
||||
if reply := elevationRequest(t, ctx, conn, protocol.Message{Type: protocol.Ping, ID: "ping"}); reply.Type != protocol.Pong {
|
||||
t.Fatalf("ping refused: %+v", reply)
|
||||
@@ -233,11 +242,13 @@ func TestElevationGateOffWithoutWebAuthFile(t *testing.T) {
|
||||
// A present but unusable file keeps the gate closed.
|
||||
broken := writeTestAuthJSON(t, map[string]any{"secret": "x"})
|
||||
closed := newElevationGate(broken, 0)
|
||||
if !closed.refuse(protocol.Message{Type: protocol.TerminalOpen}, "a", write) {
|
||||
if !closed.refuse(protocol.Message{Type: protocol.ChatShellRun}, "a", write) {
|
||||
t.Fatal("unusable password file opened the gate")
|
||||
}
|
||||
if ElevationTTLFromEnvironment(func(string) string { return "30" }) != 30*time.Minute ||
|
||||
ElevationTTLFromEnvironment(func(string) string { return "0" }) != DefaultElevationTTL {
|
||||
ElevationTTLFromEnvironment(func(string) string { return "1440" }) != 24*time.Hour ||
|
||||
ElevationTTLFromEnvironment(func(string) string { return "43201" }) != DefaultElevationTTL ||
|
||||
ElevationTTLFromEnvironment(func(string) string { return "0" }) != DefaultElevationTTL || DefaultElevationTTL != 24*time.Hour {
|
||||
t.Fatal("PIPELINES_ELEVATION_MINUTES not honoured")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,10 +1,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
"git.a-wan.net/alan/pipelines/internal/servertoken"
|
||||
)
|
||||
|
||||
@@ -23,6 +25,11 @@ import (
|
||||
// request must carry it in servertoken.Header, except the chat-tools MCP
|
||||
// endpoint (its own per-chat token, loopback and no-Origin checks) and
|
||||
// /healthz (a bare liveness boolean).
|
||||
// - Session: protocol.SessionHeader names the web session a proxied request
|
||||
// belongs to (an opaque id the proxy derives from the verified cookie). It
|
||||
// is trusted only on a request that carried the configured token; it is
|
||||
// always removed from the request and, when trusted, reaches handlers
|
||||
// through the context (trustedSession).
|
||||
type requestBoundary struct {
|
||||
next http.Handler
|
||||
token string
|
||||
@@ -47,9 +54,36 @@ func (b requestBoundary) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
// The secret never reaches a handler, a log line or a forwarded request.
|
||||
r.Header.Del(servertoken.Header)
|
||||
session := r.Header.Get(protocol.SessionHeader)
|
||||
r.Header.Del(protocol.SessionHeader)
|
||||
if b.token != "" && validSessionID(session) {
|
||||
r = r.WithContext(context.WithValue(r.Context(), sessionContextKey{}, session))
|
||||
}
|
||||
b.next.ServeHTTP(w, r)
|
||||
}
|
||||
|
||||
type sessionContextKey struct{}
|
||||
|
||||
// trustedSession is the web session id the token-authenticated proxy reported
|
||||
// for r, or "" (no server token configured, a direct client, or no header).
|
||||
func trustedSession(r *http.Request) string {
|
||||
session, _ := r.Context().Value(sessionContextKey{}).(string)
|
||||
return session
|
||||
}
|
||||
|
||||
// validSessionID bounds the proxy's opaque id: 16 to 128 URL-safe characters.
|
||||
func validSessionID(value string) bool {
|
||||
if len(value) < 16 || len(value) > 128 {
|
||||
return false
|
||||
}
|
||||
for _, c := range value {
|
||||
if !(c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c >= '0' && c <= '9' || c == '-' || c == '_') {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func tokenExempt(path string) bool {
|
||||
return path == "/healthz" || strings.HasPrefix(path, chatToolPath)
|
||||
}
|
||||
|
||||
@@ -71,7 +71,7 @@ type Options struct {
|
||||
AccessToken string
|
||||
// WebAuthFile is the web sign-in password file step-up re-authentication
|
||||
// verifies against (see elevation.go); empty disables the gate.
|
||||
// ElevationTTL is how long one confirmation lasts (default 15 minutes).
|
||||
// ElevationTTL caps how long one confirmation lasts (default 24 hours).
|
||||
WebAuthFile string
|
||||
ElevationTTL time.Duration
|
||||
}
|
||||
@@ -233,7 +233,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
// clientID is this connection's identity for chat session claims: a
|
||||
// second client is refused a session another connection holds.
|
||||
clientID := chatClientToken()
|
||||
defer elevation.forget(clientID)
|
||||
// Elevation belongs to the web session the proxy reported, so a
|
||||
// reconnect stays elevated; without one, to this connection alone.
|
||||
elevationKey := elevationKeyFor(trustedSession(r), clientID)
|
||||
defer elevation.closed(elevationKey)
|
||||
// Drop every run subscription this connection holds when the read loop
|
||||
// returns, so no subscriber, goroutine or timer outlives the connection.
|
||||
defer events.unsubscribeClient(clientID)
|
||||
@@ -284,7 +287,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "pipeline authoring is disabled on this execution server; ask the operator to publish through an explicitly enabled authoring server"})
|
||||
continue
|
||||
}
|
||||
if elevation.handle(request, clientID, write) || elevation.refuse(request, clientID, write) {
|
||||
if elevation.handle(request, elevationKey, write) || elevation.refuse(request, elevationKey, write) {
|
||||
continue
|
||||
}
|
||||
if isChatSearch(request) {
|
||||
|
||||
+19
-2
@@ -149,6 +149,19 @@ function session(req) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The derived id the server keys step-up elevation by (AuthStore.sessionId),
|
||||
* or "" without a live session. Forwarded on /connect and POST /files only.
|
||||
*/
|
||||
function elevationSession(req) {
|
||||
if (!auth) return "";
|
||||
try {
|
||||
return store.sessionId(sessionValue(req));
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
function isAuthed(req) {
|
||||
return !auth || session(req) !== null;
|
||||
}
|
||||
@@ -444,8 +457,10 @@ async function serveIndex(res) {
|
||||
*/
|
||||
function proxyArtifact(req, res) {
|
||||
let headers;
|
||||
// An upload is gated by the session's elevation on the server.
|
||||
const upload = req.method === "POST" && (req.url ?? "").split("?")[0] === "/files";
|
||||
try {
|
||||
headers = withServerToken(req.headers);
|
||||
headers = withServerToken(req.headers, undefined, upload ? elevationSession(req) : "");
|
||||
} catch {
|
||||
res.writeHead(503, { "Content-Type": "text/plain; charset=utf-8" }).end("Server token unavailable");
|
||||
return;
|
||||
@@ -667,7 +682,9 @@ const targetHost = pipelinesTarget.slice(0, pipelinesTarget.lastIndexOf(":")) ||
|
||||
function proxyUpgrade(req, clientSocket, clientHead) {
|
||||
let headers;
|
||||
try {
|
||||
headers = withServerToken(req.headers);
|
||||
// The server keys step-up elevation by this session, so a reconnect of
|
||||
// the same sign-in stays elevated.
|
||||
headers = withServerToken(req.headers, undefined, elevationSession(req));
|
||||
} catch {
|
||||
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
|
||||
return;
|
||||
|
||||
@@ -214,6 +214,21 @@ export class AuthStore {
|
||||
return { id, expires: Number(expires), issued: Number(issued) };
|
||||
}
|
||||
|
||||
/**
|
||||
* The opaque id the Pipelines server keys step-up elevation by, for a valid
|
||||
* token, else "". It is an HMAC (under the cookie secret) of the token's
|
||||
* generation and id, never the cookie: a new sign-in, a password change or
|
||||
* a revoke-all yields a new id, so the old elevation is never named again.
|
||||
*/
|
||||
sessionId(token) {
|
||||
const session = this.check(token);
|
||||
if (!session) return "";
|
||||
const { generation } = this.load();
|
||||
return createHmac("sha256", this.load().secret)
|
||||
.update(`pipelines-elevation-session.v1.${generation}.${this.notBefore}.${session.id}`)
|
||||
.digest("base64url");
|
||||
}
|
||||
|
||||
/** Signs one token out (server-side logout). */
|
||||
revoke(token) {
|
||||
const session = this.check(token);
|
||||
|
||||
+12
-5
@@ -8,6 +8,8 @@ import { homedir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
export const SERVER_TOKEN_HEADER = "x-pipelines-token";
|
||||
// The proxy's opaque id of the signed-in web session (protocol.SessionHeader).
|
||||
export const SESSION_HEADER = "x-pipelines-session";
|
||||
|
||||
export function serverTokenPath(env = process.env) {
|
||||
if (env.PIPELINES_SERVER_TOKEN_FILE) return env.PIPELINES_SERVER_TOKEN_FILE;
|
||||
@@ -37,12 +39,17 @@ export function serverToken(path = serverTokenPath()) {
|
||||
return cached.token;
|
||||
}
|
||||
|
||||
// Headers for the local upstream: any client-supplied token is dropped and the
|
||||
// proxy's own is set, so the browser never chooses what the server sees.
|
||||
export function withServerToken(headers, token = serverToken()) {
|
||||
// Headers for the local upstream: any client-supplied token or session id is
|
||||
// dropped and the proxy's own token is set, so the browser never chooses what
|
||||
// the server sees. session, when given, is the proxy's derived session id
|
||||
// (AuthStore.sessionId), set only where the server keys elevation by it.
|
||||
export function withServerToken(headers, token = serverToken(), session = "") {
|
||||
const result = { ...headers };
|
||||
for (const name of Object.keys(result))
|
||||
if (name.toLowerCase() === SERVER_TOKEN_HEADER) delete result[name];
|
||||
for (const name of Object.keys(result)) {
|
||||
const lower = name.toLowerCase();
|
||||
if (lower === SERVER_TOKEN_HEADER || lower === SESSION_HEADER) delete result[name];
|
||||
}
|
||||
if (token) result[SERVER_TOKEN_HEADER] = token;
|
||||
if (token && session) result[SESSION_HEADER] = session;
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,184 @@
|
||||
// The proxy tells the Pipelines server which web session a /connect upgrade or
|
||||
// an upload belongs to (X-Pipelines-Session), so step-up elevation lasts for
|
||||
// the sign-in instead of one connection. The id is derived, never the cookie.
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, rename, rm, writeFile } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { once } from "node:events";
|
||||
import { AuthStore, hashPassword } from "../src/authStore.js";
|
||||
import { SESSION_HEADER, withServerToken } from "../src/serverToken.js";
|
||||
|
||||
const here = fileURLToPath(new URL("..", import.meta.url));
|
||||
const PASSWORD = "tulip orbit canyon ember";
|
||||
const CHEAP = { N: 1024, r: 8, p: 1, keyLength: 32 };
|
||||
const SERVER_TOKEN = "t".repeat(48);
|
||||
|
||||
async function scratch(t) {
|
||||
const dir = await mkdtemp(join(tmpdir(), "pipelines-session-id-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
return dir;
|
||||
}
|
||||
|
||||
const replace = async (file, data) => {
|
||||
await writeFile(`${file}.next`, JSON.stringify(data), { mode: 0o600 });
|
||||
await rename(`${file}.next`, file);
|
||||
};
|
||||
|
||||
test("sessionId is stable per token and changes with logout, revoke-all and a password change", async (t) => {
|
||||
const file = join(await scratch(t), "web-auth.json");
|
||||
const data = { password_hash: hashPassword(PASSWORD, CHEAP), secret: "s".repeat(40), generation: 2 };
|
||||
await writeFile(file, JSON.stringify(data), { mode: 0o600 });
|
||||
let now = 1_800_000_000_000;
|
||||
const store = new AuthStore(file, { now: () => now, log: () => {} });
|
||||
const a = store.issue(3600).token;
|
||||
const b = store.issue(3600).token;
|
||||
const id = store.sessionId(a);
|
||||
assert.match(id, /^[A-Za-z0-9_-]{43}$/);
|
||||
assert.equal(store.sessionId(a), id);
|
||||
assert.notEqual(store.sessionId(b), id);
|
||||
assert.ok(!id.includes(a.split(".")[4]), "the id does not reveal the token id");
|
||||
assert.equal(store.sessionId("forged"), "");
|
||||
// Logout: the token stops naming a session, a new sign-in is a new id.
|
||||
store.revoke(a);
|
||||
assert.equal(store.sessionId(a), "");
|
||||
// Revoke-all: every older token is gone.
|
||||
now += 1000;
|
||||
store.revokeAll();
|
||||
assert.equal(store.sessionId(b), "");
|
||||
now += 1000;
|
||||
const c = store.issue(3600).token;
|
||||
const before = store.sessionId(c);
|
||||
assert.ok(before && before !== id);
|
||||
// A password change (new generation and secret) ends c too.
|
||||
await replace(file, { ...data, secret: "n".repeat(40), generation: 3 });
|
||||
assert.equal(store.sessionId(c), "");
|
||||
assert.notEqual(store.sessionId(store.issue(3600).token), before);
|
||||
});
|
||||
|
||||
test("withServerToken drops a client-supplied session id and sets the proxy's own", () => {
|
||||
const forged = { "X-Pipelines-Session": "forged", cookie: "pipelines_session=v2.x" };
|
||||
assert.deepEqual(withServerToken(forged, SERVER_TOKEN), { cookie: "pipelines_session=v2.x", "x-pipelines-token": SERVER_TOKEN });
|
||||
assert.equal(withServerToken(forged, SERVER_TOKEN, "derived")[SESSION_HEADER], "derived");
|
||||
assert.equal(withServerToken(forged, "", "derived")[SESSION_HEADER], undefined);
|
||||
});
|
||||
|
||||
async function freePort() {
|
||||
const server = net.createServer().listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
const { port } = server.address();
|
||||
await new Promise((done) => server.close(done));
|
||||
return port;
|
||||
}
|
||||
|
||||
/** A stand-in server that records each request's head and answers it. */
|
||||
async function recordingUpstream(t) {
|
||||
const heads = [];
|
||||
const held = new Set();
|
||||
const server = net.createServer((socket) => {
|
||||
held.add(socket);
|
||||
socket.on("error", () => {});
|
||||
socket.on("close", () => held.delete(socket));
|
||||
let buffered = "";
|
||||
socket.on("data", (chunk) => {
|
||||
if (buffered.includes("\r\n\r\n")) return;
|
||||
buffered += chunk.toString("latin1");
|
||||
const end = buffered.indexOf("\r\n\r\n");
|
||||
if (end < 0) return;
|
||||
const [line, ...rest] = buffered.slice(0, end).split("\r\n");
|
||||
const headers = Object.fromEntries(rest.map((h) => [h.slice(0, h.indexOf(":")).toLowerCase(), h.slice(h.indexOf(":") + 1).trim()]));
|
||||
heads.push({ line, headers });
|
||||
if (/^upgrade$/i.test(headers.connection ?? "") || headers.upgrade)
|
||||
socket.write("HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n\r\n");
|
||||
else socket.end("HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\nok");
|
||||
});
|
||||
}).listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
t.after(() => { for (const socket of held) socket.destroy(); return new Promise((done) => server.close(done)); });
|
||||
return { port: server.address().port, heads };
|
||||
}
|
||||
|
||||
async function startProxy(t, authData) {
|
||||
const dir = await scratch(t);
|
||||
const auth = join(dir, "web-auth.json");
|
||||
await writeFile(auth, JSON.stringify(authData), { mode: 0o600 });
|
||||
const tokenFile = join(dir, "server-token");
|
||||
await writeFile(tokenFile, SERVER_TOKEN, { mode: 0o600 });
|
||||
const upstream = await recordingUpstream(t);
|
||||
const port = await freePort();
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port), "--root", join(dir, "absent"),
|
||||
"--pipelines", `127.0.0.1:${upstream.port}`], {
|
||||
cwd: here,
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth, PIPELINES_SERVER_TOKEN_FILE: tokenFile, PIPELINES_WEB_SWEEP_MS: "200" },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
t.after(async () => { const exited = once(child, "exit"); child.kill(); await exited; });
|
||||
await once(child.stdout, "data");
|
||||
return { auth, port, base: `http://127.0.0.1:${port}`, host: `127.0.0.1:${port}`, heads: upstream.heads };
|
||||
}
|
||||
|
||||
const login = (proxy, password = PASSWORD) =>
|
||||
fetch(proxy.base + "/login", { method: "POST", body: new URLSearchParams({ password, next: "/" }), redirect: "manual" });
|
||||
const cookieOf = (response) => response.headers.get("set-cookie").split(";")[0];
|
||||
|
||||
/** The session id the upstream saw on one /connect through the proxy. */
|
||||
async function connectId(proxy, cookie) {
|
||||
const socket = net.connect(proxy.port, "127.0.0.1");
|
||||
await once(socket, "connect");
|
||||
socket.on("error", () => {});
|
||||
socket.write(`GET /connect HTTP/1.1\r\nHost: ${proxy.host}\r\nUpgrade: websocket\r\nConnection: Upgrade\r\n` +
|
||||
`Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\nSec-WebSocket-Version: 13\r\n` +
|
||||
`X-Pipelines-Session: forged-by-the-client-0123456789\r\nCookie: ${cookie}\r\n\r\n`);
|
||||
const [chunk] = await once(socket, "data");
|
||||
assert.match(chunk.toString(), /^HTTP\/1.1 101/);
|
||||
socket.destroy();
|
||||
const head = proxy.heads.at(-1);
|
||||
assert.match(head.line, /^GET \/connect /);
|
||||
return head.headers[SESSION_HEADER];
|
||||
}
|
||||
|
||||
async function uploadHead(proxy, cookie, method = "POST") {
|
||||
const response = await fetch(proxy.base + "/files?path=~/x", {
|
||||
method,
|
||||
body: method === "POST" ? "x" : undefined,
|
||||
headers: { Cookie: cookie, "X-Pipelines-Session": "forged-by-the-client-0123456789" },
|
||||
});
|
||||
await response.text();
|
||||
return proxy.heads.at(-1);
|
||||
}
|
||||
|
||||
test("the proxy forwards a derived session id on /connect and uploads, never the cookie", async (t) => {
|
||||
const data = { password_hash: hashPassword(PASSWORD), secret: "s".repeat(40), generation: 0 };
|
||||
const proxy = await startProxy(t, data);
|
||||
const cookie = cookieOf(await login(proxy));
|
||||
const token = cookie.slice(cookie.indexOf("=") + 1);
|
||||
const id = await connectId(proxy, cookie);
|
||||
assert.match(id, /^[A-Za-z0-9_-]{43}$/);
|
||||
assert.notEqual(id, "forged-by-the-client-0123456789");
|
||||
assert.ok(!id.includes(token) && !token.includes(id));
|
||||
// Every reconnect of this sign-in names the same session.
|
||||
assert.equal(await connectId(proxy, cookie), id);
|
||||
const upload = await uploadHead(proxy, cookie);
|
||||
assert.match(upload.line, /^POST \/files/);
|
||||
assert.equal(upload.headers[SESSION_HEADER], id);
|
||||
// Only uploads carry it; a download does not.
|
||||
assert.equal((await uploadHead(proxy, cookie, "GET")).headers[SESSION_HEADER], undefined);
|
||||
// Another sign-in is another session.
|
||||
const other = cookieOf(await login(proxy));
|
||||
assert.notEqual(await connectId(proxy, other), id);
|
||||
// Logout ends the old id; the next sign-in has a new one.
|
||||
await fetch(proxy.base + "/logout", { method: "POST", headers: { Cookie: cookie, Origin: proxy.base }, redirect: "manual" });
|
||||
const again = cookieOf(await login(proxy));
|
||||
const afterLogout = await connectId(proxy, again);
|
||||
assert.notEqual(afterLogout, id);
|
||||
// A password change: the old cookie is refused, a new sign-in is a new id.
|
||||
await replace(proxy.auth, { ...data, password_hash: hashPassword("ember canyon orbit tulip"), secret: "n".repeat(40), generation: 1 });
|
||||
await new Promise((done) => setTimeout(done, 300));
|
||||
const changed = cookieOf(await login(proxy, "ember canyon orbit tulip"));
|
||||
const afterChange = await connectId(proxy, changed);
|
||||
assert.ok(![id, afterLogout].includes(afterChange));
|
||||
});
|
||||
Reference in New Issue
Block a user