Fix empty favicon responses poisoning site tile icons
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.
This commit is contained in:
1 parent
bf38fc2be1
commit
8af7185aa9
2 files changed
+91
-2
No files matched your search
@@ -104,6 +104,14 @@ function dataUrlMime(dataUrl) {
|
||||
return match ? match[1].toLowerCase() : null;
|
||||
}
|
||||
|
||||
// An empty response body (a 204 from `/favicon.ico`, say) can still be stamped
|
||||
// with the candidate's declared type, yielding `data:image/x-icon;base64,`:
|
||||
// an image MIME with no bytes behind it.
|
||||
function dataUrlHasContent(dataUrl) {
|
||||
const comma = typeof dataUrl === 'string' ? dataUrl.indexOf(',') : -1;
|
||||
return comma >= 0 && comma < dataUrl.length - 1;
|
||||
}
|
||||
|
||||
function startsWithBytes(bytes, signature, offset = 0) {
|
||||
return signature.every((byte, i) => bytes[offset + i] === byte);
|
||||
}
|
||||
@@ -192,9 +200,13 @@ async function sniffImageMime(blob) {
|
||||
// the data URL's MIME, not `record.mime` (which can disagree: a response with no
|
||||
// Content-Type yields `data:application/octet-stream;…` while the candidate's
|
||||
// declared type was stamped on `mime`). Records whose rendered data URL isn't an
|
||||
// image are treated as if they were never cached.
|
||||
// image, or that carry no bytes at all, are treated as if they were never
|
||||
// cached.
|
||||
function isUsableIcon(record) {
|
||||
return isImageMime(dataUrlMime(record?.dataUrl));
|
||||
return (
|
||||
isImageMime(dataUrlMime(record?.dataUrl)) &&
|
||||
dataUrlHasContent(record?.dataUrl)
|
||||
);
|
||||
}
|
||||
|
||||
function toDataUrl(blob, mime) {
|
||||
@@ -282,6 +294,14 @@ export default class SiteIconsService extends Service {
|
||||
try {
|
||||
const blob = await this.http.requestBlob(candidate.url);
|
||||
|
||||
// A 204/empty response (some servers answer `/favicon.ico` this way)
|
||||
// carries no icon. Without this guard the candidate's declared type
|
||||
// would turn the empty body into `data:image/x-icon;base64,`, which
|
||||
// looks like an image and gets cached and rendered as a blank tile.
|
||||
if (blob.size === 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Trust the actual bytes first: a CDN serving `application/octet-stream`
|
||||
// (or no content type) may still hand back a perfectly good icon, while
|
||||
// an HTML/XML error page must always be rejected.
|
||||
|
||||
@@ -183,6 +183,43 @@ module('Unit | Service | site-icons', function (hooks) {
|
||||
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');
|
||||
|
||||
@@ -257,6 +294,38 @@ module('Unit | Service | site-icons', function (hooks) {
|
||||
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');
|
||||
|
||||
|
||||
Reference in new issue
Block a user