Música: a playlist screen per profile, with Apple's limits stated plainly
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
Tests / test (push) Canceled after 0s
Publish Docker images / build (., ./web/Dockerfile, web) (push) Canceled after 0s
Publish Docker images / build (./api, ./api/Dockerfile, api) (push) Canceled after 0s
The idea was "each person types their Apple Music username and their public playlists appear". Apple does not allow that, and it is worth writing down rather than discovering later: there is no API for another person's public playlists — their own developer forums answer that request with "you can't" (threads 692941 and 123894) — and a profile page is rendered by JavaScript with nothing behind it to call. MusicKit offers the catalog and the *signed-in* user's own library, and the second needs an Apple Developer Program membership for a developer token plus a Music-User-Token per person: a private key and a third-party secret in an app that holds none, to fetch something other than what was asked for. What a self-hosted app can do is the share link, which every playlist has: Music → Share → Copy Link. So the Música screen (next to Salud in Home's header, plus a section in Settings) keeps a list of them per profile, in the profile document like the routines — so they sync to the other devices and travel in a backup — and tapping one opens it in Apple Music on the iPhone. The username is a link to the person's public profile. The list is nicer than a column of URLs because the API reads each link's public page for a title, artwork and track count. That is scraping, so it is built to lose: a page that says nothing leaves the card showing the name its owner typed, and nothing on the screen depends on it. `POST /api/music/lookup` is host-allowlisted to Apple's music hosts — https only, checked again on the URL a redirect lands on — because the api container sits on the same network as everything else the owner runs, and a route that fetches whatever it is handed is a request-forgery lever. That boundary has its own tests, including the LAN addresses it must refuse. The parsing lives in api/music.js rather than in both halves: two copies of a parser kept in step by hand is a bomb, and this is the same shape as vitals.js. It is in the Dockerfile's COPY for the same reason vitals.js had to be.
This commit is contained in:
1 parent
45183c725f
commit
caed0cbaa0
18 files changed
+743
-16
No files matched your search
@@ -20,6 +20,7 @@ import { startWarmup } from './coach/warmup.js';
|
||||
import { dayReminderPush, restTimerPush, testPush } from './push-messages.js';
|
||||
import { verifyError } from './verify-error.js';
|
||||
import { parsePayload, mergeVitals, vitalsSeries, emptyVitals, RETENTION_DAYS } from './vitals.js';
|
||||
import { hostAllowed, readCapped, parseMusicPage } from './music.js';
|
||||
|
||||
const PORT = +(process.env.PORT || 3000);
|
||||
const DATA = process.env.DATA_DIR || '/data';
|
||||
@@ -1100,6 +1101,43 @@ const routes = {
|
||||
json(res, 200, { ok: true });
|
||||
},
|
||||
|
||||
/* ---------- Apple Music: reading a link's public page ----------
|
||||
*
|
||||
* The client keeps the links; this only fills in what a link can say about itself — its title,
|
||||
* its artwork, how many tracks — so the list looks like a list of playlists instead of a column
|
||||
* of URLs. Apple publishes a schema.org MusicPlaylist block for previews; it is read here and
|
||||
* anything unexpected degrades to the name the person typed (see lib/music.js).
|
||||
*
|
||||
* HOST-ALLOWLISTED, and that is a security boundary rather than tidiness: the URL comes from a
|
||||
* signed-in user, the request is made from this container, and this container sits on the same
|
||||
* network as everything else the owner runs — a route that fetches whatever it is handed is a
|
||||
* request-forgery lever (the same reasoning as PUSH_AGENT below, which is why it exists). Only
|
||||
* Apple's own music hosts are reachable, which is a set no attacker can point at the LAN. */
|
||||
'POST /api/music/lookup': async (req, res) => {
|
||||
const user = readSession(req);
|
||||
if (!user) return json(res, 401, { error: 'not signed in' });
|
||||
const body = await readBody(req);
|
||||
const target = text(body.url).trim();
|
||||
if (!hostAllowed(target)) return json(res, 400, { error: 'not an Apple Music link' });
|
||||
let r;
|
||||
try {
|
||||
r = await fetch(target, {
|
||||
redirect: 'follow',
|
||||
signal: AbortSignal.timeout(MUSIC_TIMEOUT_MS),
|
||||
headers: { 'User-Agent': 'KuentaGYM (self-hosted)' }
|
||||
});
|
||||
} catch {
|
||||
return json(res, 200, { ok: true, meta: null, error: 'unreachable' });
|
||||
}
|
||||
// Where we ended up is checked too, not only where we were pointed: a redirect is otherwise a
|
||||
// way around the allowlist.
|
||||
if (!r.ok || !hostAllowed(r.url)) {
|
||||
return json(res, 200, { ok: true, meta: null, error: r.ok ? 'off-host' : 'http-' + r.status });
|
||||
}
|
||||
const html = await readCapped(r, MUSIC_MAX_HTML);
|
||||
json(res, 200, { ok: true, meta: parseMusicPage(html) });
|
||||
},
|
||||
|
||||
'GET /api/push/public-key': async (req, res) => json(res, 200, { key: vapid.publicKey }),
|
||||
|
||||
'POST /api/push/subscribe': async (req, res) => {
|
||||
|
||||
Reference in new issue
Block a user