Skip to content

Commit 517fed4

Browse files
authored
chore(cloud-function): send X-Robots-Tag: noindex on pimg (#13503)
* chore(cloud-function): set "X-Robots-Tag: noindex, nofollow" on ad images * chore(cloud-function): check method on pimg endpoint * chore(cloud-function): handle failed pimg fetch
1 parent 41b7111 commit 517fed4

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

cloud-function/src/handlers/proxy-bsa.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,18 +101,38 @@ export async function proxyBSA(req: Request, res: Response) {
101101
console.error(e);
102102
}
103103
} else if (pathname.startsWith("/pimg/")) {
104+
if (req.method !== "GET") {
105+
return res.sendStatus(405).end();
106+
}
107+
104108
const src = coder.decodeAndVerify(
105109
decodeURIComponent(pathname.substring("/pimg/".length))
106110
);
111+
107112
if (!src) {
113+
console.warn("[pimg] Invalid src");
108114
return res.sendStatus(400).end();
109115
}
110-
const { buf, contentType } = await fetchImage(src);
116+
117+
const { status, buf, contentType } = await fetchImage(src);
118+
119+
if (status >= 400) {
120+
console.warn(`[pimg] Image fetch failed: HTTP ${status}`);
121+
return res
122+
.status(status)
123+
.set({
124+
"cache-control": "no-store",
125+
"content-type": contentType,
126+
})
127+
.end(Buffer.from(buf));
128+
}
129+
111130
return res
112131
.status(200)
113132
.set({
114133
"cache-control": "max-age=86400",
115134
"content-type": contentType,
135+
"x-robots-tag": "noindex, nofollow",
116136
})
117137
.end(Buffer.from(buf));
118138
}

libs/pong/image.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export function fetchImage(src: string): Promise<{
2+
status: number;
23
buf: ArrayBuffer;
34
contentType: string;
45
}>;

libs/pong/image.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
/* global fetch */
22
export async function fetchImage(src) {
3-
const imageResponse = await fetch(src);
4-
const imageBuffer = await imageResponse.arrayBuffer();
5-
const contentType = imageResponse.headers.get("content-type");
6-
return { buf: imageBuffer, contentType };
3+
const res = await fetch(src);
4+
const status = res.status;
5+
const buf = await res.arrayBuffer();
6+
const contentType = res.headers.get("content-type");
7+
return { status, buf, contentType };
78
}

0 commit comments

Comments
 (0)