Fix site icons cached from non-image responses
A response with no Content-Type reached FileReader as application/octet-stream, which the browser refuses to render, and a mismatched mime field previously marked such records as usable. Judge cached icons by their data URL MIME, sniff image bytes when the content type is missing or generic, reject markup, and fall back to the initial when an image fails to load.
This commit is contained in:
1 parent
14b43ebfa6
commit
f341c228b8
6 files changed
+374
-24
No files matched your search
@@ -1,4 +1,7 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import setStyle from '../../../modifiers/set-style';
|
||||
import { safeColor } from '../../../utils/safe-color';
|
||||
|
||||
@@ -6,6 +9,8 @@ import { safeColor } from '../../../utils/safe-color';
|
||||
// a resolved site icon, then a bundled image icon, then the brand color and
|
||||
// glyph from the registry.
|
||||
export default class SettingsIntegrationsAvatar extends Component {
|
||||
@tracked imgFailed = false;
|
||||
|
||||
get integration() {
|
||||
return this.args.integration;
|
||||
}
|
||||
@@ -14,6 +19,17 @@ export default class SettingsIntegrationsAvatar extends Component {
|
||||
return this.integration?.icon;
|
||||
}
|
||||
|
||||
// A cached icon whose bytes aren't a decodable image fails silently in the
|
||||
// browser, so ignore it and fall through to the next avatar source.
|
||||
get hasSiteIcon() {
|
||||
return Boolean(this.siteIcon) && !this.imgFailed;
|
||||
}
|
||||
|
||||
@action
|
||||
handleImageError() {
|
||||
this.imgFailed = true;
|
||||
}
|
||||
|
||||
get imageClass() {
|
||||
return this.siteIcon?.maskable
|
||||
? 'size-full object-cover'
|
||||
@@ -29,7 +45,7 @@ export default class SettingsIntegrationsAvatar extends Component {
|
||||
}
|
||||
|
||||
<template>
|
||||
{{#if this.siteIcon}}
|
||||
{{#if this.hasSiteIcon}}
|
||||
<span
|
||||
class="icon-edge icon-edge-no-highlight flex size-10 flex-none items-center justify-center overflow-hidden rounded-lg bg-gray-100"
|
||||
{{setStyle "background-color" this.themeColor}}
|
||||
@@ -39,6 +55,7 @@ export default class SettingsIntegrationsAvatar extends Component {
|
||||
alt=""
|
||||
aria-hidden="true"
|
||||
class={{this.imageClass}}
|
||||
{{on "error" this.handleImageError}}
|
||||
/>
|
||||
</span>
|
||||
{{else if @integration.iconUrl}}
|
||||
|
||||
@@ -1,8 +1,13 @@
|
||||
import Component from '@glimmer/component';
|
||||
import { action } from '@ember/object';
|
||||
import { on } from '@ember/modifier';
|
||||
import { tracked } from '@glimmer/tracking';
|
||||
import setStyle from '../../modifiers/set-style';
|
||||
import { safeColor } from '../../utils/safe-color';
|
||||
|
||||
export default class SiteLinksItemComponent extends Component {
|
||||
@tracked imgFailed = false;
|
||||
|
||||
get label() {
|
||||
return this.args.item?.label ?? 'Site';
|
||||
}
|
||||
@@ -29,6 +34,17 @@ export default class SiteLinksItemComponent extends Component {
|
||||
return this.args.item?.icon;
|
||||
}
|
||||
|
||||
// A cached icon whose bytes aren't a decodable image fails silently in the
|
||||
// browser, so fall back to the initial when the image errors.
|
||||
get hasIcon() {
|
||||
return Boolean(this.icon) && !this.imgFailed;
|
||||
}
|
||||
|
||||
@action
|
||||
handleImageError() {
|
||||
this.imgFailed = true;
|
||||
}
|
||||
|
||||
get initial() {
|
||||
const first = this.label.trim()?.[0];
|
||||
return first ? first.toUpperCase() : '?';
|
||||
@@ -74,13 +90,14 @@ export default class SiteLinksItemComponent extends Component {
|
||||
{{if this.isError 'ring-2 ring-red-400/70'}}"
|
||||
{{setStyle "background-color" this.tileColor}}
|
||||
>
|
||||
{{#if this.icon}}
|
||||
{{#if this.hasIcon}}
|
||||
<img
|
||||
src={{this.icon.dataUrl}}
|
||||
alt=""
|
||||
aria-hidden="true"
|
||||
data-test-site-links-icon
|
||||
class={{this.imageClass}}
|
||||
{{on "error" this.handleImageError}}
|
||||
/>
|
||||
{{else}}
|
||||
<span
|
||||
|
||||
+169
-9
@@ -69,13 +69,146 @@ function candidateScore(candidate, target) {
|
||||
return score;
|
||||
}
|
||||
|
||||
function toDataUrl(blob) {
|
||||
function isImageMime(type) {
|
||||
return typeof type === 'string' && type.toLowerCase().startsWith('image/');
|
||||
}
|
||||
|
||||
// A server answering 200 with an error/challenge page (or a CDN response with no
|
||||
// content type) must not be mistaken for an icon: an `<img>` pointed at a
|
||||
// non-image data URL fails silently, with no console error or network request.
|
||||
function imageMime(...types) {
|
||||
for (const type of types) {
|
||||
if (isImageMime(type)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// Determine the MIME type to store for a fetched icon. A declared image type
|
||||
// wins; a missing or generic binary content type falls back to what the
|
||||
// candidate declared, but an explicit non-image type (e.g. `text/html`) is
|
||||
// rejected so an error or challenge page is never mistaken for an icon.
|
||||
function resolveIconMime(responseType, candidateType) {
|
||||
if (isImageMime(responseType)) {
|
||||
return responseType;
|
||||
}
|
||||
if (!responseType || responseType === 'application/octet-stream') {
|
||||
return imageMime(candidateType);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function dataUrlMime(dataUrl) {
|
||||
const match = /^data:([^;,]+)/i.exec(dataUrl ?? '');
|
||||
return match ? match[1].toLowerCase() : null;
|
||||
}
|
||||
|
||||
function startsWithBytes(bytes, signature, offset = 0) {
|
||||
return signature.every((byte, i) => bytes[offset + i] === byte);
|
||||
}
|
||||
|
||||
function headerText(bytes) {
|
||||
return new TextDecoder()
|
||||
.decode(bytes.subarray(0, 256))
|
||||
.trimStart()
|
||||
.toLowerCase();
|
||||
}
|
||||
|
||||
// Identify an image from its leading bytes. Used when the response carries no
|
||||
// usable content type (a CDN may serve `application/octet-stream`), so a real
|
||||
// icon isn't discarded just because its MIME is generic.
|
||||
function detectImageMime(bytes) {
|
||||
if (
|
||||
startsWithBytes(bytes, [0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a])
|
||||
) {
|
||||
return 'image/png';
|
||||
}
|
||||
if (startsWithBytes(bytes, [0xff, 0xd8, 0xff])) {
|
||||
return 'image/jpeg';
|
||||
}
|
||||
if (startsWithBytes(bytes, [0x47, 0x49, 0x46, 0x38])) {
|
||||
return 'image/gif';
|
||||
}
|
||||
if (
|
||||
startsWithBytes(bytes, [0x52, 0x49, 0x46, 0x46]) &&
|
||||
startsWithBytes(bytes, [0x57, 0x45, 0x42, 0x50], 8)
|
||||
) {
|
||||
return 'image/webp';
|
||||
}
|
||||
if (
|
||||
startsWithBytes(bytes, [0x00, 0x00, 0x01, 0x00]) ||
|
||||
startsWithBytes(bytes, [0x00, 0x00, 0x02, 0x00])
|
||||
) {
|
||||
return 'image/x-icon';
|
||||
}
|
||||
if (startsWithBytes(bytes, [0x42, 0x4d])) {
|
||||
return 'image/bmp';
|
||||
}
|
||||
if (
|
||||
startsWithBytes(bytes, [0x49, 0x49, 0x2a, 0x00]) ||
|
||||
startsWithBytes(bytes, [0x4d, 0x4d, 0x00, 0x2a])
|
||||
) {
|
||||
return 'image/tiff';
|
||||
}
|
||||
if (startsWithBytes(bytes, [0x66, 0x74, 0x79, 0x70], 4)) {
|
||||
const brand = new TextDecoder().decode(bytes.subarray(8, 12));
|
||||
if (brand === 'avif' || brand === 'avis') {
|
||||
return 'image/avif';
|
||||
}
|
||||
if (brand.startsWith('hei') || brand.startsWith('mif')) {
|
||||
return 'image/heic';
|
||||
}
|
||||
}
|
||||
|
||||
const head = headerText(bytes);
|
||||
if (
|
||||
head.startsWith('<svg') ||
|
||||
(head.startsWith('<?xml') && head.includes('<svg'))
|
||||
) {
|
||||
return 'image/svg+xml';
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
// Markup (an HTML login/challenge page, an XML error document) is a definitive
|
||||
// non-image: it must be rejected even when a candidate declared an image type.
|
||||
function looksLikeMarkup(bytes) {
|
||||
const head = headerText(bytes);
|
||||
return (
|
||||
head.startsWith('<!doctype') ||
|
||||
head.startsWith('<html') ||
|
||||
head.startsWith('<?xml')
|
||||
);
|
||||
}
|
||||
|
||||
async function sniffImageMime(blob) {
|
||||
const bytes = new Uint8Array(await blob.slice(0, 512).arrayBuffer());
|
||||
return { mime: detectImageMime(bytes), markup: looksLikeMarkup(bytes) };
|
||||
}
|
||||
|
||||
// True when a cached record holds decodable image bytes. The browser keys off
|
||||
// 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.
|
||||
function isUsableIcon(record) {
|
||||
return isImageMime(dataUrlMime(record?.dataUrl));
|
||||
}
|
||||
|
||||
function toDataUrl(blob, mime) {
|
||||
// FileReader derives the data URL's MIME from the blob, so stamp the resolved
|
||||
// type on a copy when the response arrived without a usable content type.
|
||||
const source =
|
||||
mime && mime !== blob.type ? new Blob([blob], { type: mime }) : blob;
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result);
|
||||
reader.onerror = () =>
|
||||
reject(reader.error ?? new Error('Unable to read icon'));
|
||||
reader.readAsDataURL(blob);
|
||||
reader.readAsDataURL(source);
|
||||
});
|
||||
}
|
||||
|
||||
@@ -120,15 +253,19 @@ export default class SiteIconsService extends Service {
|
||||
return cached;
|
||||
}
|
||||
|
||||
// A record whose bytes aren't an image (e.g. an HTML error page that got
|
||||
// cached) is treated as absent so it never renders as a broken tile.
|
||||
const usableCached = cached && isUsableIcon(cached) ? cached : null;
|
||||
|
||||
if (this.inFlight.has(origin)) {
|
||||
return cached ?? this.inFlight.get(origin);
|
||||
return usableCached ?? this.inFlight.get(origin);
|
||||
}
|
||||
|
||||
const work = this.resolve(origin, cached);
|
||||
this.inFlight.set(origin, work);
|
||||
|
||||
// Serve a stale-but-usable icon immediately while the refresh runs.
|
||||
return cached ?? work;
|
||||
return usableCached ?? work;
|
||||
}
|
||||
|
||||
async refresh(urlOrOrigin) {
|
||||
@@ -139,16 +276,33 @@ export default class SiteIconsService extends Service {
|
||||
try {
|
||||
const { candidates, themeColor } = await this.discover(origin);
|
||||
|
||||
const usableCached = cached && isUsableIcon(cached) ? cached : undefined;
|
||||
|
||||
for (const candidate of candidates) {
|
||||
try {
|
||||
const blob = await this.http.requestBlob(candidate.url);
|
||||
const dataUrl = await toDataUrl(blob);
|
||||
|
||||
// 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.
|
||||
const sniffed = await sniffImageMime(blob);
|
||||
const mime =
|
||||
sniffed.mime ??
|
||||
(sniffed.markup
|
||||
? null
|
||||
: resolveIconMime(blob.type, candidate.type));
|
||||
|
||||
if (!mime) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const dataUrl = await toDataUrl(blob, mime);
|
||||
|
||||
const record = {
|
||||
origin,
|
||||
href: candidate.url,
|
||||
dataUrl,
|
||||
mime: blob.type || candidate.type || null,
|
||||
mime,
|
||||
maskable: candidate.maskable,
|
||||
width: candidate.width || null,
|
||||
themeColor,
|
||||
@@ -162,9 +316,9 @@ export default class SiteIconsService extends Service {
|
||||
}
|
||||
}
|
||||
|
||||
return cached ?? undefined;
|
||||
return usableCached;
|
||||
} catch {
|
||||
return cached ?? undefined;
|
||||
return cached && isUsableIcon(cached) ? cached : undefined;
|
||||
} finally {
|
||||
this.inFlight.delete(origin);
|
||||
}
|
||||
@@ -269,6 +423,12 @@ export default class SiteIconsService extends Service {
|
||||
}
|
||||
|
||||
isStale(record) {
|
||||
return !record?.updatedAt || Date.now() - record.updatedAt > SITE_ICON_TTL;
|
||||
if (!record?.updatedAt || Date.now() - record.updatedAt > SITE_ICON_TTL) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Records cached before the content-type check (or poisoned by an HTML
|
||||
// response) are re-resolved so they heal without user intervention.
|
||||
return !isUsableIcon(record);
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,9 @@ import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import Avatar from 'dashtab/components/settings/integrations/avatar';
|
||||
|
||||
const PNG_ICON =
|
||||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
|
||||
|
||||
module(
|
||||
'Integration | Component | settings/integrations/avatar',
|
||||
function (hooks) {
|
||||
@@ -10,17 +13,14 @@ module(
|
||||
|
||||
test('it renders a resolved maskable site icon full-bleed', async function (assert) {
|
||||
this.integration = {
|
||||
icon: { dataUrl: 'data:image/png;base64,abc', maskable: true },
|
||||
icon: { dataUrl: PNG_ICON, maskable: true },
|
||||
};
|
||||
|
||||
await render(
|
||||
<template><Avatar @integration={{this.integration}} /></template>
|
||||
);
|
||||
|
||||
assert
|
||||
.dom('img')
|
||||
.exists()
|
||||
.hasAttribute('src', 'data:image/png;base64,abc');
|
||||
assert.dom('img').exists().hasAttribute('src', PNG_ICON);
|
||||
assert.dom('img').hasClass('object-cover');
|
||||
assert.dom('span').hasClass('icon-edge-no-highlight');
|
||||
assert.dom('svg').doesNotExist();
|
||||
@@ -28,14 +28,14 @@ module(
|
||||
|
||||
test('it renders a non-maskable site icon inset', async function (assert) {
|
||||
this.integration = {
|
||||
icon: { dataUrl: 'data:image/png;base64,abc', maskable: false },
|
||||
icon: { dataUrl: PNG_ICON, maskable: false },
|
||||
};
|
||||
|
||||
await render(
|
||||
<template><Avatar @integration={{this.integration}} /></template>
|
||||
);
|
||||
|
||||
assert.dom('img').hasAttribute('src', 'data:image/png;base64,abc');
|
||||
assert.dom('img').hasAttribute('src', PNG_ICON);
|
||||
assert.dom('img').hasClass('object-contain');
|
||||
});
|
||||
|
||||
|
||||
@@ -3,6 +3,9 @@ import { setupRenderingTest } from 'dashtab/tests/helpers';
|
||||
import { render } from '@ember/test-helpers';
|
||||
import SiteLinksItem from 'dashtab/components/site-links/item';
|
||||
|
||||
const PNG_ICON =
|
||||
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAC0lEQVR42mNkYPhfDwAChwGA60e6kgAAAABJRU5ErkJggg==';
|
||||
|
||||
module('Integration | Component | site-links/item', function (hooks) {
|
||||
setupRenderingTest(hooks);
|
||||
|
||||
@@ -68,7 +71,7 @@ module('Integration | Component | site-links/item', function (hooks) {
|
||||
status: 'ready',
|
||||
unreadCount: 1,
|
||||
icon: {
|
||||
dataUrl: 'data:image/png;base64,abc',
|
||||
dataUrl: PNG_ICON,
|
||||
maskable: true,
|
||||
themeColor: '#1d4ed8',
|
||||
},
|
||||
@@ -78,9 +81,7 @@ module('Integration | Component | site-links/item', function (hooks) {
|
||||
|
||||
assert.dom('[data-test-site-links-icon]').exists();
|
||||
assert.dom('[data-test-site-links-fallback]').doesNotExist();
|
||||
assert
|
||||
.dom('[data-test-site-links-icon]')
|
||||
.hasAttribute('src', 'data:image/png;base64,abc');
|
||||
assert.dom('[data-test-site-links-icon]').hasAttribute('src', PNG_ICON);
|
||||
assert.dom('[data-test-site-links-icon]').hasClass('object-cover');
|
||||
assert
|
||||
.dom('[data-test-site-links-tile]')
|
||||
@@ -92,7 +93,7 @@ module('Integration | Component | site-links/item', function (hooks) {
|
||||
label: 'forum.example.com',
|
||||
href: 'https://forum.example.com',
|
||||
status: 'ready',
|
||||
icon: { dataUrl: 'data:image/png;base64,abc', maskable: false },
|
||||
icon: { dataUrl: PNG_ICON, maskable: false },
|
||||
});
|
||||
|
||||
await render(<template><SiteLinksItem @item={{this.item}} /></template>);
|
||||
@@ -108,7 +109,7 @@ module('Integration | Component | site-links/item', function (hooks) {
|
||||
href: 'https://forum.example.com',
|
||||
status: 'ready',
|
||||
icon: {
|
||||
dataUrl: 'data:image/png;base64,abc',
|
||||
dataUrl: PNG_ICON,
|
||||
maskable: true,
|
||||
themeColor: 'red; background-image: url(evil)',
|
||||
},
|
||||
|
||||
@@ -6,6 +6,14 @@ 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);
|
||||
|
||||
@@ -152,4 +160,151 @@ module('Unit | Service | site-icons', function (hooks) {
|
||||
|
||||
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 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 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);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user