Files
burrow-server/lib/emoji.js
T
SnipzilandClaude Opus 5.5 ee06e8ca77 fix(ci): pass the security invariants and clear the code-scanning alerts
- Remove package-lock.json: the server uses no npm packages, which the
  invariants check enforces (no supply chain to attack).
- Allow the two hosts the server really calls in .github/allowed-hosts.json:
  cdn.jsdelivr.net (Twemoji pictures, lib/emoji.js) and vision.googleapis.com
  (the optional SafeSearch check, lib/image-moderation.js). The github.com
  mention in lib/emoji.js was only a comment, now reworded.
- logText strips line feeds with .replace(/\n/g, ""), the form CodeQL's
  js/log-injection recognises as a sanitiser. The old [\r\n]+ to a space was
  just as safe, but not recognised, so every log line using it was flagged
  (34 alerts on main, 35 on this branch).
- /authenticate: the saved sign-in is decided by what the server finds for
  the refresh token, not by whether the request has one
  (js/user-controlled-bypass). A token that is not good is still refused
  with error_code 100.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-17 21:55:48 -04:00

81 lines
2.6 KiB
JavaScript

"use strict";
const fs = require("fs");
const path = require("path");
const config = require("../config.js");
const {logText} = require("./logtext.js");
// pictures of emoji, for the patched client's chat bubbles (gui.ChatBalloon):
// the game's font has none, so an emoji typed in chat (with Windows' emoji
// panel, say) is drawn as one of these. they are Twemoji's (CC-BY 4.0,
// jdecked/twemoji on GitHub), asked of config.emojiUpstream once
// each and kept in data/emoji. a name is the emoji's code points in hex,
// joined by "-", as Twemoji names its files ("1f600", "1f468-200d-1f469").
const NAME = /^[0-9a-f]{2,6}(?:-[0-9a-f]{2,6}){0,9}$/;
const MAX_BYTES = 64 * 1024;
const MAX_MISSING = 5000;
const PNG_SIGNATURE = Buffer.from([0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A]);
const DIR = path.join(config.dataDir, "emoji");
const missing = new Set(); // names the upstream does not have
const pending = new Map(); // name -> promise, so that one is asked for once
const isName = name => typeof name === "string" && NAME.test(name);
const fileOf = name => path.join(DIR, `${name}.png`);
const fetchUpstream = async name => {
const res = await fetch(`${config.emojiUpstream}${name}.png`, {signal: AbortSignal.timeout(config.upstreamTimeoutMs)});
if (!res.ok) {
if (res.status === 404 || res.status === 403) {
if (missing.size >= MAX_MISSING) {
missing.clear();
}
missing.add(name);
}
return null;
}
const body = Buffer.from(await res.arrayBuffer());
// a PNG, and a small one, or nothing is kept
if (body.length > MAX_BYTES || !body.subarray(0, 8).equals(PNG_SIGNATURE)) {
console.log(`[emoji] not a picture: ${logText(name)}`);
return null;
}
try {
fs.mkdirSync(DIR, {recursive: true});
const tmp = `${fileOf(name)}.${process.pid}.tmp`;
fs.writeFileSync(tmp, body);
fs.renameSync(tmp, fileOf(name));
}
catch (err) {
console.log(`[emoji] could not keep ${logText(name)}: ${logText(err.message)}`);
}
return body;
};
// the picture for `name`: a Buffer, or null. `mayFetch()`: whether one not
// kept yet may be asked of the upstream for this request (asked only then).
const get = async (name, mayFetch) => {
if (!isName(name) || missing.has(name)) {
return null;
}
try {
return await fs.promises.readFile(fileOf(name));
}
catch (err) {
if (err.code !== "ENOENT") {
return null;
}
}
if (!mayFetch()) {
return null;
}
if (!pending.has(name)) {
pending.set(name, fetchUpstream(name).catch(() => null).finally(() => pending.delete(name)));
}
return pending.get(name);
};
module.exports = {get, isName};