Skip to content

Commit eefccbd

Browse files
Merge pull request #696 from appdevforall/feat/K2GO-376-share-exposure
K2GO-376 feat(cx_core): expose the Share gate off loopback (nginx :8095)
2 parents 89815e0 + 098f0c5 commit eefccbd

4 files changed

Lines changed: 326 additions & 0 deletions

File tree

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
# cx-core Share: scoped off-loopback exposure (K2GO-376, step 2)
2+
3+
Status: DESIGN + role change (this branch). The security crux of granular wireless
4+
Share. Authority for the gate itself: `ADR-content-exchange.md` "Pillar Share". This
5+
doc covers only the EXPOSURE: how a paired peer reaches the sender's `/v1/share` gate,
6+
and nothing else of cx-core, off the loopback interface.
7+
8+
## The problem
9+
10+
Share lets one K2Go box serve selected content to a peer over a hotspot. The receiver
11+
PULLS from the sender's live cx-core `/v1/share/<session>/...` gate (step 1, merged).
12+
For that pull to work, the box must expose the gate off loopback. It must expose ONLY
13+
that gate. The rest of the cx-core API (`/v1/content`, `/v1/export`, `/v1/import`,
14+
`/v1/storage`, `/v1/apps`, and session creation `POST /v1/share`) must stay unreachable
15+
off-box.
16+
17+
Today nothing of cx-core is reachable off-box: cx-core binds `127.0.0.1:9500`, the
18+
dash-node relay is in-box, and the nginx `/k2go-api` location on `:8085` is
19+
allow-loopback / deny-all.
20+
21+
## Decision: a dedicated port (Option B)
22+
23+
A dedicated nginx listener on `0.0.0.0:8095` (plain HTTP) proxies ONLY
24+
`/v1/share/<session>/...` to the loopback cx-core. The shared `:8085` server and its
25+
`/k2go-api` deny-all are untouched; cx-core stays bound to loopback.
26+
27+
Why a dedicated port, not a widened location on `:8085`:
28+
- A listener that only knows how to reach `/v1/share/<session>/...` cannot leak another
29+
endpoint through a location-ordering or regex slip. The blast radius is one path
30+
prefix, on one port, in one file.
31+
- The proxy target is cx-core directly (`127.0.0.1:9500`), not the dash-node relay.
32+
The relay is a blanket `/v1` forwarder (`cx-proxy.ts`); routing the exposed port
33+
through it would add a hop and a second path-unscoped component for no gain. Direct
34+
to cx-core keeps the single scoping point in one nginx file.
35+
36+
Why plain HTTP: the link is the closed local hotspot and the token is the auth. A
37+
self-signed cert that the offline peers cannot verify adds no trust. The defense is
38+
two independent layers: the nginx path scope, and the gate token enforced in cx-core.
39+
40+
## The scope is airtight (what the server block does)
41+
42+
The template `roles/cx_core/templates/cx-core-share-nginx.conf.j2` builds the server
43+
block. It is regex-free and denies by default:
44+
45+
- `location / { return 404; }` : deny by default. Any path other than the share
46+
surface is 404, including every other `/v1/*` endpoint.
47+
- `location = /v1/share/ { return 404; }` : block session creation. This is the one
48+
real trap. cx-core's router does `path.rstrip("/")` before it splits segments
49+
(`router.py:177`), so `POST /v1/share/` collapses to `POST /v1/share` = CREATE, which
50+
mints a token for ARBITRARY items. The receiver never creates a session (the sender
51+
does, in-box over loopback), so the create path is blocked here. Bare `POST /v1/share`
52+
(no trailing slash) does not match the prefix below and falls to the deny-by-default
53+
404.
54+
- `merge_slashes on;` : so `/v1/share//` (and more slashes) collapse to `/v1/share/`
55+
and hit the create guard, not the prefix rule. This is the nginx default; it is set
56+
explicitly so the guard holds even if the rootfs http block turned it off.
57+
- `location ^~ /v1/share/ { proxy_pass http://127.0.0.1:9500; ... }` : the receiver
58+
surface only, which is `GET .../manifest`, `GET .../fetch/<ref>`, `POST .../complete`
59+
and `DELETE /v1/share/<session>`. Every one is token-gated inside cx-core (401 no
60+
token, 403 mismatch, 404 unknown session; `compare_digest`). `^~` stops a future
61+
regex location from overriding it.
62+
63+
The streaming settings (`proxy_buffering off`, `proxy_http_version 1.1`,
64+
`proxy_read_timeout 3600s`) let a multi-GB item stream through without buffering to disk
65+
and without a mid-transfer timeout on a slow hotspot link.
66+
67+
The `X-Share-Token` header passes through nginx unchanged (it is hyphenated, so the
68+
underscore-in-header drop does not apply). The token travels in the header, never the
69+
URL, so it stays out of nginx access logs.
70+
71+
Idle safety: with no active session, every `/v1/share/<anything>` request is 404
72+
(unknown session) and the create path is blocked, so an always-listening `:8095` port
73+
exposes no content when nobody is sharing. The session id is `token_hex(16)` (128-bit)
74+
and the token is `token_urlsafe(32)` (256-bit): both unguessable. The real reachability
75+
window is the hotspot being up, which the sender app owns ("do not leave it on").
76+
77+
## What the rootfs must provide (OUT OF THIS REPO)
78+
79+
The main nginx config: the `http` block, the `:8085` server block, its `listen`
80+
address, and the includes, lives in the rootfs (built by the IIAB bake), not in this
81+
repo. The `*-nginx.conf` files here are LOCATION snippets that IIAB includes INSIDE the
82+
`:8085` server block (that is why they are bare `location` blocks). A `server` block
83+
cannot go in that include set. So the rootfs owns two items:
84+
85+
1. Include the Share server block at the http context. Add one line to the rootfs
86+
nginx config, inside `http { ... }` and OUTSIDE any `server { }` block:
87+
88+
```
89+
include /etc/nginx/k2go-http.d/*.conf;
90+
```
91+
92+
The role writes the server block to `/etc/nginx/k2go-http.d/cx-core-share.conf` and
93+
creates that directory. If the rootfs does not add this include, the file sits inert
94+
and `:8095` never listens: no accidental exposure (fail-closed). The glob tolerates
95+
an empty or absent directory, so the include line is safe to add before the role has
96+
run.
97+
98+
2. Confirm the main content server listens off loopback:
99+
`listen 0.0.0.0:8085` (not `127.0.0.1:8085`). This is the open checklist item in
100+
`device-to-device-connectivity.md` section 6 (and section 10). It is required for a
101+
peer to reach the box at all, independent of Share. VERIFY on the box:
102+
103+
```
104+
grep -RnE "listen[[:space:]]+.*8085" /etc/nginx/
105+
```
106+
107+
If it shows `127.0.0.1:8085`, no peer can reach the box; the rootfs nginx must bind
108+
`0.0.0.0:8085`. (The `:8095` Share block in this role already binds `0.0.0.0`.)
109+
110+
## Port decision: 8095 is free
111+
112+
Checked against the IIAB port map (iiab wiki "IIAB Networking" and the nginx role
113+
README) and this repo's ports:
114+
115+
- IIAB 80xx cluster: 8000 jupyterhub, 8006-8008 kalite (deprecated), 8009 kolibri,
116+
8010 calibre-serve, 8080 calibre-serve / idmgr, 8081 cham (proposed), 8083
117+
calibre-web, 8089 sugarizer, 8090 apache2 shim (deprecated).
118+
- K2Go additions: 8085 nginx portal, 8114 tier-3 help docs, 9500 cx-core (loopback),
119+
8083 calibre-web (cx-core reads it), 8009 kolibri.
120+
- AzuraCast (the wide range to avoid) sits at 10000-10499 plus 12080/12443, far from
121+
8095. Icecast is not used by IIAB.
122+
123+
8095 is unassigned in every source. It sits clear above the dense 808x content cluster
124+
(8080/8081/8083) and clear of the deprecated 8089/8090. The alternate candidate 8086 is
125+
also free, but it sits inside the 808x cluster, so 8095 is the cleaner pick. Ports are
126+
site-configurable in IIAB (`default_vars.yml`), so `cx_core_share_port` is a role
127+
variable: if a site has remapped something onto 8095, change the one variable.
128+
129+
## How to verify the scoped exposure on the A16
130+
131+
Device: Samsung A16 (RF8Y80CE2DA). The box is the Debian proot on the phone; cx-core is
132+
reachable in-box on `127.0.0.1:9500`. After deploying the role (so
133+
`/etc/nginx/k2go-http.d/cx-core-share.conf` exists) AND the rootfs http-level include is
134+
in place AND nginx is reloaded, reach `:8095` from the host over `adb forward`:
135+
136+
```
137+
adb -s RF8Y80CE2DA forward tcp:8095 tcp:8095
138+
```
139+
140+
Positive (the receiver surface is reachable): an unknown session returns 404 from the
141+
gate, which proves the proxy reaches cx-core through the scoped path, not an nginx 404:
142+
143+
```
144+
curl -i http://127.0.0.1:8095/v1/share/deadbeef/manifest -H "X-Share-Token: x"
145+
# expect 404 {"error":"unknown session"} (from cx-core: proxied through)
146+
```
147+
148+
For a full positive, create a real session in-box over loopback (the sender's path),
149+
then fetch its manifest through `:8095` with the real token:
150+
151+
```
152+
# in-box (loopback), as the sender does:
153+
curl -s -XPOST http://127.0.0.1:9500/v1/share \
154+
-d '{"items":[{"app":"kiwix","localRef":"<uuid>"}]}'
155+
# -> {"sessionId":"...","token":"...","manifestUrl":"/v1/share/<id>/manifest"}
156+
157+
# off-box surface (through :8095), with the token in the header:
158+
curl -i "http://127.0.0.1:8095/v1/share/<id>/manifest" -H "X-Share-Token: <token>"
159+
# expect 200 with the manifest
160+
```
161+
162+
Negative (nothing else is reachable). Each of these MUST return a plain nginx 404 (an
163+
HTML 404 body or empty, NOT a cx-core JSON error), which proves the request never
164+
reached cx-core:
165+
166+
```
167+
curl -i http://127.0.0.1:8095/v1/content # other API: 404
168+
curl -i http://127.0.0.1:8095/v1/apps # other API: 404
169+
curl -i http://127.0.0.1:8095/v1/storage # other API: 404
170+
curl -i -XPOST http://127.0.0.1:8095/v1/share # create (bare): 404
171+
curl -i -XPOST http://127.0.0.1:8095/v1/share/ # create (trailing slash): 404
172+
curl -i -XPOST http://127.0.0.1:8095/v1/share// # create (double slash): 404
173+
curl -i --path-as-is -XPOST http://127.0.0.1:8095/v1/share%2f # create (encoded slash): 404
174+
curl -i --path-as-is -XPOST http://127.0.0.1:8095/v1/share/%2e%2e # climb attempt: 404
175+
curl -i --path-as-is http://127.0.0.1:8095/v1/share/x/%2e%2e/content # climb to another API: 404
176+
curl -i http://127.0.0.1:8095/ # root: 404
177+
```
178+
179+
The encoded cases confirm the create guard holds when a client hand-encodes the slash or
180+
tries to climb: nginx normalizes `%2f` / `%2e%2e` before it matches a location, and cx-core
181+
splits the path before it unquotes each segment, so an encoded slash never collapses the path
182+
back to the two-segment `v1/share` create route. Use `curl --path-as-is` so curl does not
183+
normalize them away before the request leaves the host.
184+
185+
The tell is the body: a cx-core JSON error (`{"error":...}`) on any of the negative
186+
cases means the scope leaked (the request reached cx-core). A bare nginx 404 means the
187+
scope held. The create cases are the ones that matter most: a JSON token response there
188+
is a full gate bypass.
189+
190+
Remove the `adb forward` when done (`adb -s RF8Y80CE2DA forward --remove tcp:8095`).
191+
192+
## Scope kept (what this change does NOT do)
193+
194+
- Does not expose the whole API: only `/v1/share/<session>/...`.
195+
- Does not bind cx-core to 0.0.0.0: cx-core stays on `127.0.0.1:9500`.
196+
- Does not widen the `:8085` `/k2go-api` deny-all, and does not touch the `:8085`
197+
server block.
198+
- Does not add TLS (plain HTTP, token is the auth: see the decision above).

‎tools/upstream-patches/overlays/roles/cx_core/defaults/main.yml‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,32 @@ cx_core_port: 9500
2626
# The PDSM service wrapper to install under proot. Ships in the repo next to the
2727
# dash-node wrapper; both drivers (this role and the iiab-android bootstrap) copy it.
2828
cx_core_pdsm_src: "{{ cx_core_src_dir }}/../pdsm/cx-core"
29+
30+
31+
# ---- Off-loopback Share exposure (K2GO-376, Pillar Share) ----
32+
# Granular wireless Share lets a paired peer on the hotspot PULL selected content from
33+
# this box's live cx-core /v1/share gate. cx-core stays bound to loopback: a dedicated
34+
# nginx listener is the ONLY off-box surface, and it proxies ONLY /v1/share/<session>/
35+
# ... (never the rest of the /v1 API). The share token (X-Share-Token header) is the
36+
# auth; this scope is defense in depth. Design + the rootfs contract:
37+
# controller/docs/cx-core-share-exposure.md.
38+
39+
# The TCP port the Share listener binds on 0.0.0.0. 8095 is free on an IIAB box: the
40+
# 80xx cluster uses 8080/8083/8089 (calibre / calibre-web / sugarizer), 8085 is the
41+
# K2Go portal, and AzuraCast sits at 10000+. Site-configurable, per IIAB tradition;
42+
# the receiver's QR carries <senderHotspotIp>:<this port>.
43+
cx_core_share_port: 8095
44+
45+
# Master switch for the off-box Share surface. False removes the listener and keeps
46+
# cx-core loopback-only (in-box content exchange still works; only wireless Share is
47+
# off). A kill-switch for the one security-sensitive surface, independent of
48+
# cx_core_enabled.
49+
#
50+
# v1 decision (K2GO-376): ON by default on EVERY platform (proot phones AND systemd
51+
# boxes: rpi / pc, arm64 / amd64), so the surface can be observed in the field. The
52+
# listener is inert without an active share session (unknown-session 404, create
53+
# blocked), and every reachable endpoint is token-gated, so an always-bound port leaks
54+
# no content. If observation shows a systemd box should not open it by default, gate the
55+
# install by platform or arch later (an is_proot / arch condition on the install tasks),
56+
# not by removing the switch.
57+
cx_core_share_expose: True

‎tools/upstream-patches/overlays/roles/cx_core/tasks/enable-or-disable.yml‎

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,52 @@
2121
name: cx-core
2222
state: stopped
2323
when: not is_proot and not cx_core_enabled
24+
25+
26+
# ---- Off-loopback Share listener (K2GO-376). The dedicated :{{ cx_core_share_port }}
27+
# nginx server block that exposes ONLY /v1/share/<session>/... Installed when cx-core
28+
# is enabled AND share exposure is on; removed otherwise (fail-closed). The rootfs must
29+
# include /etc/nginx/k2go-http.d/*.conf at the nginx http context for it to take effect
30+
# (controller/docs/cx-core-share-exposure.md); until then the file sits inert, so no
31+
# exposure happens by accident. ----
32+
33+
- name: Ensure the http-level include dir for the Share server block exists
34+
file:
35+
path: /etc/nginx/k2go-http.d
36+
state: directory
37+
mode: "0755"
38+
when: cx_core_enabled and cx_core_share_expose
39+
40+
- name: "Expose the cx-core Share gate off loopback via NGINX (:{{ cx_core_share_port }})"
41+
template:
42+
src: cx-core-share-nginx.conf.j2
43+
dest: /etc/nginx/k2go-http.d/cx-core-share.conf
44+
mode: "0644"
45+
when: cx_core_enabled and cx_core_share_expose
46+
register: cx_share_conf
47+
48+
- name: Remove the cx-core Share listener (exposure off or cx-core disabled)
49+
file:
50+
path: /etc/nginx/k2go-http.d/cx-core-share.conf
51+
state: absent
52+
when: not (cx_core_enabled and cx_core_share_expose)
53+
register: cx_share_removed
54+
55+
# Reload nginx ONLY when the Share listener was written or removed in this run, not on every
56+
# play. Gating on the change avoids bouncing nginx (and, on proot, every live :8085 connection)
57+
# when nothing changed. A graceful systemd reload, or a pdsm restart on proot (pdsm has no
58+
# reload verb), picks up the new :{{ cx_core_share_port }} listen socket.
59+
- name: Reload 'nginx' systemd service when the Share listener changed (not proot)
60+
systemd:
61+
name: nginx
62+
state: reloaded
63+
when:
64+
- not is_proot
65+
- (cx_share_conf is changed) or (cx_share_removed is changed)
66+
67+
- name: Restart 'nginx' via pdsm when the Share listener changed (proot)
68+
command: pdsm restart nginx
69+
changed_when: true
70+
when:
71+
- is_proot
72+
- (cx_share_conf is changed) or (cx_share_removed is changed)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# cx-core Share listener (K2GO-376, Pillar Share). The ONLY off-loopback surface of
2+
# the content-exchange core. Installed by roles/cx_core (tasks/enable-or-disable.yml).
3+
#
4+
# A paired peer on the hotspot PULLS selected content from this box. cx-core stays
5+
# bound to 127.0.0.1; this dedicated listener proxies ONLY /v1/share/<session>/... to
6+
# it. A dedicated port cannot leak the rest of the /v1 API the way a widened location
7+
# on the shared :8085 server could. The share-session token (X-Share-Token header) is
8+
# the real auth, enforced inside cx-core; this scope is defense in depth.
9+
#
10+
# Plain HTTP by design: the link is the closed local hotspot and the token is the
11+
# auth. A self-signed cert the offline peers cannot verify adds no trust here. See
12+
# controller/docs/cx-core-share-exposure.md and ADR-content-exchange.md "Pillar Share".
13+
#
14+
# ROOTFS NOTE: a "server" block must be included at the nginx http context. IIAB's
15+
# /etc/nginx/conf.d holds LOCATION snippets included inside the :8085 server block, so
16+
# this file cannot live there. The rootfs includes it at http level (see the doc).
17+
18+
server {
19+
listen 0.0.0.0:{{ cx_core_share_port }}; # plain HTTP: the gate token is the auth
20+
server_name _;
21+
22+
# Collapse repeated slashes, so "/v1/share//" cannot slip past the create guard
23+
# below. cx-core strips a trailing slash, so /v1/share/ maps to the create call.
24+
merge_slashes on;
25+
26+
# Deny by default: nothing on this port is reachable unless a rule below allows it.
27+
location / {
28+
return 404;
29+
}
30+
31+
# Block session creation. POST /v1/share mints a token for ARBITRARY items, so it
32+
# is NEVER exposed off-box (the sender creates the session in-box over loopback).
33+
# cx-core strips the trailing slash, so /v1/share/ reaches create too: block it.
34+
location = /v1/share/ {
35+
return 404;
36+
}
37+
38+
# The receiver surface only: GET .../manifest, GET .../fetch/<ref>,
39+
# POST .../complete, DELETE /v1/share/<session>. Every one is token-gated inside
40+
# cx-core (401 no token, 403 mismatch, 404 unknown session). "^~" keeps a future
41+
# regex location from overriding this prefix match.
42+
location ^~ /v1/share/ {
43+
proxy_pass http://127.0.0.1:{{ cx_core_port }};
44+
proxy_http_version 1.1;
45+
proxy_buffering off; # stream multi-GB items; do not buffer to disk
46+
proxy_request_buffering off;
47+
proxy_read_timeout 3600s; # a large item over a slow hotspot link
48+
proxy_send_timeout 3600s;
49+
}
50+
}

0 commit comments

Comments
 (0)