Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

BlueMapAuth for Paper: BlueMap's web map behind a sign-in, showing each player only the worlds and players they're allowed to see.

**Status:** early, version 0.2.0. 24 HTTP and config tests pass, plus a bot test (25/25, and 8/8 without Mayor and LuckPerms, 2026-11-11) on a throwaway Paper 26.1.2 server with the real BlueMap 5.29, LuckPerms 5.5, Mayor and WelcomeWagon, signing in through WelcomeWagon's "Open the map" over HTTP. The hand-off page was checked in a real browser (Chromium, 2026-11-11, against made-up data on localhost; `BrowserDemo` in the test sources). The token leaves the address bar, the cookie is invisible to scripts, a reused link explains itself, and the page has no policy errors. On a copy of a real server on Paper 26.2 and 26.3 build 249 (2026-11-14) it loaded with no errors, switched off, so serving the map on 26.2 and 26.3 is still untested, as is running behind Caddy.
**Status:** early, version 0.2.1. 31 HTTP and config tests pass, plus a bot test (27/27 direct, 28/28 through a local Caddy running the staged site block, and 8/8 without Mayor and LuckPerms, 2026-11-24) on a throwaway Paper 26.1.2 server with the real BlueMap 5.29, LuckPerms 5.5, Mayor and WelcomeWagon, signing in through WelcomeWagon's "Open the map" over HTTP. The hand-off page was checked in a real browser (Chromium, 2026-11-11, against made-up data on localhost; `BrowserDemo` in the test sources). The token leaves the address bar, the cookie is invisible to scripts, a reused link explains itself, and the page has no policy errors. On a copy of a real server on Paper 26.2 and 26.3 build 249 (2026-11-14) it loaded with no errors, switched off, so serving the map on 26.2 and 26.3 is still untested, as is running behind Caddy.

## What it does

- Serves BlueMap's web map at its own address, such as `map.example.org`, and nothing of it to anyone who isn't signed in. BlueMap's own web server stays on 127.0.0.1, so BlueMapAuth is the only way in.
- Signs a browser in with a one-time link from WelcomeWagon's invite page ("Open the map"). The link works once, for a minute. The map then has its own cookie for its own address only, and it ends within 30 seconds of the WelcomeWagon session ending.
- Lists only the maps of worlds the viewer may enter (`multiverse.access.<world>` by default). Any other map is "not found", the same as one that doesn't exist.
- Passes on only an exact list of paths, case-sensitive, and refuses any percent-encoding or `.`/`..` segment, so nothing reaches BlueMap in a form it would read differently. Live markers and the live event stream aren't passed on.
- Shows only the players the viewer may see. In game, that's chat's reach from Mayor. A viewer who isn't in game sees only players who share a Mayor circle with them.
- Fails closed. Without WelcomeWagon nobody can sign in. Without BlueMap there are no maps. Without Mayor the viewer sees only themselves. For a viewer who isn't in game, without LuckPerms there are no maps and no other players.

