A 204/empty response to `/favicon.ico` was stamped with the candidate's declared type, producing `data:image/x-icon;base64,`. Because that MIME is an image, the empty record was treated as usable and cached for the 7-day TTL, so discovery never ran again and the declared apple-touch-icon was never picked up. Skip zero-length candidate bodies during resolution, and treat cached data URLs with no payload as unusable so existing poisoned records heal on the next load.
380 lines
13 KiB
JavaScript
380 lines
13 KiB
JavaScript
import { module, test } from 'qunit';
|
|
import { setupTest } from 'dashtab/tests/helpers';
|
|
import { normalizeOrigin } from 'dashtab/services/site-icons';
|
|
|
|
function blob(content, type) {
|
|
return new Blob([content], { type });
|
|
}
|
|
|
|
// The leading bytes of a PNG, enough for the service's magic-number sniffing.
|
|
function pngBlob(type) {
|
|
return new Blob(
|
|
[Uint8Array.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])],
|
|
{ type }
|
|
);
|
|
}
|
|
|
|
module('Unit | Service | site-icons', function (hooks) {
|
|
setupTest(hooks);
|
|
|
|
hooks.beforeEach(async function () {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.clear('icons');
|
|
});
|
|
|
|
test('normalizeOrigin derives an origin and rejects junk', function (assert) {
|
|
assert.strictEqual(
|
|
normalizeOrigin('https://forum.example.com/latest'),
|
|
'https://forum.example.com'
|
|
);
|
|
assert.strictEqual(normalizeOrigin('nope'), null);
|
|
assert.strictEqual(normalizeOrigin(''), null);
|
|
});
|
|
|
|
test('it prefers a maskable manifest icon and caches the bytes', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
const requested = [];
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="manifest" href="/site.webmanifest"></head></html>';
|
|
service.http.requestJson = async () => ({
|
|
theme_color: '#0f172a',
|
|
icons: [
|
|
{ src: '/icon-192.png', sizes: '192x192', type: 'image/png' },
|
|
{
|
|
src: '/icon-maskable-512.png',
|
|
sizes: '512x512',
|
|
type: 'image/png',
|
|
purpose: 'maskable',
|
|
},
|
|
],
|
|
});
|
|
service.http.requestBlob = async (url) => {
|
|
requested.push(url);
|
|
return blob('icon', 'image/png');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(
|
|
record.href,
|
|
'https://forum.example.com/icon-maskable-512.png'
|
|
);
|
|
assert.true(record.maskable);
|
|
assert.strictEqual(record.themeColor, '#0f172a');
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
assert.strictEqual(requested.length, 1);
|
|
|
|
const storage = this.owner.lookup('service:storage');
|
|
assert.deepEqual(
|
|
await storage.get('icons', 'https://forum.example.com'),
|
|
record
|
|
);
|
|
|
|
// A second lookup is served from the cache without touching the network.
|
|
service.http.requestBlob = async () => {
|
|
throw new Error('should not fetch again');
|
|
};
|
|
const cached = await service.iconFor('https://forum.example.com');
|
|
assert.strictEqual(cached.href, record.href);
|
|
});
|
|
|
|
test('it falls back to /favicon.ico when the HTML cannot be fetched', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/favicon.ico')) {
|
|
return blob('icon', 'image/x-icon');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.href, 'https://forum.example.com/favicon.ico');
|
|
assert.false(record.maskable);
|
|
});
|
|
|
|
test('it resolves relative icon URLs against the site origin', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="apple-touch-icon" href="/touch.png"></head></html>';
|
|
service.http.requestBlob = async () => blob('icon', 'image/png');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.href, 'https://forum.example.com/touch.png');
|
|
assert.true(record.maskable);
|
|
});
|
|
|
|
test('it prefers a declared apple-touch-icon and marks it maskable', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="icon" href="/favicon.ico"><link rel="apple-touch-icon" href="/touch.png"></head></html>';
|
|
service.http.requestBlob = async () => blob('icon', 'image/png');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.href, 'https://forum.example.com/touch.png');
|
|
assert.true(record.maskable);
|
|
});
|
|
|
|
test('it treats the fallback apple-touch-icon as maskable', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
return blob('icon', 'image/png');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(
|
|
record.href,
|
|
'https://forum.example.com/apple-touch-icon.png'
|
|
);
|
|
assert.true(record.maskable);
|
|
});
|
|
|
|
test('it resolves nothing when every candidate fails', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async () => {
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record, undefined);
|
|
});
|
|
|
|
test('it skips a candidate that responds with a non-image body', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="icon" href="/favicon.ico"></head></html>';
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
return blob('<html>challenge</html>', 'text/html');
|
|
}
|
|
if (url.endsWith('/favicon.ico')) {
|
|
return blob('icon', 'image/x-icon');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.href, 'https://forum.example.com/favicon.ico');
|
|
assert.strictEqual(record.mime, 'image/x-icon');
|
|
assert.ok(record.dataUrl.startsWith('data:image/x-icon'));
|
|
});
|
|
|
|
test('it skips an empty candidate response and uses the next one', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
// A 204 whose body is empty but carries the candidate's declared type.
|
|
return blob('', 'image/png');
|
|
}
|
|
if (url.endsWith('/favicon.ico')) {
|
|
return pngBlob('image/x-icon');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.href, 'https://forum.example.com/favicon.ico');
|
|
assert.strictEqual(record.mime, 'image/png');
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it resolves nothing when every candidate responds with an empty body', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async () => blob('', 'image/x-icon');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record, undefined);
|
|
});
|
|
|
|
test('it stamps the candidate type when the response has no content type', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="apple-touch-icon" href="/touch.png" type="image/png"></head></html>';
|
|
service.http.requestBlob = async () => blob('icon', '');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.mime, 'image/png');
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it re-resolves a cached record that is not an image', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('icons', 'https://forum.example.com', {
|
|
origin: 'https://forum.example.com',
|
|
href: 'https://forum.example.com/touch.png',
|
|
dataUrl: 'data:text/html;base64,PGh0bWw+',
|
|
mime: 'text/html',
|
|
maskable: true,
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:site-icons');
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
return blob('icon', 'image/png');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(
|
|
record.href,
|
|
'https://forum.example.com/apple-touch-icon.png'
|
|
);
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it heals a cached record whose data URL MIME is not an image', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('icons', 'https://forum.example.com', {
|
|
origin: 'https://forum.example.com',
|
|
href: 'https://forum.example.com/apple-touch-icon.png',
|
|
// `mime` claims an image, but the data URL (what the browser decodes) does
|
|
// not — the shape left behind by a response without a Content-Type.
|
|
dataUrl: 'data:application/octet-stream;base64,aWNvbg==',
|
|
mime: 'image/png',
|
|
maskable: true,
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:site-icons');
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
return pngBlob('application/octet-stream');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.mime, 'image/png');
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it heals a cached record whose data URL has no bytes', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('icons', 'https://forum.example.com', {
|
|
origin: 'https://forum.example.com',
|
|
href: 'https://forum.example.com/favicon.ico',
|
|
// The shape left behind by an empty 204: an image MIME with no payload.
|
|
dataUrl: 'data:image/x-icon;base64,',
|
|
mime: 'image/x-icon',
|
|
maskable: false,
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:site-icons');
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async (url) => {
|
|
if (url.endsWith('/apple-touch-icon.png')) {
|
|
return pngBlob('image/png');
|
|
}
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(
|
|
record.href,
|
|
'https://forum.example.com/apple-touch-icon.png'
|
|
);
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it sniffs an image served with a generic content type', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () =>
|
|
'<html><head><link rel="icon" href="/favicon.ico"></head></html>';
|
|
service.http.requestBlob = async () => pngBlob('application/octet-stream');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record.mime, 'image/png');
|
|
assert.ok(record.dataUrl.startsWith('data:image/png'));
|
|
});
|
|
|
|
test('it rejects markup served with a generic content type', async function (assert) {
|
|
const service = this.owner.lookup('service:site-icons');
|
|
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async () =>
|
|
blob('<!doctype html><html></html>', 'application/octet-stream');
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record, undefined);
|
|
});
|
|
|
|
test('it ignores a poisoned cached record when nothing else resolves', async function (assert) {
|
|
const storage = this.owner.lookup('service:storage');
|
|
await storage.set('icons', 'https://forum.example.com', {
|
|
origin: 'https://forum.example.com',
|
|
href: 'https://forum.example.com/touch.png',
|
|
dataUrl: 'data:text/html;base64,PGh0bWw+',
|
|
mime: 'text/html',
|
|
maskable: true,
|
|
updatedAt: Date.now(),
|
|
});
|
|
|
|
const service = this.owner.lookup('service:site-icons');
|
|
service.http.requestText = async () => {
|
|
throw new Error('blocked by CORS');
|
|
};
|
|
service.http.requestBlob = async () => {
|
|
throw new Error('not found');
|
|
};
|
|
|
|
const record = await service.iconFor('https://forum.example.com');
|
|
|
|
assert.strictEqual(record, undefined);
|
|
});
|
|
});
|