|
| 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). |
0 commit comments