Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DNS fixes #229

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/helpers/src/dkim/dns-archive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export async function resolveDNSFromZKEmailArchive(name: string, type: string) {
}

// Get domain from full dns record name - $selector._domainkey.$domain.com
const domain = name.split('.').slice(-2).join('.');
const domain = name.split('.').slice(2).join('.');
const selector = name.split('.')[0];

const queryUrl = new URL(ZKEMAIL_DNS_ARCHIVER_API);
Expand Down
15 changes: 12 additions & 3 deletions packages/helpers/src/dkim/dns-over-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,16 +65,16 @@ export class DoH {
if (result.Status === DoH.DoHStatusNoError && result.Answer.length > 0) {
for (const ans of result.Answer) {
if (ans.type === DoH.DoHTypeTXT) {
let DKIMRecord = ans.data;
let dkimRecord = ans.data;
/*
Remove all double quotes
Some DNS providers wrap TXT records in double quotes,
and others like Cloudflare may include them. According to
TXT (potentially multi-line) and DKIM (Base64 data) standards,
we can directly remove all double quotes from the DKIM public key.
*/
DKIMRecord = DKIMRecord.replace(/"/g, "");
return DKIMRecord;
dkimRecord = dkimRecord.replace(/"/g, "");
return dkimRecord;
}
}
}
Expand Down Expand Up @@ -115,6 +115,15 @@ export async function resolveDNSHTTP(name: string, type: string) {
throw new CustomError('No DKIM record found in Google', 'ENODATA');
}

const regex = /p=([^;]*)/;
const match = regex.exec(googleResult);
if (match) {
const valueAfterP = match[1]; // Extracting the value after p=
if (valueAfterP === '') {
throw new CustomError('No DKIM record found in Google (empty p=)', 'ENODATA');
}
}

const cloudflareResult = await DoH.resolveDKIMPublicKey(
name,
DoHServer.Cloudflare
Expand Down
21 changes: 17 additions & 4 deletions packages/helpers/src/input-generators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type InputGenerationArgs = {
bodyMask?: number[];
};

type DKIMVerificationArgs = {
domain?: string;
enableSanitization?: boolean;
fallbackToZKEmailDNSArchive?: boolean;
};

function removeSoftLineBreaks(body: string[]): string[] {
const result = [];
let i = 0;
Expand Down Expand Up @@ -58,16 +64,23 @@ function removeSoftLineBreaks(body: string[]): string[] {
*
* @description Generate circuit inputs for the EmailVerifier circuit from raw email content
* @param rawEmail Full email content as a buffer or string
* @param params Arguments to control the input generation
* @param inputParams Arguments to control the input generation
* @param dkimVerificationArgs Arguments to control the DKIM verification
* @returns Circuit inputs for the EmailVerifier circuit
*/
export async function generateEmailVerifierInputs(
rawEmail: Buffer | string,
params: InputGenerationArgs = {},
inputParams: InputGenerationArgs = {},
dkimVerificationArgs: DKIMVerificationArgs = {},
) {
const dkimResult = await verifyDKIMSignature(rawEmail);
const dkimResult = await verifyDKIMSignature(
rawEmail,
dkimVerificationArgs.domain,
dkimVerificationArgs.enableSanitization,
dkimVerificationArgs.fallbackToZKEmailDNSArchive,
);

return generateEmailVerifierInputsFromDKIMResult(dkimResult, params);
return generateEmailVerifierInputsFromDKIMResult(dkimResult, inputParams);
}

/**
Expand Down
Loading