Skip to content

Commit

Permalink
helpers: add sanitizer insert13Before10
Browse files Browse the repository at this point in the history
  • Loading branch information
saleel committed Apr 2, 2024
1 parent 73a907e commit 90d572b
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/helpers/src/dkim/sanitizers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ function setHeaderValue(email: string, header: string, value: string) {

// Google sets their own Message-ID and put the original one in X-Google-Original-Message-ID
// when ARC forwarding
// TODO: Add test for this
function revertGoogleMessageId(email: string): string {
// (Optional check) This only happens when google does ARC
if (!email.includes("ARC-Authentication-Results")) {
Expand All @@ -31,6 +32,7 @@ function revertGoogleMessageId(email: string): string {
return email;
}


// Remove labels inserted to Subject - `[ListName] Newsletter 2024` to `Newsletter 2024`
function removeLabels(email: string): string {
// Replace Subject: [label] with Subject:
Expand All @@ -39,9 +41,30 @@ function removeLabels(email: string): string {
}


// Sometimes, newline encodings re-encode \r\n as just \n, so re-insert the \r
// TODO: Add test for this
export function insert13Before10(a: Uint8Array): Uint8Array {
let ret = new Uint8Array(a.length + 1000);
let j = 0;
for (let i = 0; i < a.length; i++) {
// Ensure each \n is preceded by a \r
if (a[i] === 10 && i > 0 && a[i - 1] !== 13) {
ret[j] = 13;
j++;
}
ret[j] = a[i];
j++;
}

return ret.slice(0, j);
}



const sanitizers = [
revertGoogleMessageId,
removeLabels,
insert13Before10,
];


Expand Down

0 comments on commit 90d572b

Please sign in to comment.