Files
kuentagym/api/test/music.test.js
T
dsantillan caed0cbaa0
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
Música: a playlist screen per profile, with Apple's limits stated plainly
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.
2026-09-23 12:22:01 -04:00

70 lines
3.8 KiB
JavaScript

/* What a public Apple Music page says about itself, and which links this API is willing to fetch.
*
* The parser is best-effort by design — Apple's markup is theirs to change — so these tests are
* about the two things that must not wobble: the schema block is read when it is there, the Open
* Graph tags take over when it is not, and a page that is not a playlist at all returns nothing
* rather than throwing. The allowlist gets the same treatment, because that one is a boundary
* rather than a convenience. */
import { test } from 'node:test';
import assert from 'node:assert/strict';
import { parseMusicPage, hostAllowed } from '../music.js';
const PAGE = `<!DOCTYPE html><html><head>
<meta property="og:title" content="Leg Day — Playlist by Ana">
<meta property="og:image" content="https://is1-ssl.mzstatic.com/image/thumb/Feature/abc/3000x3000bb.jpg">
<script type="application/ld+json">
{"@context":"https://schema.org","@type":"MusicPlaylist","name":"Leg Day &#39;24","numTracks":42,
"image":"https://is1-ssl.mzstatic.com/image/thumb/Feature/abc/1000x1000bb.jpg",
"byArtist":{"@type":"MusicGroup","name":"Ana"}}
</script></head><body>Leg Day</body></html>`;
test('reads the schema block first, and asks Apple\'s CDN for one useful size', () => {
const m = parseMusicPage(PAGE);
assert.equal(m.title, "Leg Day '24");
assert.equal(m.tracks, 42);
assert.equal(m.curator, 'Ana');
assert.equal(m.artwork, 'https://is1-ssl.mzstatic.com/image/thumb/Feature/abc/600x600bb.jpg');
});
test('falls back to Open Graph, and never invents what it cannot read', () => {
const noSchema = PAGE.replace(/<script[\s\S]*?<\/script>/, '');
const m = parseMusicPage(noSchema);
assert.equal(m.title, 'Leg Day — Playlist by Ana');
assert.ok(m.artwork.endsWith('600x600bb.jpg'));
assert.equal(m.tracks, null);
assert.equal(m.curator, null);
// A schema block that is there but unparseable is the same as one that is not: half a JSON object
// must not take the title down with it.
const broken = parseMusicPage(PAGE.replace('"numTracks":42,', '"numTracks":,'));
assert.equal(broken.title, 'Leg Day — Playlist by Ana');
assert.equal(broken.tracks, null);
});
test('says nothing rather than throwing when the page is not a playlist', () => {
const empty = { title: null, artwork: null, tracks: null, curator: null };
assert.deepEqual(parseMusicPage('<html><body>Sign in to Apple Music</body></html>'), empty);
assert.deepEqual(parseMusicPage(''), empty);
assert.deepEqual(parseMusicPage(null), empty);
// The `content` before `property` order is real markup too, and is matched.
assert.equal(parseMusicPage('<meta content="X" property="og:title">').title, 'X');
});
test('only Apple\'s own music hosts, over https — this is a boundary, not a preference', () => {
assert.equal(hostAllowed('https://music.apple.com/us/playlist/x/pl.1'), true);
assert.equal(hostAllowed('https://itunes.apple.com/us/album/x/1'), true);
assert.equal(hostAllowed('https://geo.music.apple.com/us/playlist/x/pl.1'), true);
// The interesting ones: the api container sits on the owner's network, so anything that could
// name a machine on it is refused — a lookalike domain, a LAN address, localhost, plain http, and
// a scheme that is not http at all.
assert.equal(hostAllowed('https://music.apple.com.evil.test/us/playlist/x/pl.1'), false);
assert.equal(hostAllowed('http://music.apple.com/us/playlist/x/pl.1'), false);
assert.equal(hostAllowed('http://127.0.0.1:3000/api/health'), false);
assert.equal(hostAllowed('http://192.168.1.1/'), false);
assert.equal(hostAllowed('http://169.254.169.254/latest/meta-data/'), false);
assert.equal(hostAllowed('file:///etc/passwd'), false);
assert.equal(hostAllowed('gopher://music.apple.com/'), false);
assert.equal(hostAllowed('not a url'), false);
assert.equal(hostAllowed(''), false);
});