Fixed bug in connection logic.
This commit is contained in:
1 parent
5c4ebb81d7
commit
f08fdbeda9
7 files changed
+106
-7
No files matched your search
+8
-1
@@ -35,6 +35,13 @@ Private test certificates are generated in memory, never distributed. No Windows
|
||||
NVDA process, standalone add-on or Orca plugin has been run against this code.
|
||||
Live captured fixtures and versioned interoperability results remain required.
|
||||
|
||||
A fresh random-channel probe against nvdaremote.com confirmed that its successful
|
||||
join reply includes the joining client's ID in `origin`. This matches
|
||||
[NVDARemote/remote-server Channel.add_client](https://github.com/NVDARemote/remote-server/blob/master/server.py).
|
||||
The embedded NVDA server omits that field. Transport regression tests cover both
|
||||
variants, both roles, and either peer connection order. This relay probe does not
|
||||
establish live NVDA speech, keyboard or Braille interoperability.
|
||||
|
||||
## Framing and validation
|
||||
|
||||
TLS is mandatory. Each frame is a UTF-8 JSON object followed by LF. A relay frame
|
||||
@@ -51,7 +58,7 @@ messages; they never include channel keys, remote output or server text.
|
||||
| Message | Implemented representation and limits |
|
||||
| --- | --- |
|
||||
| `protocol_version`, `join` | Outbound authentication only: version 2, channel and `connection_type` of `master` or `slave`. |
|
||||
| `channel_joined` | Matching channel required. Missing `clients` means empty. A maximum of 16 peers may be parsed; session policy accepts at most one. `user_ids`, if present, must exactly match typed peers. Nonempty legacy IDs without roles fail. The echoed key is removed from returned membership. |
|
||||
| `channel_joined` | Matching channel required. Missing `clients` means empty. A maximum of 16 peers may be parsed; session policy accepts at most one. `user_ids`, if present, must exactly match typed peers. Nonempty legacy IDs without roles fail. The echoed key is removed from returned membership. Public relay `origin` identifies this client and must differ from all peers; the embedded NVDA relay omits it. |
|
||||
| `client_joined`, `client_left` | Typed `client` requires positive `id` and known `connection_type`. Optional `user_id` must agree. ID-only legacy membership is unsupported. |
|
||||
| `speak` | Up to 1,024 sequence entries. Strings preserve whitespace and order. Numeric priority 0, 1 or 2; absence defaults to 0. Null and string priorities fail. |
|
||||
| `cancel`, `pause_speech` | Typed cancel or required boolean `switch`. No output backend is called. |
|
||||
|
||||
@@ -254,8 +254,14 @@ func connection_status_text(status connectionStatus) string {
|
||||
return "Certificate verification failed. No channel key was sent. A fresh local connection action is required; certificate inspection is never automatic."
|
||||
case errors.Is(status.err, errLease):
|
||||
return "Reader approval is unavailable or was revoked. Fresh local approval is required."
|
||||
case errors.Is(status.err, errPeer):
|
||||
return "Remote peer membership or origin changed. Fresh local approval is required."
|
||||
case errors.Is(status.err, errProtocol), errors.Is(status.err, errLimit):
|
||||
return "The relay sent an invalid or unsupported protocol message. Fresh local approval is required."
|
||||
case errors.Is(status.err, errTimeout):
|
||||
return "The relay connection timed out. Fresh local approval is required."
|
||||
default:
|
||||
return "Relay connection failed or the peer changed. Fresh local approval is required."
|
||||
return "Relay connection failed or closed. Fresh local approval is required."
|
||||
}
|
||||
default:
|
||||
return "Disconnected. Fresh local approval is required to connect again."
|
||||
|
||||
@@ -55,6 +55,17 @@ type membership struct {
|
||||
peers []peer
|
||||
}
|
||||
|
||||
// Public relays identify this client in channel_joined.origin; NVDA's
|
||||
// embedded relay omits it. It must never identify an existing channel peer.
|
||||
func (v membership) validOrigin(origin int) bool {
|
||||
for _, p := range v.peers {
|
||||
if p.ID == origin {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
type keyTransition struct {
|
||||
VK int `json:"vk_code"`
|
||||
Extended bool `json:"extended"`
|
||||
|
||||
@@ -10,6 +10,7 @@ type session struct {
|
||||
epoch uint64
|
||||
state string // joining, waiting, ready, suspended, closed
|
||||
peer peer
|
||||
selfID int // Optional local client ID supplied by public relays at join.
|
||||
ignored uint64 // Saturating counter; never stores untrusted diagnostic text.
|
||||
}
|
||||
|
||||
@@ -40,10 +41,10 @@ func (s *session) accept(epoch uint64, m message) (bool, error) {
|
||||
switch m.kind {
|
||||
case "channel_joined":
|
||||
v, ok := m.payload.(membership)
|
||||
if !ok || m.origin != 0 || s.state != "joining" || len(v.peers) > 1 {
|
||||
if !ok || !v.validOrigin(m.origin) || s.state != "joining" || len(v.peers) > 1 {
|
||||
return s.suspend(errPeer)
|
||||
}
|
||||
s.state = "waiting"
|
||||
s.state, s.selfID = "waiting", m.origin
|
||||
if len(v.peers) == 1 {
|
||||
if v.peers[0].Role == s.role {
|
||||
return s.suspend(errPeer)
|
||||
@@ -53,7 +54,7 @@ func (s *session) accept(epoch uint64, m message) (bool, error) {
|
||||
return false, nil
|
||||
case "client_joined":
|
||||
p, ok := m.payload.(peer)
|
||||
if !ok || s.state != "waiting" || p.Role == s.role || m.origin != 0 && m.origin != p.ID {
|
||||
if !ok || p.ID == s.selfID || s.state != "waiting" || p.Role == s.role || m.origin != 0 && m.origin != p.ID {
|
||||
return s.suspend(errPeer)
|
||||
}
|
||||
s.peer, s.state = p, "ready"
|
||||
|
||||
@@ -125,3 +125,15 @@ func TestSessionRejectsPeerOriginatedChannelJoin(t *testing.T) {
|
||||
t.Fatal("peer supplied relay membership", accepted, err, s.state)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionRejectsOwnIDAsPeer(t *testing.T) {
|
||||
s, _ := newSession(controller, 1)
|
||||
joined := mustMessage(t, `{"type":"channel_joined","origin":42,"channel":"fixture","clients":[]}`)
|
||||
if _, err := s.accept(1, joined); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := mustMessage(t, `{"type":"client_joined","client":{"id":42,"connection_type":"slave"}}`)
|
||||
if _, err := s.accept(1, m); !errors.Is(err, errPeer) {
|
||||
t.Fatalf("accepted own ID as peer: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -188,10 +188,10 @@ func connect(ctx context.Context, e endpoint, r role, key string) (*relayConn, m
|
||||
}
|
||||
continue
|
||||
case "channel_joined":
|
||||
if m.origin != 0 {
|
||||
v := m.payload.(membership)
|
||||
if !v.validOrigin(m.origin) {
|
||||
return nil, message{}, errPeer
|
||||
}
|
||||
v := m.payload.(membership)
|
||||
if v.channel != key {
|
||||
return nil, message{}, errProtocol
|
||||
}
|
||||
|
||||
@@ -553,3 +553,65 @@ func TestChannelKeysAndEndpointValidation(t *testing.T) {
|
||||
t.Fatal("bare IPv6 rejected")
|
||||
}
|
||||
}
|
||||
|
||||
// Exercise both relay implementations, in both roles and connection orders.
|
||||
func TestTLSJoinRelayOriginVariants(t *testing.T) {
|
||||
cert, roots := testCertificate(t, true)
|
||||
for _, origin := range []int{0, 42} {
|
||||
for _, r := range []role{controller, controlled} {
|
||||
for _, peerPresent := range []bool{false, true} {
|
||||
opposite := controlled
|
||||
if r == controlled {
|
||||
opposite = controller
|
||||
}
|
||||
e := testRelay(t, cert, func(conn net.Conn) {
|
||||
reader := relayHandshake(t, conn, r)
|
||||
if reader == nil {
|
||||
return
|
||||
}
|
||||
peers := []peer{}
|
||||
if peerPresent {
|
||||
peers = append(peers, peer{ID: 7, Role: opposite})
|
||||
}
|
||||
reply := map[string]any{"type": "channel_joined", "channel": "synthetic-fixture", "clients": peers}
|
||||
if origin != 0 {
|
||||
reply["origin"] = origin
|
||||
}
|
||||
encoder := json.NewEncoder(conn)
|
||||
_ = encoder.Encode(reply)
|
||||
if !peerPresent {
|
||||
_ = encoder.Encode(map[string]any{"type": "client_joined", "client": peer{ID: 7, Role: opposite}})
|
||||
}
|
||||
_ = encoder.Encode(map[string]any{"type": "set_braille_info", "origin": 7, "name": "fixture", "numCells": 20})
|
||||
_, _ = reader.ReadByte()
|
||||
})
|
||||
e.roots = roots
|
||||
conn, joined, err := connect(context.Background(), e, r, "synthetic-fixture")
|
||||
if err != nil {
|
||||
t.Fatalf("origin=%d role=%s peerPresent=%v: %v", origin, r, peerPresent, err)
|
||||
}
|
||||
s, _ := newSession(r, 1)
|
||||
if _, err = s.accept(1, joined); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !peerPresent {
|
||||
m, err := conn.read()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = s.accept(1, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
m, err := conn.read()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if accepted, err := s.accept(1, m); err != nil || !accepted || s.state != "ready" {
|
||||
t.Fatalf("peer traffic: %v %v", accepted, err)
|
||||
}
|
||||
conn.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user