Expand All @@ -24,7 +25,7 @@ The jar is `build/libs/BlueMapAuth-<version>.jar`. In `plugins/BlueMapAuth/confi

## Bot test

`test/live/bluemapauth.mjs` builds a throwaway server in a temp folder, runs bots and browser requests against it, and deletes it. The settings it needs are at the top of the file. `MK_TEST_MISSING=1` runs it without Mayor and LuckPerms, to check those paths fail closed. It needs Node 22 or newer, and `npm ci` in `test/live` first.
`test/live/bluemapauth.mjs` builds a throwaway server in a temp folder, runs bots and browser requests against it, and deletes it. The settings it needs are at the top of the file. `MK_TEST_MISSING=1` runs it without Mayor and LuckPerms, to check those paths fail closed. `MK_TEST_PROXY=1` puts a throwaway Caddy in front, running the staged map site block. It needs Node 22 or newer, and `npm ci` in `test/live` first.

## Commands

Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "com.extantape"
version = "0.2.0"
version = "0.2.1"

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(25))
Expand Down
18 changes: 18 additions & 0 deletions docs/releases/v0.2.1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
A security fix from the review of the map's public address (K70, 2026-11-24). Install it before the map is reachable from outside.

## What changed

- **Only an exact list of paths is passed to BlueMap:** the map page, `/settings.json`, `/assets/...`, `/lang/...`, a few icon files, and `/maps/<map>/...` for maps the viewer may see. Matching is case-sensitive. Anything else is "not found".
- **Paths BlueMap would read differently are refused:** any percent-encoding, backslash, empty segment, or `.` or `..` segment. Before, a signed-in player could reach a map they weren't allowed to see, and its live player list, by encoding a letter (`/%6Daps/...`), adding `/./` or changing the capitals. BlueMap decodes and tidies paths, and the Mac's disk ignores capitals.
- **Live data:** `live/players.json` is still filtered by who the viewer may see. `live/markers.json` now answers with no markers, because markers can carry positions Mayor hasn't checked. The live event stream and anything else under `live/` isn't passed on.
- **Map files are no longer cached for 5 minutes.** Each request is checked against the session, so a shared browser doesn't keep showing the map after sign-out.
- **Sign-in tries from IPv6 are counted per /64**, so a few addresses can't use up the limit.
- **A malformed hand-off link is answered "That link didn't work"** (403) instead of a server error.

## Installing over 0.2.0

With the server stopped, swap in `BlueMapAuth-0.2.1.jar`. Nothing else changes, and signed-in browsers stay signed in.

## Tested

31 unit and HTTP tests, including every path variant from the review. Bots on a throwaway Paper 26.1.2 server with the real BlueMap 5.29, LuckPerms, Mayor 0.8.0 and WelcomeWagon 0.6.1: 27/27 direct, and 28/28 with a throwaway Caddy in front running the staged map site block. The run without Mayor and LuckPerms passed 8/8 (fail closed). The encoded, dot and capitalised paths and the live stream were sent exactly as written and none reached a hidden map or live data. Not yet behind the real public address.
7 changes: 7 additions & 0 deletions docs/test-audit.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

Kept by this repo's owning session, per `~/Code/CLAUDE.md`, "Test self-audits". Newest first.


## 2026-11-24: security review fixes (0.2.1, after a fix)

- Promises: only an exact list of paths reaches BlueMap; encoded, dot and recased paths are refused; hidden maps and their live data stay hidden; live markers and the event stream never pass; a malformed hand-off is a 403; sign-in limits count an IPv6 /64 as one visitor.
- Covered: `MapServerTest.routeAllowsOnlyTheExactList`, `encodedDottedOrRecasedPathsNeverReachAKidsHiddenMap` (18 variants, BlueMap never asked), `liveMarkersAreNeverPassedOn`, `aMalformedPercentInAHandOffIsRefusedNot502`; `ClientAddressTest` (IPv6 /64, IPv4 per address, untrusted forwarding). Live: 12 variants sent raw against the real BlueMap, direct (27/27) and through Caddy with the staged block (28/28); fail closed 8/8.
- Gaps skipped: TLS, the http redirect and PROXY protocol are the proxy's, covered by Infrastructure's map-proxy-test.sh (17/17). The rate limit's per-/64 key is unit-tested; a live run with many IPv6 addresses isn't possible on 127.0.0.1.

## 2026-11-10: first audit (starter kit)

- Promises: none yet.
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/extantape/bluemapauth/web/ClientAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,23 @@ public final class ClientAddress {

public ClientAddress(List<String> trustedProxies) { this.trusted = List.copyOf(trustedProxies); }

/**
* The rate-limit key for a visitor. An IPv6 visitor usually holds a whole /64, so the key is the /64; otherwise a
* few addresses could use up the site-wide sign-in limit (K70 review, 2026-11-24). IPv4 stays per address.
*/
public String of(InetAddress remote, String forwardedFor) {
return key(visitor(remote, forwardedFor));
}

static String key(String address) {
InetAddress a = literal(address);
if (a == null || a.getAddress().length != 16) return address;
byte[] b = a.getAddress();
for (int i = 8; i < 16; i++) b[i] = 0;
try { return InetAddress.getByAddress(b).getHostAddress() + "/64"; } catch (java.net.UnknownHostException e) { return address; }
}

private String visitor(InetAddress remote, String forwardedFor) {
if (!trusted(remote) || forwardedFor == null || forwardedFor.isBlank()) return remote.getHostAddress();
// Walk from the right: the last address a trusted proxy didn't add is the visitor.
String[] hops = forwardedFor.split(",");
Expand Down
89 changes: 64 additions & 25 deletions src/main/java/com/extantape/bluemapauth/web/MapServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,39 @@ public final class MapServer {
static final Duration MIN_ANSWER = Duration.ofMillis(400);
static final int MAX_JSON = 4 * 1024 * 1024;
private static final int MAX_BODY = 256;
private static final Pattern MAP_PATH = Pattern.compile("/maps/([A-Za-z0-9_-]{1,64})(/.*)?");
private static final Pattern BAD_PATH = Pattern.compile("(?i).*(\\.\\.|%2e|%2f|%5c|%00|\\\\|//).*");
/** A path segment BlueMapAuth will pass on: plain letters, digits, dash, underscore and dots inside a name. */
private static final Pattern SEGMENT = Pattern.compile("[A-Za-z0-9_-][A-Za-z0-9_.-]{0,127}");
private static final Pattern ROOT_FILE = Pattern.compile("[A-Za-z0-9_-]{1,64}\\.(?:ico|png|svg|webmanifest)");

/** What a request path is, after the exact allowlist (K70 review, 2026-11-24). Anything not listed is NOT_FOUND. */
enum Kind { BAD, PAGE, SETTINGS, STATIC, MAP_FILE, MAP_PLAYERS, MAP_MARKERS, NOT_FOUND }

record Route(Kind kind, String map) {}

/**
* Routes a raw request path by an exact, case-sensitive allowlist. Refuses any percent-encoding, backslash, empty,
* "." or ".." segment, so nothing BlueMap would decode or normalise differently gets through. Pure, for tests.
*/
static Route route(String raw) {
if (raw == null || !raw.startsWith("/") || raw.indexOf('%') >= 0 || raw.indexOf('\\') >= 0) return new Route(Kind.BAD, null);
if (raw.equals("/") || raw.equals("/index.html")) return new Route(Kind.PAGE, null);
String[] seg = raw.substring(1).split("/", -1);
for (String x : seg) if (!SEGMENT.matcher(x).matches()) return new Route(Kind.BAD, null);
if (seg.length == 1) {
if (seg[0].equals("settings.json")) return new Route(Kind.SETTINGS, null);
return new Route(ROOT_FILE.matcher(seg[0]).matches() ? Kind.STATIC : Kind.NOT_FOUND, null);
}
if (seg[0].equals("assets") || seg[0].equals("lang")) return new Route(Kind.STATIC, null);
if (seg[0].equals("maps") && seg.length >= 3 && seg[1].length() <= 64 && seg[1].indexOf('.') < 0) {
if (seg[2].equals("live")) {
if (seg.length == 4 && seg[3].equals("players.json")) return new Route(Kind.MAP_PLAYERS, seg[1]);
if (seg.length == 4 && seg[3].equals("markers.json")) return new Route(Kind.MAP_MARKERS, seg[1]);
return new Route(Kind.NOT_FOUND, null); // the live event stream and anything else live never pass
}
return new Route(Kind.MAP_FILE, seg[1]);
}
return new Route(Kind.NOT_FOUND, null);
}
private static final Set<String> PASS_BACK = Set.of("content-type", "content-encoding", "etag", "last-modified");

public record Settings(String bind, int port, URI publicUrl, URI blueMap, String signInUrl, List<String> trustedProxies,
Expand Down Expand Up @@ -97,21 +128,22 @@ private void handle(HttpExchange x) {
try {
String path = x.getRequestURI().getRawPath();
String method = x.getRequestMethod();
if (path == null || !path.startsWith("/") || BAD_PATH.matcher(path).matches()) { plain(x, 400, "Bad request."); return; }
if (path.equals("/handoff")) {
if (path != null && path.equals("/handoff")) {
if (method.equals("GET")) page(x, 200, Pages.handoff(settings.signInUrl()));
else if (method.equals("POST")) handoff(x);
else notAllowed(x, "GET, POST");
return;
}
Route r = route(path);
if (r.kind() == Kind.BAD) { plain(x, 400, "Bad request."); return; }
if (!method.equals("GET") && !method.equals("HEAD")) { notAllowed(x, "GET, HEAD"); return; }
Optional<Access.Viewer> v = sessions.lookup(cookie(x, COOKIE));
if (v.isEmpty()) {
if (path.equals("/") || path.equals("/index.html")) page(x, 401, Pages.signIn(settings.signInUrl()));
if (r.kind() == Kind.PAGE) page(x, 401, Pages.signIn(settings.signInUrl()));
else plain(x, 401, "Sign in first.");
return;
}
serve(x, v.get(), path);
serve(x, v.get(), path, r);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (RuntimeException | IOException e) {
Expand All @@ -125,27 +157,29 @@ private void handle(HttpExchange x) {

// --- signed in ---

private void serve(HttpExchange x, Access.Viewer v, String path) throws IOException, InterruptedException {
if (path.equals("/settings.json")) {
Set<String> allowed = allowedMaps(v);
String body = fetchJson(path);
String out = body == null ? null : JsonFilter.settings(body, allowed::contains);
if (out == null) { plain(x, 502, ""); return; }
json(x, out);
return;
}
var m = MAP_PATH.matcher(path);
if (path.startsWith("/maps/")) {
if (!m.matches() || !allowedMaps(v).contains(m.group(1))) { plain(x, 404, "Not found."); return; }
if ("/live/players.json".equals(m.group(2))) {
private void serve(HttpExchange x, Access.Viewer v, String path, Route r) throws IOException, InterruptedException {
switch (r.kind()) {
case SETTINGS -> {
Set<String> allowed = allowedMaps(v);
String body = fetchJson(path);
json(x, body == null ? JsonFilter.EMPTY_PLAYERS : JsonFilter.players(body, ps -> access.visible(v.uuid(), ps)));
return;
String out = body == null ? null : JsonFilter.settings(body, allowed::contains);
if (out == null) { plain(x, 502, ""); return; }
json(x, out);
}
pass(x, path, m.group(2) != null && m.group(2).startsWith("/live/") ? "private, no-store" : "private, max-age=300");
return;
case MAP_PLAYERS, MAP_MARKERS, MAP_FILE -> {
if (!allowedMaps(v).contains(r.map())) { plain(x, 404, "Not found."); return; }
if (r.kind() == Kind.MAP_PLAYERS) {
String body = fetchJson(path);
json(x, body == null ? JsonFilter.EMPTY_PLAYERS : JsonFilter.players(body, ps -> access.visible(v.uuid(), ps)));
} else if (r.kind() == Kind.MAP_MARKERS) {
json(x, "{}"); // markers can carry player positions that Mayor hasn't checked, so none are sent
} else {
pass(x, path, "private, no-cache");
}
}
case PAGE, STATIC -> pass(x, path, "private, no-cache");
default -> plain(x, 404, "Not found.");
}
pass(x, path, "private, no-cache");
}

private Set<String> allowedMaps(Access.Viewer v) {
Expand Down Expand Up @@ -189,7 +223,12 @@ private void handoff(HttpExchange x) throws IOException {

static String tokenOf(String form) {
if (!form.startsWith("token=") || form.indexOf('&') >= 0) return null;
String t = URLDecoder.decode(form.substring(6), StandardCharsets.US_ASCII);
String t;
try {
t = URLDecoder.decode(form.substring(6), StandardCharsets.US_ASCII);
} catch (IllegalArgumentException e) {
return null; // a malformed % is a bad link, answered 403, never a 502
}
return t.matches("[A-Za-z0-9_-]{43}") ? t : null;
}

Expand Down
35 changes: 35 additions & 0 deletions src/test/java/com/extantape/bluemapauth/web/ClientAddressTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.extantape.bluemapauth.web;

import org.junit.jupiter.api.Test;

import java.net.InetAddress;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

/** Rate-limit keys (K70 review, 2026-11-24): IPv6 by its /64, IPv4 per address, forwarded only from a trusted proxy. */
class ClientAddressTest {
final ClientAddress clients = new ClientAddress(List.of("127.0.0.1"));

@Test
void addressesInOneIpv6SlashSixtyFourShareOneAllowance() throws Exception {
InetAddress proxy = InetAddress.ofLiteral("127.0.0.1");
String a = clients.of(proxy, "2001:db8:1:2:aaaa::1"), b = clients.of(proxy, "2001:db8:1:2:ffff:ffff:ffff:ffff");
assertEquals(a, b);
assertEquals("2001:db8:1:2:0:0:0:0/64", a);
assertNotEquals(a, clients.of(proxy, "2001:db8:1:3::1"), "the next /64 is someone else");
}

@Test
void ipv4StaysPerAddress() {
InetAddress proxy = InetAddress.ofLiteral("127.0.0.1");
assertEquals("203.0.113.7", clients.of(proxy, "203.0.113.7"));
assertNotEquals(clients.of(proxy, "203.0.113.7"), clients.of(proxy, "203.0.113.8"));
}

@Test
void aForwardedAddressFromAnUntrustedSenderIsIgnored() {
assertEquals("198.51.100.9", clients.of(InetAddress.ofLiteral("198.51.100.9"), "203.0.113.7"));
}
}
Loading
Loading