Skip to content
New issue

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

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

Already on GitHub? # to your account

fix node:dns resolveTxt for multiple quotes #3330

Merged
merged 1 commit into from
Jan 13, 2025
Merged
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
23 changes: 22 additions & 1 deletion src/node/internal/internal_dns_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,32 @@ export function normalizeSrv({ data }: Answer): SRV {
};
}

// This regex works by:
//
// `"` - Matches an opening quote
// ([^"]|"(?!"))* - Matches either:
// [^"] - Any character that's not a quote
// "(?!") - A quote that's not followed by another quote
// `"` - Matches a closing quote
// /g - Global flag to match all occurrences
const SPLIT_REGEX = /"([^"]|"(?!"))*"/g;

export function normalizeTxt({ data }: Answer): string[] {
// Each entry has quotation marks as a prefix and suffix.
// Node.js APIs doesn't have them.
if (data.startsWith('"') && data.endsWith('"')) {
return [data.replaceAll('"', '')];
// If the input starts and ends with a quotation mark, we need to split
// each occurrence and remove the leading/trailing characters.
// For example, for the input `"test""test""test with " quote"`
// It returns: ['test', 'test', 'test with " quote']
return (
data.match(SPLIT_REGEX)?.map((s) => {
if (s.startsWith('"') && s.endsWith('"')) {
return s.slice(1, -1);
}
return s;
}) ?? []
);
}
return [data];
}
Loading