Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -251,9 +251,17 @@ export function buildRfc2822Message(input: {
readonly referencesInternetMessageIds?: readonly string[];
readonly attachments?: ReadonlyArray<{ filename: string; mimeType: string; contentBase64: string }>;
}): string {
const encodeWord = (value: string): string =>
/[^\x20-\x7e]/.test(value) ? `=?UTF-8?B?${Buffer.from(value, "utf8").toString("base64")}?=` : value;
const formatAddress = (a: MappedEmailAddress): string => (a.name ? `${encodeWord(a.name)} <${a.address}>` : a.address);
const encodeWord = (value: string, label: string): string => {
assertSafeHeaderValue(value, label);
return /[^\x20-\x7e]|[<>,"\\]/.test(value) ? `=?UTF-8?B?${Buffer.from(value, "utf8").toString("base64")}?=` : value;
};
const formatAddress = (a: MappedEmailAddress): string => {
assertSafeEmailAddress(a.address);
return a.name ? `${encodeWord(a.name, "display name")} <${a.address}>` : a.address;
};
assertSafeEmailAddress(input.fromAddress);
assertSafeHeaderValue(input.subject, "subject");
const headers: string[] = [
`From: ${formatAddress({ address: input.fromAddress, name: input.fromName })}`,
@@ -261,12 +269,12 @@ export function buildRfc2822Message(input: {
];
if (input.cc?.length) headers.push(`Cc: ${input.cc.map(formatAddress).join(", ")}`);
if (input.bcc?.length) headers.push(`Bcc: ${input.bcc.map(formatAddress).join(", ")}`);
headers.push(`Subject: ${encodeWord(input.subject)}`);
headers.push(`Subject: ${encodeWord(input.subject, "subject")}`);
if (input.inReplyToInternetMessageId) {
headers.push(`In-Reply-To: <${input.inReplyToInternetMessageId}>`);
headers.push(`In-Reply-To: <${safeInternetMessageId(input.inReplyToInternetMessageId)}>`);
}
if (input.referencesInternetMessageIds?.length) {
headers.push(`References: ${input.referencesInternetMessageIds.map((id) => `<${id}>`).join(" ")}`);
headers.push(`References: ${input.referencesInternetMessageIds.map((id) => `<${safeInternetMessageId(id)}>`).join(" ")}`);
}
headers.push("MIME-Version: 1.0");
@@ -279,14 +287,63 @@ export function buildRfc2822Message(input: {
const boundary = `hg_${Math.abs(hashCode(input.subject + input.fromAddress + String(input.to.length)))}_mime`;
headers.push(`Content-Type: multipart/mixed; boundary="${boundary}"`);
const attachmentParts = input.attachments
.map(
(a) =>
`--${boundary}\r\nContent-Type: ${a.mimeType}; name="${a.filename}"\r\nContent-Disposition: attachment; filename="${a.filename}"\r\nContent-Transfer-Encoding: base64\r\n\r\n${a.contentBase64}`,
)
.map((a) => {
const filename = safeMimeFilename(a.filename);
const mimeType = safeMimeType(a.mimeType);
const contentBase64 = safeBase64Body(a.contentBase64);
return `--${boundary}\r\nContent-Type: ${mimeType}; name="${filename}"\r\nContent-Disposition: attachment; filename="${filename}"\r\nContent-Transfer-Encoding: base64\r\n\r\n${contentBase64}`;
})
.join("\r\n");
return `${headers.join("\r\n")}\r\n\r\n--${boundary}\r\n${htmlPart}\r\n${attachmentParts}\r\n--${boundary}--`;
}
function assertSafeHeaderValue(value: string, label: string): void {
if (/[\u0000-\u001f\u007f]/u.test(value) || Buffer.byteLength(value, "utf8") > 998) {
throw new TypeError(`RFC-2822 ${label} contains invalid control characters or exceeds the header limit.`);
}
}
function assertSafeEmailAddress(address: string): void {
assertSafeHeaderValue(address, "email address");
if (address.length > 320 || !/^[^\s@<>,";:]+@[^\s@<>,";:]+$/u.test(address)) {
throw new TypeError("RFC-2822 email address is invalid.");
}
}
function safeInternetMessageId(value: string): string {
assertSafeHeaderValue(value, "message id");
const normalized = value.trim().replace(/^<|>$/g, "");
if (!normalized || !/^[^\s<>]+$/u.test(normalized)) {
throw new TypeError("RFC-2822 message id is invalid.");
}
return normalized;
}
function safeMimeFilename(value: string): string {
assertSafeHeaderValue(value, "attachment filename");
const normalized = value.replace(/["\\]/g, "_").trim();
if (!normalized || Buffer.byteLength(normalized, "utf8") > 255) {
throw new TypeError("RFC-2822 attachment filename is invalid.");
}
return normalized;
}
function safeMimeType(value: string): string {
assertSafeHeaderValue(value, "attachment MIME type");
if (!/^[a-z0-9][a-z0-9!#$&^_.+-]*\/[a-z0-9][a-z0-9!#$&^_.+-]*$/iu.test(value)) {
throw new TypeError("RFC-2822 attachment MIME type is invalid.");
}
return value.toLowerCase();
}
function safeBase64Body(value: string): string {
const normalized = value.replace(/\s+/gu, "");
if (!normalized || normalized.length % 4 !== 0 || !/^[A-Za-z0-9+/]+={0,2}$/u.test(normalized)) {
throw new TypeError("RFC-2822 attachment body is not valid base64.");
}
return normalized;
}
function hashCode(value: string): number {
let hash = 0;
for (let i = 0; i < value.length; i += 1) {