Merge browser discovery and authenticated relay foundation
This commit is contained in:
@@ -23,21 +23,27 @@ class SavedServerRow extends StatelessWidget {
|
||||
required this.selected,
|
||||
required this.busy,
|
||||
required this.onAction,
|
||||
this.allowSelection = true,
|
||||
this.primaryContext = false,
|
||||
});
|
||||
|
||||
final bool allowSelection, primaryContext;
|
||||
final SavedServer server;
|
||||
final bool selected;
|
||||
final bool busy;
|
||||
final ValueChanged<SavedServerAction> onAction;
|
||||
|
||||
String _label(SavedServerAction action) => switch (action) {
|
||||
SavedServerAction.use => 'Use ${server.name}',
|
||||
SavedServerAction.use =>
|
||||
primaryContext
|
||||
? 'Use ${server.name} for pipelines and runs'
|
||||
: 'Use ${server.name}',
|
||||
SavedServerAction.edit => 'Edit ${server.name}',
|
||||
SavedServerAction.remove => 'Remove ${server.name}',
|
||||
};
|
||||
|
||||
List<SavedServerAction> get _actions => <SavedServerAction>[
|
||||
if (!selected) SavedServerAction.use,
|
||||
if (allowSelection && !selected) SavedServerAction.use,
|
||||
SavedServerAction.edit,
|
||||
SavedServerAction.remove,
|
||||
];
|
||||
@@ -87,7 +93,9 @@ class SavedServerRow extends StatelessWidget {
|
||||
PopupMenuItem(value: action, child: Text(_label(action))),
|
||||
],
|
||||
);
|
||||
final Widget mark = selected
|
||||
final Widget mark = !allowSelection
|
||||
? Icon(cupertino ? CupertinoIcons.desktopcomputer : Icons.computer)
|
||||
: selected
|
||||
? Icon(
|
||||
cupertino
|
||||
? CupertinoIcons.checkmark_circle_fill
|
||||
@@ -109,13 +117,18 @@ class SavedServerRow extends StatelessWidget {
|
||||
subtitle: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: <Widget>[
|
||||
if (selected) const Text('Selected server'),
|
||||
if (selected)
|
||||
Text(
|
||||
primaryContext ? 'Pipelines and runs server' : 'Selected server',
|
||||
),
|
||||
for (final (index, address) in server.endpoints.indexed)
|
||||
Text('${index == 0 ? 'Preferred' : 'Backup $index'}: $address'),
|
||||
],
|
||||
),
|
||||
trailing: menu,
|
||||
onTap: selected || busy ? null : () => onAction(SavedServerAction.use),
|
||||
onTap: !allowSelection || selected || busy
|
||||
? null
|
||||
: () => onAction(SavedServerAction.use),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../core/servers/server_catalog.dart';
|
||||
@@ -21,7 +22,12 @@ class _ServerSaveFailure implements Exception {
|
||||
}
|
||||
|
||||
class SavedServersSection extends StatelessWidget {
|
||||
const SavedServersSection({super.key, required this.connections});
|
||||
const SavedServersSection({
|
||||
super.key,
|
||||
required this.connections,
|
||||
this.allowPrimarySelection,
|
||||
});
|
||||
final bool? allowPrimarySelection;
|
||||
final ServerConnections connections;
|
||||
|
||||
Future<void> _edit(BuildContext context, [SavedServer? server]) async {
|
||||
@@ -100,17 +106,27 @@ class SavedServersSection extends StatelessWidget {
|
||||
builder: (context, _) {
|
||||
final cupertino = usesCupertinoHost(context);
|
||||
final error = connections.error;
|
||||
final allowSelection = allowPrimarySelection ?? !kIsWeb;
|
||||
return SettingsGroup(
|
||||
title: 'Servers',
|
||||
leadingRows: true,
|
||||
footer: error == null
|
||||
? null
|
||||
: Text(error, style: TextStyle(color: settingsErrorColor(context))),
|
||||
footer: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.start,
|
||||
children: [
|
||||
const Text('All saved machines connect for chat.'),
|
||||
if (allowSelection)
|
||||
const Text('The primary machine is used for pipelines and runs.'),
|
||||
if (error != null)
|
||||
Text(error, style: TextStyle(color: settingsErrorColor(context))),
|
||||
],
|
||||
),
|
||||
children: [
|
||||
for (final server in connections.catalog.servers)
|
||||
SavedServerRow(
|
||||
server: server,
|
||||
selected: server.id == connections.catalog.active?.id,
|
||||
allowSelection: allowSelection,
|
||||
primaryContext: true,
|
||||
busy: connections.busy,
|
||||
onAction: (act) => _act(context, server, act),
|
||||
),
|
||||
|
||||
@@ -12,5 +12,15 @@ the discovered name before probing/signing in. Root wires saveServerProfile.
|
||||
|
||||
Verification: 34 focused discovery/onboarding/saved-server tests passed, including
|
||||
name editing, no premature connection, browser errors/duplicates/default ports,
|
||||
and native discovery regressions. Browser captures follow after fixture build;
|
||||
this note does not yet claim rendered or deployed evidence.
|
||||
and native discovery regressions. Analyzer clean (NWAPbG), full-app fixture build passed. Material dark
|
||||
1280px and Cupertino light560px browsercaptures under
|
||||
/tmp/review/browser-discovery-{material,cupertino} were inspected: discovered
|
||||
results and editable selected name/address fit. The fixture served controlled
|
||||
JSON at /api/discovery; actual backend auth/discovery has separate Node/Go tests.
|
||||
No production deployment or device testing in this commit.
|
||||
|
||||
Saved browser server rows no longer offer an exclusive Use action or navigate on
|
||||
tap. All saved machines connect for chat; primary selection is explicitly for
|
||||
pipelines/runs on native hosts. A widget regression covers preservation of the
|
||||
browser's current server identity when another saved machine is tapped. Combined
|
||||
discovery/settings layout suite: 43 tests passed (out.c7dJ7j).
|
||||
|
||||
@@ -2,6 +2,10 @@ import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/servers/server_catalog.dart';
|
||||
import 'package:pipelines_app/core/servers/server_catalog_store.dart';
|
||||
import 'package:pipelines_app/features/settings/server_connections.dart';
|
||||
import 'package:pipelines_app/features/settings/saved_servers_section.dart';
|
||||
import 'package:pipelines_app/features/settings/saved_server_row.dart';
|
||||
import 'package:pipelines_app/features/settings/saved_server_editor.dart';
|
||||
import 'package:pipelines_app/features/onboarding/discovery/browser_server_discovery.dart';
|
||||
import 'package:pipelines_app/features/onboarding/onboarding_flow.dart';
|
||||
@@ -20,6 +24,49 @@ BrowserServerDiscovery discovery() => BrowserServerDiscovery(
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets(
|
||||
'browser server rows cannot navigate away by selecting one machine',
|
||||
(tester) async {
|
||||
final home = SavedServer.validated(
|
||||
id: 'home',
|
||||
name: 'Home',
|
||||
endpoints: ['wss://home.test/connect'],
|
||||
);
|
||||
final other = SavedServer.validated(
|
||||
id: 'other',
|
||||
name: 'Other',
|
||||
endpoints: ['wss://other.test/connect'],
|
||||
);
|
||||
final connections = ServerConnections(
|
||||
store: MemoryServerCatalogStore(
|
||||
catalog: ServerCatalog(servers: [home, other], selectedId: home.id),
|
||||
),
|
||||
onSelected: (_) async =>
|
||||
fail('Browser must keep all chat connections in this page'),
|
||||
);
|
||||
addTearDown(connections.dispose);
|
||||
await connections.load();
|
||||
await tester.pumpWidget(
|
||||
MaterialApp(
|
||||
home: Scaffold(
|
||||
body: SavedServersSection(
|
||||
connections: connections,
|
||||
allowPrimarySelection: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
await tester.tap(find.text('Other'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(connections.catalog.active!.id, 'home');
|
||||
await tester.tap(find.byKey(savedServerMenuKey('other')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.textContaining('Use Other'), findsNothing);
|
||||
expect(find.text('Edit Other'), findsOneWidget);
|
||||
expect(find.text('Remove Other'), findsOneWidget);
|
||||
},
|
||||
);
|
||||
|
||||
testWidgets(
|
||||
'choosing a discovered machine prefills editable name and address',
|
||||
(tester) async {
|
||||
|
||||
@@ -181,7 +181,7 @@ void main() {
|
||||
);
|
||||
|
||||
expect(find.byType(SavedServerRow), findsNWidgets(2));
|
||||
expect(find.text('Selected server'), findsOneWidget);
|
||||
expect(find.text('Pipelines and runs server'), findsOneWidget);
|
||||
expect(
|
||||
find.byWidgetPredicate(
|
||||
(widget) => widget is Icon && widget.semanticLabel == 'Selected',
|
||||
|
||||
+10
@@ -1265,3 +1265,13 @@ has a 20-second context, bounded Git output/time and file traversal, and at most
|
||||
100 deletion candidates. Partial scans cannot claim that the storage target has
|
||||
been reached. Cleanup refusals and limits remain visible rather than discarding
|
||||
protected data to satisfy a quota.
|
||||
|
||||
|
||||
### LAN server discovery
|
||||
|
||||
`{"type":"discover_servers"}` returns `discovered_servers` with a
|
||||
`discovered_servers` array of `{name,endpoint,auth,version}` records. The server
|
||||
browses `_pipelines._tcp.local` on its own LAN, with a bounded DNS-SD query; it
|
||||
does not scan arbitrary ports. An unavailable browse returns a protocol error.
|
||||
The authenticated web front door exposes the same result at `GET /api/discovery`,
|
||||
including before the browser has opened its application websocket.
|
||||
|
||||
+14
-2
@@ -319,8 +319,20 @@ the installer rather than being reset.
|
||||
The same password is given to the ntfy `phone-reader` account when that account
|
||||
is first created; existing ntfy accounts keep their passwords.
|
||||
|
||||
To change the web password, edit `password` in `web-auth.json` (keep it 0600)
|
||||
and run `pipelines restart`.
|
||||
After updating the installation, change the web sign-in password from the
|
||||
checkout with:
|
||||
|
||||
```sh
|
||||
./password
|
||||
```
|
||||
|
||||
It prompts twice without echoing the password, requires at least eight
|
||||
characters, and writes the private password file atomically. The updated web
|
||||
proxy picks it up on the next request; no service restart is needed, and existing
|
||||
sessions and running tasks remain connected. SSH is supported with an interactive
|
||||
TTY. This changes the web/app sign-in password; the ntfy notification account's
|
||||
password is unchanged. `--prefix PATH` selects another installation, and
|
||||
`PIPELINES_WEB_AUTH_FILE` selects an explicitly configured password file.
|
||||
|
||||
## Upgrading
|
||||
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
# Browser discovery and simultaneous server connections
|
||||
|
||||
The server adds a bounded `discover_servers` RPC and local `/discovery` HTTP
|
||||
handler using its existing mDNS dependency. Advertisements are validated, sorted
|
||||
and deduplicated; only valid Pipelines `/connect` records are returned. The web
|
||||
front door exposes this as authenticated `GET /api/discovery`. This searches the
|
||||
hosting machine's LAN, which can differ from the browser's current network.
|
||||
|
||||
For browser federation, authenticated same-origin `POST /api/peers/:serverId`
|
||||
accepts `{endpoints:[wsURLs],password?:string}` and responds with `connected`,
|
||||
`needs_password`, and successful `connected_endpoints`. It signs into each remote
|
||||
machine directly, retaining cookies only in proxy memory, scoped to the local
|
||||
login session, server id and exact registered alias. `DELETE` revokes that server.
|
||||
A proxy restart requires signing into remote peers again; remote harness sessions
|
||||
and background work are independent and are not stopped by this relay.
|
||||
|
||||
The browser opens `/peers/:serverId/connect?endpoint=<original ws URL>` and reads
|
||||
files/artifacts/updates or uploads through `/peers/:serverId/<resource>`, using the
|
||||
same optional `endpoint` query. These are same-origin paths, so HTTPS browsers can
|
||||
reach trusted LAN servers without cross-origin cookies or mixed-content sockets.
|
||||
Local browser cookies and Origin are never forwarded to remote machines; only the
|
||||
selected peer's exact stored credential is used. No redirects are followed, no
|
||||
remote Set-Cookie is exposed, and proxying is limited to registered endpoints and
|
||||
known application resource paths. Mutating registration calls require same Origin
|
||||
and JSON, and all endpoints remain behind the local login gate.
|
||||
|
||||
New local sessions have unique nonce-bearing signed cookies, while existing
|
||||
legacy cookies remain valid. Profile edits revoke previous aliases immediately;
|
||||
registration generations prevent late responses resurrecting deleted profiles.
|
||||
Failed reauthentication to an unchanged profile preserves its existing cookie and
|
||||
websocket. Expired owner records close their sockets and are pruned before limits.
|
||||
|
||||
Verification: all 13 Node tests passed, including isolated real HTTP/WS peers,
|
||||
per-browser and per-server cookie isolation, scoped files and websocket routing,
|
||||
authenticated discovery, revocation races, failed sign-in preservation, and owner
|
||||
expiry cleanup. Go discovery/protocol/server tests and focused vet passed. All
|
||||
fixtures used loopback/scratch state; no operator sessions, passwords, deployments
|
||||
or workflows were changed. Parent owns Flutter integration and delivery.
|
||||
@@ -0,0 +1,24 @@
|
||||
# Password command
|
||||
|
||||
Added checkout entry point `./password` with twice-entered hidden input and an
|
||||
8-character minimum. It uses the existing installation's recorded password file,
|
||||
explicit PIPELINES_WEB_AUTH_FILE, or default private file for partial installs.
|
||||
Password arguments are not accepted, and non-TTY use is refused before prompting.
|
||||
|
||||
The helper validates file ownership, regular-file type, permissions and auth JSON;
|
||||
stages on the destination filesystem, fsyncs content and atomically replaces the
|
||||
file. Concurrent helper updates serialize on the parent directory lock, and an
|
||||
external change detected during staging is not overwritten. Cookie secret and
|
||||
unknown metadata remain unchanged, preserving existing sessions. No service,
|
||||
background task, provider credential or ntfy account is modified.
|
||||
|
||||
Runtime use requires the updated proxy's per-request auth reload, implemented in
|
||||
the browser-discovery/relay lane. Merely updating a checkout does not replace an
|
||||
already-running installed proxy. The helper does not kill a proxy: the existing
|
||||
supervisor would treat that as a fatal service exit.
|
||||
|
||||
Verification: 11 focused tests passed, covering custom/default/partial-install
|
||||
paths, private ownership boundaries, old-file preservation on errors, same-device
|
||||
atomic replacement, external-write refusal, prompt retry/privacy and non-TTY
|
||||
refusal. Wrapper help, shell syntax, Python compilation and diff checks passed.
|
||||
No operator password or running service was changed during testing.
|
||||
@@ -0,0 +1,113 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
)
|
||||
|
||||
// FoundServer is a validated service advertised on the serving host's LAN.
|
||||
type FoundServer struct {
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Auth string `json:"auth"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
|
||||
// Browse sends one bounded DNS-SD query, never a network port scan.
|
||||
func Browse(ctx context.Context) ([]FoundServer, error) {
|
||||
return browse(ctx, mdns.QueryContext)
|
||||
}
|
||||
|
||||
func browse(ctx context.Context, query func(context.Context, *mdns.QueryParam) error) ([]FoundServer, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
entries := make(chan *mdns.ServiceEntry, 128)
|
||||
result := make(chan error, 1)
|
||||
go func() {
|
||||
result <- query(ctx, &mdns.QueryParam{Service: ServiceType, Domain: "local", Timeout: 3 * time.Second,
|
||||
Entries: entries, WantUnicastResponse: true, Logger: log.New(io.Discard, "", 0)})
|
||||
}()
|
||||
found := map[string]FoundServer{}
|
||||
consume := func(entry *mdns.ServiceEntry) {
|
||||
if item, ok := discovered(entry); ok && len(found) < 128 {
|
||||
found[item.Endpoint] = item
|
||||
}
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case entry := <-entries:
|
||||
consume(entry)
|
||||
case err := <-result:
|
||||
for {
|
||||
select {
|
||||
case entry := <-entries:
|
||||
consume(entry)
|
||||
default:
|
||||
values := make([]FoundServer, 0, len(found))
|
||||
for _, item := range found {
|
||||
values = append(values, item)
|
||||
}
|
||||
sort.Slice(values, func(i, j int) bool { return values[i].Endpoint < values[j].Endpoint })
|
||||
return values, err
|
||||
}
|
||||
}
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func discovered(entry *mdns.ServiceEntry) (FoundServer, bool) {
|
||||
if entry == nil || entry.Port < 1 || entry.Port > 65535 {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
fields := map[string]string{}
|
||||
for _, value := range entry.InfoFields {
|
||||
if key, text, ok := strings.Cut(value, "="); ok {
|
||||
fields[key] = text
|
||||
}
|
||||
}
|
||||
path := fields["path"]
|
||||
if path != "/connect" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
ip := entry.AddrV4
|
||||
if ip == nil {
|
||||
ip = entry.AddrV6
|
||||
}
|
||||
if ip == nil && entry.AddrV6IPAddr != nil {
|
||||
ip = entry.AddrV6IPAddr.IP
|
||||
}
|
||||
if ip == nil || ip.IsUnspecified() || ip.IsMulticast() || ip.IsLoopback() || ip.IsLinkLocalUnicast() {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
if fields["tls"] != "0" && fields["tls"] != "1" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
scheme := "ws"
|
||||
if fields["tls"] == "1" {
|
||||
scheme = "wss"
|
||||
}
|
||||
auth := fields["auth"]
|
||||
if auth != "password" && auth != "none" {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
name := strings.TrimSpace(fields["name"])
|
||||
if name == "" {
|
||||
name = strings.TrimSuffix(entry.Host, ".")
|
||||
}
|
||||
if name == "" || len(name) > 200 {
|
||||
return FoundServer{}, false
|
||||
}
|
||||
endpoint := (&url.URL{Scheme: scheme, Host: net.JoinHostPort(ip.String(), strconv.Itoa(entry.Port)), Path: path}).String()
|
||||
return FoundServer{Name: name, Endpoint: endpoint, Auth: auth, Version: fields["v"]}, true
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/mdns"
|
||||
)
|
||||
|
||||
func service(ip string) *mdns.ServiceEntry {
|
||||
return &mdns.ServiceEntry{AddrV4: net.ParseIP(ip), Host: "server.local.", Port: 8083,
|
||||
InfoFields: []string{"name=Linux desktop", "path=/connect", "tls=0", "auth=password", "v=release"}}
|
||||
}
|
||||
|
||||
func TestBrowseCollectsDeduplicatedValidAdvertisements(t *testing.T) {
|
||||
values, err := browse(context.Background(), func(ctx context.Context, p *mdns.QueryParam) error {
|
||||
if p.Service != ServiceType || p.Timeout.Seconds() > 4 {
|
||||
t.Fatal("unbounded or wrong service query")
|
||||
}
|
||||
for _, entry := range []*mdns.ServiceEntry{service("192.168.1.3"), service("192.168.1.2"), service("192.168.1.3"), service("127.0.0.1"), nil} {
|
||||
p.Entries <- entry
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil || len(values) != 2 {
|
||||
t.Fatalf("values=%+v err=%v", values, err)
|
||||
}
|
||||
if values[0].Endpoint != "ws://192.168.1.2:8083/connect" || values[0].Name != "Linux desktop" || values[0].Auth != "password" {
|
||||
t.Fatalf("wrong record: %+v", values[0])
|
||||
}
|
||||
}
|
||||
|
||||
func TestBrowseHonorsCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := browse(ctx, func(ctx context.Context, p *mdns.QueryParam) error { <-ctx.Done(); return ctx.Err() })
|
||||
if err == nil {
|
||||
t.Fatal("canceled browse must fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestDiscoveryRejectsMalformedAndUnsafeAdvertisements(t *testing.T) {
|
||||
for _, ip := range []string{"0.0.0.0", "127.0.0.1", "224.0.0.1", "169.254.3.1", "::", "::1", "fe80::1"} {
|
||||
if _, ok := discovered(service(ip)); ok {
|
||||
t.Fatalf("accepted address %s", ip)
|
||||
}
|
||||
}
|
||||
for _, fields := range [][]string{{"path=//evil/connect", "auth=password", "tls=0"}, {"path=/connect", "auth=unknown", "tls=0"}, {"path=/connect", "auth=password", "tls=garbage"}} {
|
||||
item := service("192.168.1.2")
|
||||
item.InfoFields = fields
|
||||
if _, ok := discovered(item); ok {
|
||||
t.Fatalf("accepted invalid TXT %+v", fields)
|
||||
}
|
||||
}
|
||||
item := service("192.168.1.2")
|
||||
item.Port = 65536
|
||||
if _, ok := discovered(item); ok {
|
||||
t.Fatal("accepted invalid port")
|
||||
}
|
||||
}
|
||||
@@ -287,8 +287,9 @@ type Message struct {
|
||||
// AttentionAcks is the ack_attention body, the attention_acks reply (the
|
||||
// whole set) and the attention_acks_changed push (the delta). See
|
||||
// attention_acks.go.
|
||||
AttentionAcks *AttentionAcks `json:"attention_acks,omitempty"`
|
||||
ServerStats *ServerStats `json:"server_stats,omitempty"`
|
||||
AttentionAcks *AttentionAcks `json:"attention_acks,omitempty"`
|
||||
DiscoveredServers []DiscoveredServer `json:"discovered_servers,omitzero"`
|
||||
ServerStats *ServerStats `json:"server_stats,omitempty"`
|
||||
// App notification stream (see notifications.go): After is the
|
||||
// subscribe_notifications cursor, Notifications the subscribe reply,
|
||||
// Notification one push, and IDs the clear_notifications request, the
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
package protocol
|
||||
|
||||
const (
|
||||
DiscoverServersRequest = "discover_servers"
|
||||
DiscoverServersResult = "discovered_servers"
|
||||
)
|
||||
|
||||
// DiscoveredServer is one Pipelines DNS-SD advertisement on the serving LAN.
|
||||
type DiscoveredServer struct {
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Auth string `json:"auth"`
|
||||
Version string `json:"version"`
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"git.a-wan.net/alan/pipelines/internal/discovery"
|
||||
"git.a-wan.net/alan/pipelines/internal/protocol"
|
||||
)
|
||||
|
||||
var discoverySlots = make(chan struct{}, 4)
|
||||
|
||||
func discoverServers(ctx context.Context) ([]discovery.FoundServer, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 4*time.Second)
|
||||
defer cancel()
|
||||
select {
|
||||
case discoverySlots <- struct{}{}:
|
||||
defer func() { <-discoverySlots }()
|
||||
return discovery.Browse(ctx)
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func handleServerDiscovery(ctx context.Context, request protocol.Message, write func(protocol.Message) error) {
|
||||
servers, err := discoverServers(ctx)
|
||||
if err != nil {
|
||||
_ = write(protocol.Message{Type: protocol.Error, ID: request.ID, Error: "LAN discovery unavailable: " + err.Error()})
|
||||
return
|
||||
}
|
||||
items := make([]protocol.DiscoveredServer, 0, len(servers))
|
||||
for _, server := range servers {
|
||||
items = append(items, protocol.DiscoveredServer(server))
|
||||
}
|
||||
_ = write(protocol.Message{Type: protocol.DiscoverServersResult, ID: request.ID, DiscoveredServers: items})
|
||||
}
|
||||
|
||||
func serveServerDiscovery(w http.ResponseWriter, r *http.Request) {
|
||||
servers, err := discoverServers(r.Context())
|
||||
w.Header().Set("Cache-Control", "no-store")
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err != nil {
|
||||
http.Error(w, "LAN discovery unavailable", http.StatusServiceUnavailable)
|
||||
return
|
||||
}
|
||||
_ = json.NewEncoder(w).Encode(struct {
|
||||
Servers []discovery.FoundServer `json:"discovered_servers"`
|
||||
}{servers})
|
||||
}
|
||||
@@ -153,6 +153,7 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
}
|
||||
runs.serveArtifact(w, r)
|
||||
})
|
||||
mux.HandleFunc("GET /discovery", serveServerDiscovery)
|
||||
mux.HandleFunc("GET /files", serveHomeFile)
|
||||
mux.HandleFunc("POST /chat-uploads", serveChatUpload)
|
||||
mux.HandleFunc("GET /healthz", func(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -276,6 +277,10 @@ func HandlerWithOptions(ctx context.Context, options Options, stores ...*pipelin
|
||||
if handleDirectoryListing(request, write) {
|
||||
continue
|
||||
}
|
||||
if request.Type == protocol.DiscoverServersRequest {
|
||||
go handleServerDiscovery(connCtx, request, write)
|
||||
continue
|
||||
}
|
||||
if request.Type == protocol.ServerStatsRequest {
|
||||
go handleServerStats(connCtx, request, runs, options.ServerStatsSampler, started, build, write)
|
||||
continue
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
exec python3 "$(dirname "$0")/scripts/password.py" "$@"
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Change the local Pipelines web sign-in password without restarting services."""
|
||||
import argparse
|
||||
import fcntl
|
||||
import getpass
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import stat
|
||||
import sys
|
||||
import tempfile
|
||||
|
||||
|
||||
def auth_path(*, prefix=None, home=None, environ=None):
|
||||
home = Path(home or Path.home())
|
||||
env = os.environ if environ is None else environ
|
||||
selected = Path(prefix or env.get("PIPELINES_PREFIX") or home / ".local/share/pipelines").expanduser().resolve()
|
||||
explicit = env.get("PIPELINES_WEB_AUTH_FILE")
|
||||
if explicit:
|
||||
return Path(explicit).expanduser().absolute()
|
||||
manifest = selected / "pipelines-install.json"
|
||||
if manifest.exists():
|
||||
if manifest.is_symlink() or manifest.stat().st_uid != os.getuid():
|
||||
raise ValueError("Refusing an unsafe installation manifest")
|
||||
data = json.loads(manifest.read_text())
|
||||
if data.get("format") != 1 or data.get("prefix") != str(selected):
|
||||
raise ValueError("Invalid Pipelines installation manifest")
|
||||
recorded = (data.get("web") or {}).get("auth_file")
|
||||
if recorded:
|
||||
return Path(recorded).expanduser().absolute()
|
||||
return home / ".config/pipelines/web-auth.json"
|
||||
|
||||
|
||||
def read_auth(path):
|
||||
path = Path(path)
|
||||
info = path.lstat()
|
||||
if not stat.S_ISREG(info.st_mode) or info.st_uid != os.getuid():
|
||||
raise ValueError("Password file must be a regular file owned by this user")
|
||||
if info.st_mode & 0o077:
|
||||
raise ValueError(f"Password file must be private: chmod 600 '{path}'")
|
||||
data = json.loads(path.read_text())
|
||||
if not isinstance(data, dict) or not isinstance(data.get("password"), str) or len(data["password"]) < 8 or not isinstance(data.get("secret"), str) or len(data["secret"]) < 32:
|
||||
raise ValueError("Invalid Pipelines password file; it was retained unchanged")
|
||||
return data, info
|
||||
|
||||
|
||||
def change_password(path, password):
|
||||
if len(password) < 8:
|
||||
raise ValueError("Choose a password of at least 8 characters")
|
||||
path = Path(path)
|
||||
# Lock the directory inode, which remains stable across atomic file replaces.
|
||||
directory = os.open(path.parent, os.O_RDONLY)
|
||||
try:
|
||||
fcntl.flock(directory, fcntl.LOCK_EX)
|
||||
data, original = read_auth(path)
|
||||
data["password"] = password
|
||||
descriptor, temporary = tempfile.mkstemp(prefix=".pipelines-password-", dir=path.parent)
|
||||
try:
|
||||
with os.fdopen(descriptor, "w") as output:
|
||||
os.fchmod(output.fileno(), 0o600)
|
||||
json.dump(data, output, indent=2)
|
||||
output.write("\n")
|
||||
output.flush()
|
||||
os.fsync(output.fileno())
|
||||
current = path.lstat()
|
||||
if (current.st_dev, current.st_ino, current.st_mtime_ns, current.st_size) != (original.st_dev, original.st_ino, original.st_mtime_ns, original.st_size):
|
||||
raise ValueError("Password file changed during the update; retry the command")
|
||||
os.replace(temporary, path)
|
||||
finally:
|
||||
Path(temporary).unlink(missing_ok=True)
|
||||
finally:
|
||||
os.close(directory)
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--prefix", type=Path, help="Installation prefix (or PIPELINES_PREFIX)")
|
||||
args = parser.parse_args()
|
||||
path = auth_path(prefix=args.prefix)
|
||||
read_auth(path)
|
||||
if not sys.stdin.isatty():
|
||||
raise ValueError("Run ./password in an interactive terminal (SSH with a TTY is supported)")
|
||||
while True:
|
||||
password = getpass.getpass("New Pipelines password: ")
|
||||
confirmation = getpass.getpass("Confirm new password: ")
|
||||
if len(password) < 8:
|
||||
print("Choose a password of at least 8 characters.", file=sys.stderr)
|
||||
elif password != confirmation:
|
||||
print("Passwords do not match. Try again.", file=sys.stderr)
|
||||
else:
|
||||
break
|
||||
change_password(path, password)
|
||||
print("Web sign-in password changed. No service restart is needed.")
|
||||
print("Existing sessions remain signed in. The notification password is unchanged.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (OSError, ValueError, KeyboardInterrupt) as error:
|
||||
sys.exit(f"Password not changed: {error}")
|
||||
@@ -0,0 +1,124 @@
|
||||
import importlib.util
|
||||
import json
|
||||
import io
|
||||
import os
|
||||
from pathlib import Path
|
||||
import tempfile
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
|
||||
spec = importlib.util.spec_from_file_location("pipelines_password", Path(__file__).with_name("password.py"))
|
||||
password = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(password)
|
||||
|
||||
|
||||
class PasswordTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.temporary = tempfile.TemporaryDirectory(prefix="pipelines-password-test-")
|
||||
self.addCleanup(self.temporary.cleanup)
|
||||
self.home = Path(self.temporary.name).resolve()
|
||||
self.auth = self.home / ".config/pipelines/web-auth.json"
|
||||
self.auth.parent.mkdir(parents=True)
|
||||
self.original = {"password": "old password", "secret": "s" * 32, "other": "preserve"}
|
||||
self.auth.write_text(json.dumps(self.original))
|
||||
self.auth.chmod(0o600)
|
||||
|
||||
def test_default_path_works_for_partial_install_without_manifest(self):
|
||||
self.assertEqual(password.auth_path(home=self.home, environ={}), self.auth)
|
||||
|
||||
def test_manifest_custom_auth_and_explicit_environment(self):
|
||||
prefix = self.home / "installation"
|
||||
prefix.mkdir()
|
||||
recorded = self.home / "custom-auth.json"
|
||||
(prefix / "pipelines-install.json").write_text(json.dumps({
|
||||
"format": 1, "prefix": str(prefix), "web": {"auth_file": str(recorded)}}))
|
||||
self.assertEqual(password.auth_path(home=self.home, prefix=prefix, environ={}), recorded)
|
||||
self.assertEqual(password.auth_path(home=self.home, prefix=prefix,
|
||||
environ={"PIPELINES_WEB_AUTH_FILE": str(self.auth)}), self.auth)
|
||||
|
||||
def test_change_preserves_cookie_secret_metadata_and_private_permissions(self):
|
||||
password.change_password(self.auth, "new password")
|
||||
result = json.loads(self.auth.read_text())
|
||||
self.assertEqual(result, dict(self.original, password="new password"))
|
||||
self.assertEqual(self.auth.stat().st_mode & 0o777, 0o600)
|
||||
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
|
||||
|
||||
def test_short_password_does_not_modify_auth(self):
|
||||
with self.assertRaises(ValueError):
|
||||
password.change_password(self.auth, "short")
|
||||
self.assertEqual(json.loads(self.auth.read_text()), self.original)
|
||||
|
||||
def test_symlinked_password_is_refused_and_target_untouched(self):
|
||||
link = self.home / "link.json"
|
||||
link.symlink_to(self.auth)
|
||||
with self.assertRaises(ValueError):
|
||||
password.change_password(link, "new password")
|
||||
self.assertEqual(json.loads(self.auth.read_text()), self.original)
|
||||
self.assertTrue(link.is_symlink())
|
||||
|
||||
def test_public_or_malformed_auth_is_not_replaced(self):
|
||||
self.auth.chmod(0o644)
|
||||
with self.assertRaisesRegex(ValueError, "private"):
|
||||
password.change_password(self.auth, "new password")
|
||||
self.auth.chmod(0o600)
|
||||
self.auth.write_text("{broken")
|
||||
with self.assertRaises(ValueError):
|
||||
password.change_password(self.auth, "new password")
|
||||
self.assertEqual(self.auth.read_text(), "{broken")
|
||||
|
||||
def test_replacement_failure_preserves_original_and_cleans_staging(self):
|
||||
before = self.auth.read_bytes()
|
||||
with patch.object(password.os, "replace", side_effect=OSError("fixture failure")):
|
||||
with self.assertRaises(OSError):
|
||||
password.change_password(self.auth, "new password")
|
||||
self.assertEqual(self.auth.read_bytes(), before)
|
||||
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
|
||||
|
||||
def test_atomic_replace_occurs_on_destination_filesystem(self):
|
||||
replace = os.replace
|
||||
calls = []
|
||||
def checked_replace(source, target):
|
||||
calls.append((source, target))
|
||||
self.assertEqual(Path(source).parent, self.auth.parent)
|
||||
return replace(source, target)
|
||||
with patch.object(password.os, "replace", side_effect=checked_replace):
|
||||
password.change_password(self.auth, "new password")
|
||||
self.assertEqual(len(calls), 1)
|
||||
|
||||
def test_file_changed_during_update_is_not_overwritten(self):
|
||||
replace = os.fsync
|
||||
def external_update(descriptor):
|
||||
self.auth.write_text(json.dumps(dict(self.original, password="changed elsewhere")))
|
||||
return replace(descriptor)
|
||||
with patch.object(password.os, "fsync", side_effect=external_update):
|
||||
with self.assertRaisesRegex(ValueError, "changed during"):
|
||||
password.change_password(self.auth, "new password")
|
||||
self.assertEqual(json.loads(self.auth.read_text())["password"], "changed elsewhere")
|
||||
self.assertEqual(list(self.auth.parent.glob(".pipelines-password-*")), [])
|
||||
|
||||
def test_cli_retries_mismatch_and_does_not_print_password(self):
|
||||
output = io.StringIO()
|
||||
errors = io.StringIO()
|
||||
with patch.object(password.sys, "argv", ["password"]), \
|
||||
patch.object(password, "auth_path", return_value=self.auth), \
|
||||
patch.object(password.sys.stdin, "isatty", return_value=True), \
|
||||
patch.object(password.getpass, "getpass", side_effect=[
|
||||
"first password", "wrong confirmation", "new password", "new password"]), \
|
||||
patch.object(password.sys, "stdout", output), \
|
||||
patch.object(password.sys, "stderr", errors):
|
||||
password.main()
|
||||
self.assertEqual(json.loads(self.auth.read_text())["password"], "new password")
|
||||
self.assertIn("do not match", errors.getvalue())
|
||||
self.assertIn("No service restart", output.getvalue())
|
||||
for value in ("first password", "wrong confirmation", "new password", "old password"):
|
||||
self.assertNotIn(value, output.getvalue() + errors.getvalue())
|
||||
|
||||
def test_cli_refuses_non_tty_before_prompting(self):
|
||||
with patch.object(password.sys, "argv", ["password"]), \
|
||||
patch.object(password, "auth_path", return_value=self.auth), \
|
||||
patch.object(password.sys.stdin, "isatty", return_value=False), \
|
||||
patch.object(password.getpass, "getpass") as prompt:
|
||||
with self.assertRaisesRegex(ValueError, "interactive terminal"):
|
||||
password.main()
|
||||
prompt.assert_not_called()
|
||||
self.assertEqual(json.loads(self.auth.read_text()), self.original)
|
||||
+34
-11
@@ -18,7 +18,7 @@
|
||||
//
|
||||
// The default host is 127.0.0.1. Use --host 0.0.0.0 only on a trusted LAN/VPN.
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import { createHmac, timingSafeEqual, randomBytes } from "node:crypto";
|
||||
import { createReadStream, readFileSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { readFile, stat } from "node:fs/promises";
|
||||
@@ -26,6 +26,7 @@ import { extname, join, normalize, resolve, sep } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import net from "node:net";
|
||||
import { sameOriginUpgrade } from "./src/proxySecurity.js";
|
||||
import { PeerRelay, proxyDiscovery } from "./src/peerRelay.js";
|
||||
|
||||
// The proxy's own directory holds the sign-in page's icons.
|
||||
const HERE = resolve(fileURLToPath(new URL(".", import.meta.url)));
|
||||
@@ -47,7 +48,7 @@ const AUTH_FILE =
|
||||
process.env.PIPELINES_WEB_AUTH_FILE ?? join(homedir(), ".config", "pipelines", "web-auth.json");
|
||||
const SESSION_COOKIE = "pipelines_session";
|
||||
|
||||
function loadAuth() {
|
||||
function loadAuth(fatal = true) {
|
||||
if (AUTH_DISABLED) return null;
|
||||
try {
|
||||
const parsed = JSON.parse(readFileSync(AUTH_FILE, "utf8"));
|
||||
@@ -62,12 +63,13 @@ function loadAuth() {
|
||||
} catch {
|
||||
// Fall through to the refusal below.
|
||||
}
|
||||
console.error(
|
||||
`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`,
|
||||
);
|
||||
if (!fatal) throw new Error("Web authentication is temporarily unavailable");
|
||||
console.error(`web auth: refusing to start without ${AUTH_FILE} (or PIPELINES_WEB_AUTH=disabled for tests)`);
|
||||
process.exit(1);
|
||||
}
|
||||
const auth = loadAuth();
|
||||
let auth = loadAuth();
|
||||
|
||||
function refreshAuth() { auth = loadAuth(false); }
|
||||
|
||||
function cookieValue(req, name) {
|
||||
for (const part of String(req.headers.cookie ?? "").split(";")) {
|
||||
@@ -85,7 +87,8 @@ function cookieValue(req, name) {
|
||||
}
|
||||
|
||||
function signSession(expires) {
|
||||
return `${expires}.${createHmac("sha256", auth.secret).update(String(expires)).digest("base64url")}`;
|
||||
const payload = `${expires}.${randomBytes(18).toString("base64url")}`;
|
||||
return `${payload}.${createHmac("sha256", auth.secret).update(payload).digest("base64url")}`;
|
||||
}
|
||||
|
||||
const SESSION_LONG_SECONDS = 30 * 24 * 60 * 60;
|
||||
@@ -98,9 +101,11 @@ function sessionToken(remember = false) {
|
||||
|
||||
function validSession(value) {
|
||||
if (!auth || typeof value !== "string") return false;
|
||||
const [expires, signature] = value.split(".");
|
||||
if (!expires || !signature) return false;
|
||||
const expected = createHmac("sha256", auth.secret).update(expires).digest();
|
||||
const parts = value.split(".");
|
||||
if (parts.length !== 2 && parts.length !== 3) return false;
|
||||
const expires = parts[0], signature = parts.at(-1);
|
||||
if (!/^\d+$/.test(expires) || !signature) return false;
|
||||
const expected = createHmac("sha256", auth.secret).update(parts.slice(0, -1).join(".")).digest();
|
||||
let given;
|
||||
try {
|
||||
given = Buffer.from(signature, "base64url");
|
||||
@@ -115,6 +120,8 @@ function isAuthed(req) {
|
||||
return !auth || validSession(cookieValue(req, SESSION_COOKIE));
|
||||
}
|
||||
|
||||
const peerRelay = new PeerRelay((req) => AUTH_DISABLED ? "test-auth-disabled" : cookieValue(req, SESSION_COOKIE));
|
||||
|
||||
function passwordOk(candidate) {
|
||||
if (!auth) return true;
|
||||
const given = Buffer.from(String(candidate ?? ""));
|
||||
@@ -387,6 +394,10 @@ function proxyArtifact(req, res) {
|
||||
}
|
||||
|
||||
const server = createServer(async (req, res) => {
|
||||
try { refreshAuth(); } catch {
|
||||
res.writeHead(503, { "Content-Type": "text/plain", "Cache-Control": "no-store" }).end("Web authentication unavailable");
|
||||
return;
|
||||
}
|
||||
const pathname = (req.url ?? "/").split("?")[0];
|
||||
// Icons and the web manifest are public: the sign-in page shows the icon,
|
||||
// and browsers and app installers fetch them without the session cookie.
|
||||
@@ -434,12 +445,13 @@ const server = createServer(async (req, res) => {
|
||||
res.writeHead(303, { Location: safeNext(fields.get("next")) }).end();
|
||||
return;
|
||||
} else if (pathname === "/logout") {
|
||||
peerRelay.forget(req);
|
||||
res.setHeader("Set-Cookie", `${SESSION_COOKIE}=; Path=/; HttpOnly; SameSite=Strict; Max-Age=0`);
|
||||
res.writeHead(303, { Location: "/login" }).end();
|
||||
return;
|
||||
} else if (!isAuthed(req)) {
|
||||
const wantsHtml = String(req.headers.accept ?? "").includes("text/html");
|
||||
if (req.method === "GET" && wantsHtml) {
|
||||
if (req.method === "GET" && wantsHtml && !pathname.startsWith("/api/") && !pathname.startsWith("/peers/")) {
|
||||
res.writeHead(302, { Location: `/login?next=${encodeURIComponent(req.url ?? "/")}` }).end();
|
||||
return;
|
||||
}
|
||||
@@ -449,6 +461,12 @@ const server = createServer(async (req, res) => {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (pathname === "/api/discovery" && req.method === "GET") {
|
||||
await proxyDiscovery(req, res, targetHost, targetPort);
|
||||
return;
|
||||
}
|
||||
if (await peerRelay.api(req, res)) return;
|
||||
if (peerRelay.http(req, res)) return;
|
||||
if (pathname.startsWith("/artifacts/") || pathname === "/files") {
|
||||
proxyArtifact(req, res);
|
||||
return;
|
||||
@@ -543,6 +561,10 @@ function proxyUpgrade(req, clientSocket, clientHead) {
|
||||
}
|
||||
|
||||
server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
try { refreshAuth(); } catch {
|
||||
clientSocket.end("HTTP/1.1 503 Service Unavailable\r\nContent-Length: 0\r\n\r\n");
|
||||
return;
|
||||
}
|
||||
const pathname = (req.url ?? "").split("?")[0];
|
||||
if (!sameOriginUpgrade(req.headers.origin, req.headers.host)) {
|
||||
clientSocket.write("HTTP/1.1 403 Forbidden\r\nConnection: close\r\nContent-Length: 0\r\n\r\n");
|
||||
@@ -554,6 +576,7 @@ server.on("upgrade", (req, clientSocket, clientHead) => {
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
}
|
||||
if (peerRelay.upgrade(req, clientSocket, clientHead)) return;
|
||||
if (pathname !== "/connect") {
|
||||
clientSocket.destroy();
|
||||
return;
|
||||
|
||||
@@ -0,0 +1,180 @@
|
||||
import { createHash } from "node:crypto";
|
||||
import { authenticate, endpoint, json } from "./peerTransport.js";
|
||||
import { sameOriginUpgrade } from "./proxySecurity.js";
|
||||
|
||||
async function body(req) {
|
||||
const chunks = [];
|
||||
let size = 0;
|
||||
for await (const chunk of req) {
|
||||
size += chunk.length;
|
||||
if (size > 65536) throw new Error("Request too large");
|
||||
chunks.push(chunk);
|
||||
}
|
||||
return JSON.parse(Buffer.concat(chunks).toString("utf8"));
|
||||
}
|
||||
|
||||
export class PeerRegistry {
|
||||
constructor(scopeFor) {
|
||||
this.scopeFor = scopeFor;
|
||||
this.sessions = new Map();
|
||||
}
|
||||
owner(req) {
|
||||
const token = this.scopeFor(req);
|
||||
if (!token) throw new Error("Sign in first");
|
||||
const key = createHash("sha256").update(token).digest("hex");
|
||||
for (const [old, value] of this.sessions) {
|
||||
if (Date.now() - value.used > 12 * 3600000) {
|
||||
for (const peer of value.peers.values())
|
||||
for (const socket of peer.sockets) socket.destroy();
|
||||
this.sessions.delete(old);
|
||||
}
|
||||
}
|
||||
if (!this.sessions.has(key)) {
|
||||
if (this.sessions.size >= 256)
|
||||
throw new Error("Too many browser sessions");
|
||||
this.sessions.set(key, {
|
||||
peers: new Map(),
|
||||
versions: new Map(),
|
||||
used: Date.now(),
|
||||
});
|
||||
}
|
||||
const owner = this.sessions.get(key);
|
||||
owner.used = Date.now();
|
||||
return owner;
|
||||
}
|
||||
forget(req) {
|
||||
try {
|
||||
const token = this.scopeFor(req);
|
||||
if (!token) return;
|
||||
const key = createHash("sha256").update(token).digest("hex");
|
||||
const owner = this.sessions.get(key);
|
||||
if (!owner) return;
|
||||
for (const peer of owner.peers.values())
|
||||
for (const socket of peer.sockets) socket.destroy();
|
||||
this.sessions.delete(key);
|
||||
} catch {}
|
||||
}
|
||||
async api(req, res) {
|
||||
const parsed = new URL(req.url, "http://localhost");
|
||||
const match = /^\/api\/peers\/([^/]+)$/.exec(parsed.pathname);
|
||||
if (!match) return false;
|
||||
if (
|
||||
!sameOriginUpgrade(req.headers.origin, req.headers.host) ||
|
||||
(["POST", "DELETE"].includes(req.method) && !req.headers.origin)
|
||||
) {
|
||||
json(res, 403, { error: "Same-origin request required" });
|
||||
return true;
|
||||
}
|
||||
try {
|
||||
const id = decodeURIComponent(match[1]);
|
||||
if (!id || id.length > 256) throw new Error("Invalid server id");
|
||||
const owner = this.owner(req);
|
||||
if (req.method === "DELETE") {
|
||||
owner.versions.set(id, (owner.versions.get(id) ?? 0) + 1);
|
||||
const previous = owner.peers.get(id);
|
||||
if (previous) for (const socket of previous.sockets) socket.destroy();
|
||||
owner.peers.delete(id);
|
||||
json(res, 200, { connected: false, needs_password: false });
|
||||
return true;
|
||||
}
|
||||
if (req.method === "GET") {
|
||||
const peer = owner.peers.get(id);
|
||||
json(res, 200, {
|
||||
connected: Boolean(peer?.connected),
|
||||
needs_password: !peer?.connected,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (
|
||||
req.method !== "POST" ||
|
||||
!String(req.headers["content-type"]).startsWith("application/json")
|
||||
) {
|
||||
json(res, 405, { error: "POST JSON required" });
|
||||
return true;
|
||||
}
|
||||
const data = await body(req);
|
||||
if (
|
||||
!Array.isArray(data.endpoints) ||
|
||||
data.endpoints.length < 1 ||
|
||||
data.endpoints.length > 8 ||
|
||||
(data.password !== undefined &&
|
||||
(typeof data.password !== "string" || data.password.length > 4096))
|
||||
)
|
||||
throw new Error("Invalid server addresses");
|
||||
const urls = [
|
||||
...new Set(data.endpoints.map((value) => endpoint(value).href)),
|
||||
];
|
||||
const previous = owner.peers.get(id);
|
||||
if (!previous && owner.peers.size >= 64)
|
||||
throw new Error("Too many saved servers");
|
||||
const sameEndpoints =
|
||||
previous &&
|
||||
previous.endpoints.length === urls.length &&
|
||||
urls.every((url) => previous.endpoints.includes(url));
|
||||
const version = (owner.versions.get(id) ?? 0) + 1;
|
||||
owner.versions.set(id, version);
|
||||
if (previous && !sameEndpoints) {
|
||||
for (const socket of previous.sockets) socket.destroy();
|
||||
owner.peers.delete(id);
|
||||
}
|
||||
const peer = {
|
||||
endpoints: urls,
|
||||
cookies: new Map(),
|
||||
sockets: new Set(),
|
||||
connected: false,
|
||||
active: urls[0],
|
||||
};
|
||||
const results = await Promise.all(
|
||||
urls.map(async (value) => {
|
||||
try {
|
||||
const old = previous?.endpoints.includes(value)
|
||||
? (previous.cookies.get(value) ?? "")
|
||||
: "";
|
||||
const status = await authenticate(
|
||||
new URL(value),
|
||||
old,
|
||||
data.password,
|
||||
);
|
||||
if (status.connected) {
|
||||
peer.cookies.set(value, status.cookie);
|
||||
peer.active = value;
|
||||
peer.connected = true;
|
||||
}
|
||||
return status;
|
||||
} catch {
|
||||
return { connected: false, needs_password: false };
|
||||
}
|
||||
}),
|
||||
);
|
||||
if (owner.versions.get(id) !== version) {
|
||||
json(res, 409, { error: "Server selection changed during sign-in" });
|
||||
return true;
|
||||
}
|
||||
if (sameEndpoints && !peer.connected) {
|
||||
json(res, 200, {
|
||||
connected: false,
|
||||
needs_password: results.some((r) => r.needs_password),
|
||||
previous_connection_kept: true,
|
||||
error: "Sign-in failed. The existing connection was kept.",
|
||||
});
|
||||
return true;
|
||||
}
|
||||
if (sameEndpoints) peer.sockets = previous.sockets;
|
||||
owner.peers.set(id, peer);
|
||||
json(res, 200, {
|
||||
connected_endpoints: [...peer.cookies.keys()],
|
||||
connected: peer.connected,
|
||||
needs_password:
|
||||
!peer.connected && results.some((r) => r.needs_password),
|
||||
...(!peer.connected && !results.some((r) => r.needs_password)
|
||||
? { error: "Server could not be reached" }
|
||||
: {}),
|
||||
});
|
||||
} catch {
|
||||
json(res, 400, {
|
||||
error: "Server registration failed. Check the addresses and sign-in.",
|
||||
});
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
import {
|
||||
endpoint,
|
||||
webOrigin,
|
||||
boundedRequest,
|
||||
remoteRequest,
|
||||
json,
|
||||
} from "./peerTransport.js";
|
||||
import { PeerRegistry } from "./peerRegistry.js";
|
||||
|
||||
const allowed = (path) =>
|
||||
path === "/files" ||
|
||||
path === "/chat-uploads" ||
|
||||
path.startsWith("/artifacts/") ||
|
||||
path.startsWith("/updates/");
|
||||
export class PeerRelay extends PeerRegistry {
|
||||
target(req, path) {
|
||||
const parsed = new URL(req.url, "http://localhost");
|
||||
const match = /^\/peers\/([^/]+)\/(.*)$/.exec(parsed.pathname);
|
||||
if (!match) return null;
|
||||
const owner = this.owner(req);
|
||||
const peer = owner.peers.get(decodeURIComponent(match[1]));
|
||||
if (!peer) throw new Error("Select and sign in to this server again");
|
||||
const selected = endpoint(
|
||||
parsed.searchParams.get("endpoint") ?? peer.active,
|
||||
).href;
|
||||
if (!peer.endpoints.includes(selected) || !peer.cookies.has(selected))
|
||||
throw new Error("Server address is not signed in");
|
||||
parsed.searchParams.delete("endpoint");
|
||||
const url = new URL(path ?? `/${match[2]}`, webOrigin(selected));
|
||||
url.search = parsed.search;
|
||||
if (url.origin !== webOrigin(selected).origin)
|
||||
throw new Error("Remote origin changed");
|
||||
return { url, cookie: peer.cookies.get(selected), peer };
|
||||
}
|
||||
http(req, res) {
|
||||
if (!req.url.startsWith("/peers/")) return false;
|
||||
try {
|
||||
const target = this.target(req);
|
||||
if (
|
||||
!target ||
|
||||
!allowed(target.url.pathname) ||
|
||||
!["GET", "HEAD", "POST"].includes(req.method) ||
|
||||
(req.method === "POST" && target.url.pathname !== "/chat-uploads")
|
||||
) {
|
||||
json(res, 404, { error: "Unsupported remote resource" });
|
||||
return true;
|
||||
}
|
||||
const headers = { ...(target.cookie ? { cookie: target.cookie } : {}) };
|
||||
for (const name of [
|
||||
"content-type",
|
||||
"content-length",
|
||||
"range",
|
||||
"x-file-name",
|
||||
"x-upload-name",
|
||||
"x-chat-file-name",
|
||||
"x-pipelines-upload-name",
|
||||
])
|
||||
if (req.headers[name]) headers[name] = req.headers[name];
|
||||
const upstream = remoteRequest(target.url, {
|
||||
method: req.method,
|
||||
headers,
|
||||
});
|
||||
upstream.setTimeout(30000, () => upstream.destroy());
|
||||
upstream.on("response", (response) => {
|
||||
const safe = {};
|
||||
for (const name of [
|
||||
"content-type",
|
||||
"content-length",
|
||||
"content-range",
|
||||
"accept-ranges",
|
||||
"content-disposition",
|
||||
])
|
||||
if (response.headers[name]) safe[name] = response.headers[name];
|
||||
res.writeHead(
|
||||
response.statusCode >= 300 && response.statusCode < 400
|
||||
? 502
|
||||
: response.statusCode,
|
||||
{ ...safe, "Cache-Control": "no-store" },
|
||||
);
|
||||
response.pipe(res);
|
||||
});
|
||||
upstream.on("error", () => {
|
||||
if (!res.headersSent)
|
||||
json(res, 502, { error: "Remote server unavailable" });
|
||||
else res.destroy();
|
||||
});
|
||||
req.on("aborted", () => upstream.destroy());
|
||||
res.on("close", () => upstream.destroy());
|
||||
req.pipe(upstream);
|
||||
} catch {
|
||||
json(res, 401, { error: "Sign in to the remote server again" });
|
||||
}
|
||||
return true;
|
||||
}
|
||||
upgrade(req, socket, head) {
|
||||
if (!req.url.startsWith("/peers/")) return false;
|
||||
try {
|
||||
const target = this.target(req, "/connect");
|
||||
const headers = {
|
||||
host: target.url.host,
|
||||
connection: "Upgrade",
|
||||
upgrade: "websocket",
|
||||
"sec-websocket-key": req.headers["sec-websocket-key"],
|
||||
"sec-websocket-version": "13",
|
||||
...(target.cookie ? { cookie: target.cookie } : {}),
|
||||
};
|
||||
const upstream = remoteRequest(target.url, { headers });
|
||||
upstream.setTimeout(10000, () => upstream.destroy());
|
||||
upstream.on("upgrade", (response, remote, remoteHead) => {
|
||||
socket.write(
|
||||
`HTTP/1.1 101 Switching Protocols\r\n${Object.entries(
|
||||
response.headers,
|
||||
)
|
||||
.filter(([k]) =>
|
||||
[
|
||||
"upgrade",
|
||||
"connection",
|
||||
"sec-websocket-accept",
|
||||
"sec-websocket-protocol",
|
||||
].includes(k),
|
||||
)
|
||||
.map(([k, v]) => `${k}: ${v}`)
|
||||
.join("\r\n")}\r\n\r\n`,
|
||||
);
|
||||
target.peer.sockets.add(socket);
|
||||
remote.setTimeout(0);
|
||||
socket.once("close", () => {
|
||||
target.peer.sockets.delete(socket);
|
||||
remote.destroy();
|
||||
});
|
||||
remote.once("close", () => socket.destroy());
|
||||
socket.on("error", () => remote.destroy());
|
||||
remote.on("error", () => socket.destroy());
|
||||
if (head?.length) remote.write(head);
|
||||
if (remoteHead?.length) socket.write(remoteHead);
|
||||
remote.pipe(socket);
|
||||
socket.pipe(remote);
|
||||
});
|
||||
upstream.on("response", (response) => {
|
||||
response.resume();
|
||||
socket.end(
|
||||
`HTTP/1.1 ${response.statusCode === 401 ? 401 : 502} Remote Unavailable\r\nContent-Length: 0\r\n\r\n`,
|
||||
);
|
||||
});
|
||||
upstream.on("error", () => socket.destroy());
|
||||
socket.once("close", () => upstream.destroy());
|
||||
upstream.end();
|
||||
} catch {
|
||||
socket.end("HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export async function proxyDiscovery(req, res, host, port) {
|
||||
try {
|
||||
const response = await boundedRequest(
|
||||
new URL(
|
||||
`http://${host.includes(":") ? `[${host}]` : host}:${port}/discovery`,
|
||||
),
|
||||
);
|
||||
res
|
||||
.writeHead(response.status, {
|
||||
"Content-Type": "application/json",
|
||||
"Cache-Control": "no-store",
|
||||
})
|
||||
.end(response.body);
|
||||
} catch {
|
||||
json(res, 503, { error: "LAN discovery unavailable" });
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
import http from "node:http";
|
||||
import https from "node:https";
|
||||
|
||||
export function endpoint(value) {
|
||||
const url = new URL(value);
|
||||
if (
|
||||
!["ws:", "wss:"].includes(url.protocol) ||
|
||||
!url.hostname ||
|
||||
url.username ||
|
||||
url.password ||
|
||||
url.hash ||
|
||||
url.search ||
|
||||
url.pathname !== "/connect"
|
||||
)
|
||||
throw new Error("Use a ws(s) server address ending in /connect");
|
||||
return url;
|
||||
}
|
||||
export function webOrigin(url) {
|
||||
const result = new URL(url);
|
||||
result.protocol = result.protocol === "wss:" ? "https:" : "http:";
|
||||
result.pathname = "/";
|
||||
return result;
|
||||
}
|
||||
export function remoteRequest(url, options = {}) {
|
||||
return (url.protocol === "https:" ? https : http).request(url, options);
|
||||
}
|
||||
|
||||
// Never follows redirects or sends local browser cookies to another machine.
|
||||
export function boundedRequest(
|
||||
url,
|
||||
{ method = "GET", cookie = "", body = "", headers = {} } = {},
|
||||
) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = remoteRequest(url, {
|
||||
method,
|
||||
headers: { ...headers, ...(cookie ? { Cookie: cookie } : {}) },
|
||||
});
|
||||
const timer = setTimeout(
|
||||
() => request.destroy(new Error("Server timed out")),
|
||||
8000,
|
||||
);
|
||||
request.once("error", (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
request.once("response", (response) => {
|
||||
const chunks = [];
|
||||
let bytes = 0;
|
||||
response.on("data", (chunk) => {
|
||||
bytes += chunk.length;
|
||||
if (bytes > 65536) {
|
||||
response.destroy();
|
||||
request.destroy(new Error("Server response was too large"));
|
||||
return;
|
||||
}
|
||||
chunks.push(chunk);
|
||||
});
|
||||
response.once("error", (error) => {
|
||||
clearTimeout(timer);
|
||||
reject(error);
|
||||
});
|
||||
response.once("end", () => {
|
||||
clearTimeout(timer);
|
||||
resolve({
|
||||
status: response.statusCode,
|
||||
headers: response.headers,
|
||||
body: Buffer.concat(chunks).toString("utf8"),
|
||||
});
|
||||
});
|
||||
});
|
||||
request.end(body);
|
||||
});
|
||||
}
|
||||
|
||||
export function sessionCookie(response) {
|
||||
return (response.headers["set-cookie"] ?? [])
|
||||
.map((value) => value.split(";")[0])
|
||||
.filter((value) => value.startsWith("pipelines_session="))
|
||||
.join("; ");
|
||||
}
|
||||
|
||||
export async function authenticate(url, cookie, password) {
|
||||
const login = new URL("/login", webOrigin(url));
|
||||
let response;
|
||||
if (password !== undefined) {
|
||||
const body = new URLSearchParams({ password, remember: "on" }).toString();
|
||||
response = await boundedRequest(login, {
|
||||
method: "POST",
|
||||
body,
|
||||
cookie,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
"Content-Length": Buffer.byteLength(body),
|
||||
},
|
||||
});
|
||||
if ([302, 303].includes(response.status)) {
|
||||
cookie = sessionCookie(response) || cookie;
|
||||
} else if (response.status === 401 || response.status === 429) {
|
||||
return { connected: false, needs_password: true, cookie: "" };
|
||||
}
|
||||
}
|
||||
response = await boundedRequest(login, { cookie });
|
||||
if ([302, 303].includes(response.status))
|
||||
return { connected: true, needs_password: false, cookie };
|
||||
if (
|
||||
response.status === 401 ||
|
||||
(response.status === 200 && /name=["']password["']/.test(response.body))
|
||||
) {
|
||||
return { connected: false, needs_password: true, cookie: "" };
|
||||
}
|
||||
if (response.status === 200)
|
||||
return { connected: true, needs_password: false, cookie };
|
||||
if (response.status === 404) {
|
||||
const health = await boundedRequest(new URL("/healthz", webOrigin(url)));
|
||||
if (health.status === 200 && JSON.parse(health.body)?.healthy === true)
|
||||
return { connected: true, needs_password: false, cookie: "" };
|
||||
}
|
||||
throw new Error("Server could not be reached");
|
||||
}
|
||||
|
||||
export function json(res, status, value) {
|
||||
res
|
||||
.writeHead(status, {
|
||||
"Content-Type": "application/json",
|
||||
"Cache-Control": "no-store",
|
||||
})
|
||||
.end(JSON.stringify(value));
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdtemp, writeFile, rename, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { createServer } from "node:net";
|
||||
import { once } from "node:events";
|
||||
|
||||
test("atomic password updates take effect without restart and fail closed on invalid files", async (t) => {
|
||||
const dir = await mkdtemp(join(tmpdir(), "pipelines-auth-reload-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const auth = join(dir, "auth.json");
|
||||
const data = { password: "old-password", secret: "s".repeat(40) };
|
||||
await writeFile(auth, JSON.stringify(data));
|
||||
const portServer = createServer().listen(0,"127.0.0.1"); await once(portServer,"listening");
|
||||
const port = portServer.address().port; await new Promise((done) => portServer.close(done));
|
||||
const child = spawn(process.execPath, ["serve.mjs", "--port", String(port)], {
|
||||
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
||||
env: { ...process.env, PIPELINES_WEB_AUTH: "", PIPELINES_WEB_AUTH_FILE: auth }, stdio:["ignore","pipe","inherit"] });
|
||||
t.after(async () => { const stopped = once(child,"exit"); child.kill(); await stopped; });
|
||||
await once(child.stdout,"data");
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
const login = (password) => fetch(base + "/login", { method:"POST", body:new URLSearchParams({password}), redirect:"manual" });
|
||||
const first = await login("old-password"); assert.equal(first.status,303);
|
||||
const cookie = first.headers.get("set-cookie").split(";")[0];
|
||||
await writeFile(auth + ".next", JSON.stringify({ ...data, password:"new-password" })); await rename(auth + ".next",auth);
|
||||
assert.equal((await login("old-password")).status,401);
|
||||
assert.equal((await login("new-password")).status,303);
|
||||
assert.equal((await fetch(base + "/login", {headers:{Cookie:cookie},redirect:"manual"})).status,302);
|
||||
await writeFile(auth + ".next", "malformed"); await rename(auth + ".next",auth);
|
||||
assert.equal((await login("new-password")).status,503);
|
||||
assert.equal((await fetch(base + "/login", {headers:{Cookie:cookie},redirect:"manual"})).status,503);
|
||||
await writeFile(auth,JSON.stringify(data));
|
||||
assert.equal((await login("old-password")).status,303);
|
||||
});
|
||||
@@ -0,0 +1,471 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { createServer, request } from "node:http";
|
||||
import { createHash, randomBytes } from "node:crypto";
|
||||
import { once } from "node:events";
|
||||
import { mkdtemp, writeFile, rm } from "node:fs/promises";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { PeerRegistry } from "../src/peerRegistry.js";
|
||||
import { spawn } from "node:child_process";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
async function fixture(t) {
|
||||
const observations = [];
|
||||
const gates = {};
|
||||
const hold = (name) => {
|
||||
let release, enter;
|
||||
const promise = new Promise((done) => {
|
||||
release = done;
|
||||
});
|
||||
const entered = new Promise((done) => {
|
||||
enter = done;
|
||||
});
|
||||
gates[name] = { promise, enter };
|
||||
t.after(release);
|
||||
return { entered, release };
|
||||
};
|
||||
const remotes = [];
|
||||
for (const name of ["one", "two"]) {
|
||||
const server = createServer(async (req, res) => {
|
||||
observations.push({
|
||||
name,
|
||||
path: req.url,
|
||||
cookie: req.headers.cookie,
|
||||
upload: req.headers["x-pipelines-upload-name"],
|
||||
});
|
||||
if (req.url === "/login" && req.method === "GET" && gates[name]) {
|
||||
gates[name].enter();
|
||||
await gates[name].promise;
|
||||
}
|
||||
if (req.url === "/discovery") {
|
||||
res.setHeader("Content-Type", "application/json");
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
discovered_servers: [
|
||||
{
|
||||
name: "Linux desktop",
|
||||
endpoint: "ws://192.168.1.2:8083/connect",
|
||||
auth: "password",
|
||||
version: "test",
|
||||
},
|
||||
],
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (req.url === "/login" && req.method === "POST") {
|
||||
let body = "";
|
||||
for await (const chunk of req) body += chunk;
|
||||
if (new URLSearchParams(body).get("password") !== `password-${name}`) {
|
||||
res.writeHead(401).end();
|
||||
return;
|
||||
}
|
||||
res
|
||||
.writeHead(303, {
|
||||
"Set-Cookie": `pipelines_session=remote-${name}; HttpOnly`,
|
||||
Location: "/",
|
||||
})
|
||||
.end();
|
||||
return;
|
||||
}
|
||||
if (req.url === "/login") {
|
||||
if (req.headers.cookie === `pipelines_session=remote-${name}`)
|
||||
res.writeHead(302, { Location: "/" }).end();
|
||||
else res.end('<input name="password">');
|
||||
return;
|
||||
}
|
||||
res.setHeader("Set-Cookie", "do-not-forward=private");
|
||||
res.end(
|
||||
JSON.stringify({ name, url: req.url, cookie: req.headers.cookie }),
|
||||
);
|
||||
});
|
||||
server.on("upgrade", (req, socket) => {
|
||||
observations.push({
|
||||
name,
|
||||
path: req.url,
|
||||
cookie: req.headers.cookie,
|
||||
origin: req.headers.origin,
|
||||
});
|
||||
if (req.headers.cookie !== `pipelines_session=remote-${name}`) {
|
||||
socket.end("HTTP/1.1 401 Unauthorized\r\nContent-Length: 0\r\n\r\n");
|
||||
return;
|
||||
}
|
||||
const accept = createHash("sha1")
|
||||
.update(
|
||||
req.headers["sec-websocket-key"] +
|
||||
"258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
|
||||
)
|
||||
.digest("base64");
|
||||
socket.write(
|
||||
`HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: ${accept}\r\n\r\n`,
|
||||
);
|
||||
const message = Buffer.from(JSON.stringify({ name }));
|
||||
socket.write(
|
||||
Buffer.concat([Buffer.from([0x81, message.length]), message]),
|
||||
);
|
||||
socket.on("error", () => {});
|
||||
});
|
||||
server.listen(0, "127.0.0.1");
|
||||
await once(server, "listening");
|
||||
t.after(() => {
|
||||
server.closeAllConnections();
|
||||
server.close();
|
||||
});
|
||||
remotes.push(`ws://127.0.0.1:${server.address().port}/connect`);
|
||||
}
|
||||
const dir = await mkdtemp(join(tmpdir(), "peer-relay-"));
|
||||
t.after(() => rm(dir, { recursive: true, force: true }));
|
||||
const auth = join(dir, "auth.json");
|
||||
await writeFile(
|
||||
auth,
|
||||
JSON.stringify({ password: "local-password", secret: "s".repeat(40) }),
|
||||
);
|
||||
const reserve = createServer().listen(0, "127.0.0.1");
|
||||
await once(reserve, "listening");
|
||||
const port = reserve.address().port;
|
||||
await new Promise((done) => reserve.close(done));
|
||||
const child = spawn(
|
||||
process.execPath,
|
||||
[
|
||||
"serve.mjs",
|
||||
"--port",
|
||||
String(port),
|
||||
"--pipelines",
|
||||
new URL(remotes[0]).host,
|
||||
],
|
||||
{
|
||||
cwd: fileURLToPath(new URL("..", import.meta.url)),
|
||||
env: {
|
||||
...process.env,
|
||||
PIPELINES_WEB_AUTH: "",
|
||||
PIPELINES_WEB_AUTH_FILE: auth,
|
||||
},
|
||||
stdio: ["ignore", "pipe", "inherit"],
|
||||
},
|
||||
);
|
||||
t.after(async () => {
|
||||
const exited = once(child, "exit");
|
||||
child.kill();
|
||||
await exited;
|
||||
});
|
||||
await once(child.stdout, "data");
|
||||
const base = `http://127.0.0.1:${port}`;
|
||||
const login = async () => {
|
||||
const reply = await fetch(base + "/login", {
|
||||
method: "POST",
|
||||
body: new URLSearchParams({ password: "local-password" }),
|
||||
redirect: "manual",
|
||||
});
|
||||
return reply.headers.get("set-cookie").split(";")[0];
|
||||
};
|
||||
const cookie = await login();
|
||||
const register = (id, data, who = cookie, origin = base) =>
|
||||
fetch(base + "/api/peers/" + id, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Cookie: who,
|
||||
Origin: origin,
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
return { base, cookie, remotes, observations, register, login, hold };
|
||||
}
|
||||
|
||||
test("browser can authenticate both machines, relay files and keep local credentials isolated", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal(
|
||||
(await f.register("one", { endpoints: [f.remotes[0]] }, "")).status,
|
||||
401,
|
||||
);
|
||||
const needing = await f.register("one", { endpoints: [f.remotes[0]] });
|
||||
assert.equal((await needing.json()).needs_password, true);
|
||||
assert.equal(
|
||||
(
|
||||
await (
|
||||
await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
})
|
||||
).json()
|
||||
).connected,
|
||||
true,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await (
|
||||
await f.register("two", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
})
|
||||
).json()
|
||||
).connected,
|
||||
true,
|
||||
);
|
||||
for (const [id, name] of [
|
||||
["one", "one"],
|
||||
["two", "two"],
|
||||
]) {
|
||||
const reply = await fetch(
|
||||
f.base + `/peers/${id}/files?path=%2Fhome%2Freport`,
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
);
|
||||
assert.equal(reply.status, 200);
|
||||
assert.equal(reply.headers.get("set-cookie"), null);
|
||||
assert.deepEqual(await reply.json(), {
|
||||
name,
|
||||
url: "/files?path=%2Fhome%2Freport",
|
||||
cookie: `pipelines_session=remote-${name}`,
|
||||
});
|
||||
}
|
||||
const otherCookie = await f.login();
|
||||
assert.notEqual(otherCookie, f.cookie);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one/files", {
|
||||
headers: { Cookie: otherCookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await f.register(
|
||||
"one",
|
||||
{ endpoints: [f.remotes[0]], password: "password-one" },
|
||||
f.cookie,
|
||||
"http://evil.test",
|
||||
)
|
||||
).status,
|
||||
403,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await f.register("bad", {
|
||||
endpoints: ["ws://user:secret@localhost/connect"],
|
||||
})
|
||||
).status,
|
||||
400,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one//evil.test/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/one/other", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
f.observations.some((x) => x.cookie === f.cookie),
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("websocket relay authenticates the chosen machine without forwarding browser Origin", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("two", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
const req = request(
|
||||
f.base +
|
||||
"/peers/two/connect?endpoint=" +
|
||||
encodeURIComponent(f.remotes[1]),
|
||||
{
|
||||
headers: {
|
||||
Cookie: f.cookie,
|
||||
Origin: f.base,
|
||||
Connection: "Upgrade",
|
||||
Upgrade: "websocket",
|
||||
"Sec-WebSocket-Key": randomBytes(16).toString("base64"),
|
||||
"Sec-WebSocket-Version": "13",
|
||||
},
|
||||
},
|
||||
);
|
||||
req.on("upgrade", (res, socket, head) => {
|
||||
const receive = (chunk) => {
|
||||
socket.destroy();
|
||||
resolve({ status: res.statusCode, text: chunk.subarray(2).toString() });
|
||||
};
|
||||
if (head.length) receive(head);
|
||||
else socket.once("data", receive);
|
||||
});
|
||||
req.on("response", (res) => reject(new Error(String(res.statusCode))));
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
assert.equal(result.status, 101);
|
||||
assert.deepEqual(JSON.parse(result.text), { name: "two" });
|
||||
const observed = f.observations.find(
|
||||
(x) => x.name === "two" && x.path === "/connect",
|
||||
);
|
||||
assert.equal(observed.cookie, "pipelines_session=remote-two");
|
||||
assert.equal(observed.origin, undefined);
|
||||
});
|
||||
|
||||
test("removing or changing a profile revokes its previous remote route", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(
|
||||
f.base +
|
||||
"/peers/server/files?endpoint=" +
|
||||
encodeURIComponent(f.remotes[0]),
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
const removed = await fetch(f.base + "/api/peers/server", {
|
||||
method: "DELETE",
|
||||
headers: { Cookie: f.cookie, Origin: f.base },
|
||||
});
|
||||
assert.equal(removed.status, 200);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/server/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
});
|
||||
|
||||
test("browser discovery is authenticated and served from the hosting machine", async (t) => {
|
||||
const f = await fixture(t);
|
||||
assert.equal((await fetch(f.base + "/api/discovery")).status, 401);
|
||||
const result = await fetch(f.base + "/api/discovery", {
|
||||
headers: { Cookie: f.cookie },
|
||||
});
|
||||
assert.equal(result.status, 200);
|
||||
assert.equal(
|
||||
(await result.json()).discovered_servers[0].name,
|
||||
"Linux desktop",
|
||||
);
|
||||
assert.equal(f.observations.filter((x) => x.path === "/discovery").length, 1);
|
||||
});
|
||||
|
||||
test("failed password retry keeps the previous authenticated route and socket", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
const live = await new Promise((resolve, reject) => {
|
||||
const req = request(f.base + "/peers/one/connect", {
|
||||
headers: {
|
||||
Cookie: f.cookie,
|
||||
Origin: f.base,
|
||||
Connection: "Upgrade",
|
||||
Upgrade: "websocket",
|
||||
"Sec-WebSocket-Key": randomBytes(16).toString("base64"),
|
||||
"Sec-WebSocket-Version": "13",
|
||||
},
|
||||
});
|
||||
req.on("upgrade", (res, socket) => resolve(socket));
|
||||
req.on("response", (res) => reject(new Error(String(res.statusCode))));
|
||||
req.on("error", reject);
|
||||
req.end();
|
||||
});
|
||||
t.after(() => live.destroy());
|
||||
const failed = await f.register("one", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "wrong-password",
|
||||
});
|
||||
const status = await failed.json();
|
||||
assert.equal(status.previous_connection_kept, true);
|
||||
assert.equal(status.needs_password, true);
|
||||
const file = await fetch(f.base + "/peers/one/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
});
|
||||
assert.equal(file.status, 200);
|
||||
assert.equal((await file.json()).cookie, "pipelines_session=remote-one");
|
||||
assert.equal(live.destroyed, false);
|
||||
});
|
||||
|
||||
test("changed aliases revoke immediately and late sign-in cannot resurrect a deleted profile", async (t) => {
|
||||
const f = await fixture(t);
|
||||
await f.register("server", {
|
||||
endpoints: [f.remotes[0]],
|
||||
password: "password-one",
|
||||
});
|
||||
const gate = f.hold("two");
|
||||
const changing = f.register("server", {
|
||||
endpoints: [f.remotes[1]],
|
||||
password: "password-two",
|
||||
});
|
||||
await gate.entered;
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(
|
||||
f.base +
|
||||
"/peers/server/files?endpoint=" +
|
||||
encodeURIComponent(f.remotes[0]),
|
||||
{ headers: { Cookie: f.cookie } },
|
||||
)
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
await fetch(f.base + "/api/peers/server", {
|
||||
method: "DELETE",
|
||||
headers: { Cookie: f.cookie, Origin: f.base },
|
||||
});
|
||||
gate.release();
|
||||
assert.equal((await changing).status, 409);
|
||||
assert.equal(
|
||||
(
|
||||
await fetch(f.base + "/peers/server/files", {
|
||||
headers: { Cookie: f.cookie },
|
||||
})
|
||||
).status,
|
||||
401,
|
||||
);
|
||||
});
|
||||
|
||||
test("expired registry owners are closed and pruned before the capacity check", () => {
|
||||
const registry = new PeerRegistry((req) => req.token);
|
||||
let closed = 0;
|
||||
for (let i = 0; i < 256; i++)
|
||||
registry.sessions.set(String(i), {
|
||||
used: 0,
|
||||
versions: new Map(),
|
||||
peers: new Map([
|
||||
[
|
||||
"x",
|
||||
{
|
||||
sockets: new Set([
|
||||
{
|
||||
destroy() {
|
||||
closed++;
|
||||
},
|
||||
},
|
||||
]),
|
||||
},
|
||||
],
|
||||
]),
|
||||
});
|
||||
registry.owner({ token: "new-session" });
|
||||
assert.equal(closed, 256);
|
||||
assert.equal(registry.sessions.size, 1);
|
||||
const before = registry.sessions.size;
|
||||
registry.forget({ token: "unknown-invalid-session" });
|
||||
assert.equal(registry.sessions.size, before);
|
||||
});
|
||||
Reference in New Issue
Block a user