-
Notifications
You must be signed in to change notification settings - Fork 84
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
Add an option to resolve DNS from ZKEmail Archive when HTTP DNS fails #227
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
136a8c8
feat: enable dns archiver flag in tryVerifyDKIM
saleel ec8bfd3
chore: refactor DoH resolver
saleel 5345da8
chore: update dns over https
saleel 8c63422
feat: add fallback to archive on https failure
saleel 4e692ff
chore: add tests for fallbackToZKEmailDNSArchive
saleel c8e98af
chore: refactor fallback tests
saleel 6cc8b8a
chore: remove unused imports
saleel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { CustomError } from "../lib/mailauth/tools"; | ||
|
||
const ZKEMAIL_DNS_ARCHIVER_API = "https://archive.prove.email/api/key"; | ||
|
||
export async function resolveDNSFromZKEmailArchive(name: string, type: string) { | ||
if (type !== "TXT") { | ||
throw new Error(`ZK Email Archive only supports TXT records - got ${type}`); | ||
} | ||
|
||
// Get domain from full dns record name - $selector._domainkey.$domain.com | ||
const domain = name.split(".").slice(-2).join("."); | ||
const selector = name.split(".")[0]; | ||
|
||
const queryUrl = new URL(ZKEMAIL_DNS_ARCHIVER_API); | ||
queryUrl.searchParams.set("domain", domain); | ||
|
||
const resp = await fetch(queryUrl); | ||
const data = await resp.json(); | ||
|
||
const dkimRecord = data.find((record: any) => record.selector === selector); | ||
|
||
if (!dkimRecord) { | ||
throw new CustomError( | ||
`DKIM record not found for domain ${domain} and selector ${selector} in ZK Email Archive.`, | ||
"ENODATA" | ||
); | ||
} | ||
|
||
return [dkimRecord.value]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import { CustomError } from "../lib/mailauth/tools"; | ||
|
||
// DoH servers list | ||
export enum DoHServer { | ||
// Google Public DNS | ||
Google = "https://dns.google/resolve", | ||
// Cloudflare DNS | ||
Cloudflare = "https://cloudflare-dns.com/dns-query", | ||
} | ||
|
||
/** | ||
* DNS over HTTPS (DoH) resolver | ||
* | ||
* @export | ||
* @class DoH | ||
*/ | ||
export class DoH { | ||
// DNS response codes | ||
static DoHStatusNoError = 0; | ||
// DNS RR types | ||
static DoHTypeTXT = 16; | ||
|
||
/** | ||
* Resolve DKIM public key from DNS | ||
* | ||
* @static | ||
* @param {string} name DKIM record name (e.g. 20230601._domainkey.gmail.com) | ||
* @param {string} DNSServer DNS over HTTPS API URL | ||
* @return {*} {(Promise<string | null>)} DKIM public key or null if not found | ||
* @memberof DoH | ||
*/ | ||
public static async resolveDKIMPublicKey( | ||
name: string, | ||
DNSServer: string | ||
): Promise<string | null> { | ||
if (!DNSServer.startsWith("https://")) { | ||
DNSServer = "https://" + DNSServer; | ||
} | ||
if (DNSServer.endsWith("/")) { | ||
DNSServer = DNSServer.slice(0, -1); | ||
} | ||
const resp = await fetch( | ||
DNSServer + | ||
"?" + | ||
new URLSearchParams({ | ||
name: name, | ||
// DKIM public key record type is TXT | ||
type: DoH.DoHTypeTXT.toString(), | ||
}), | ||
{ | ||
headers: { | ||
accept: "application/dns-json", | ||
}, | ||
} | ||
); | ||
if (resp.status === 200) { | ||
const out = await resp.json(); | ||
if ( | ||
typeof out === "object" && | ||
out !== null && | ||
"Status" in out && | ||
"Answer" in out | ||
) { | ||
const resp = out as DoHResponse; | ||
if (resp.Status === DoH.DoHStatusNoError && resp.Answer.length > 0) { | ||
for (const ans of resp.Answer) { | ||
if (ans.type === DoH.DoHTypeTXT) { | ||
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; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
} | ||
|
||
interface DoHResponse { | ||
Status: number; // NOERROR - Standard DNS response code (32 bit integer). | ||
TC: boolean; // Whether the response is truncated | ||
AD: boolean; // Whether all response data was validated with DNSSEC | ||
CD: boolean; // Whether the client asked to disable DNSSEC | ||
Question: Question[]; | ||
Answer: Answer[]; | ||
Comment: string; | ||
} | ||
|
||
interface Question { | ||
name: string; // FQDN with trailing dot | ||
type: number; // A - Standard DNS RR type. 5:CNAME, 16:TXT | ||
} | ||
|
||
interface Answer { | ||
name: string; // Always matches name in the Question section | ||
type: number; // A - Standard DNS RR type. 5:CNAME, 16:TXT | ||
TTL: number; // Record's time-to-live in seconds | ||
data: string; // Record data | ||
} | ||
|
||
export async function resolveDNSHTTP(name: string, type: string) { | ||
if (type !== "TXT") { | ||
throw new Error(`DNS over HTTP: Only type TXT is supported, got ${type}`); | ||
} | ||
const googleResult = await DoH.resolveDKIMPublicKey(name, DoHServer.Google); | ||
if (!googleResult) { | ||
throw new CustomError("No DKIM record found in Google", "ENODATA"); | ||
} | ||
|
||
const cloudflareResult = await DoH.resolveDKIMPublicKey( | ||
name, | ||
DoHServer.Cloudflare | ||
); | ||
|
||
// Log an error if there is a mismatch in the result | ||
if (googleResult !== cloudflareResult) { | ||
console.error( | ||
"DKIM record mismatch between Google and Cloudflare! Using Google result." | ||
); | ||
} | ||
|
||
return [googleResult]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is basically lib/mailauth/Doh.ts moved here