Skip to content

Commit 5f47eb6

Browse files
committed
fix: light validation of ad responses to as to not crash the page
1 parent b903c81 commit 5f47eb6

2 files changed

Lines changed: 55 additions & 1 deletion

File tree

‎apps/site/hooks/__tests__/useCarbonAd.test.jsx‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ const { default: useCarbonAd } = await import('#site/hooks/useCarbonAd');
1111

1212
const ad = {
1313
company: 'Company',
14+
description: 'Description',
15+
callToAction: 'Learn more',
1416
statlink: 'https://srv.buysellads.com/ads/click/x/1',
1517
statview: 'https://srv.buysellads.com/ads/view/x/1',
1618
};
@@ -141,6 +143,52 @@ describe('useCarbonAd', () => {
141143
});
142144
});
143145

146+
describe('validation', () => {
147+
const ValidationProbe = () => {
148+
const { ad } = useCarbonAd('CWBI6KJY');
149+
150+
return <div>{ad ? 'ad' : 'no ad'}</div>;
151+
};
152+
153+
const renderWith = async creative => {
154+
globalThis.fetch.mock.mockImplementation(async () => ({
155+
ok: true,
156+
json: async () => ({ ads: [creative] }),
157+
}));
158+
159+
const { container } = render(<ValidationProbe />);
160+
await flush();
161+
162+
return container.textContent;
163+
};
164+
165+
it('accepts an ad without a title', async () => {
166+
assert.equal(await renderWith(ad), 'ad');
167+
});
168+
169+
it('accepts an ad with a title', async () => {
170+
assert.equal(await renderWith({ ...ad, title: 'Title' }), 'ad');
171+
});
172+
173+
for (const field of ['statlink', 'description', 'callToAction']) {
174+
it(`rejects a missing ${field}`, async () => {
175+
assert.equal(await renderWith({ ...ad, [field]: undefined }), 'no ad');
176+
});
177+
178+
it(`rejects a non-string ${field}`, async () => {
179+
assert.equal(await renderWith({ ...ad, [field]: { a: 1 } }), 'no ad');
180+
});
181+
}
182+
183+
it('rejects a non-string title', async () => {
184+
assert.equal(await renderWith({ ...ad, title: { a: 1 } }), 'no ad');
185+
});
186+
187+
it('rejects an empty creative', async () => {
188+
assert.equal(await renderWith({}), 'no ad');
189+
});
190+
});
191+
144192
it('sends the view beacon after the ad was at least 50% visible for one second', async t => {
145193
await renderAd(t);
146194

‎apps/site/hooks/useCarbonAd.ts‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ export const adColors = (ad: CarbonAd) =>
2525
'--ad-cta-text-dark': ad.ctaTextColorDark ?? ad.ctaTextColor,
2626
}) as CSSProperties;
2727

28+
// The response is untrusted, and rendering a non-string would crash the page
29+
const isValidAd = (ad?: Record<string, unknown>) =>
30+
[ad?.statlink, ad?.description, ad?.callToAction].every(
31+
field => field && typeof field === 'string'
32+
) && typeof (ad?.title ?? '') === 'string';
33+
2834
const useCarbonAd = (zone: string, media?: string) => {
2935
const [ad, setAd] = useState<CarbonAd | null>(null);
3036
const [loading, setLoading] = useState(true);
@@ -47,7 +53,7 @@ const useCarbonAd = (zone: string, media?: string) => {
4753

4854
fetch(url, { signal: controller.signal })
4955
.then(response => (response.ok ? response.json() : null))
50-
.then(data => data?.ads?.[0]?.statlink && setAd(data.ads[0]))
56+
.then(data => isValidAd(data?.ads?.[0]) && setAd(data.ads[0]))
5157
.catch(() => {})
5258
.finally(() => {
5359
if (!controller.signal.aborted) {

0 commit comments

Comments
 (0)