Skip to content

Commit

Permalink
fix node:dns resolveTxt for multiple quotes (#3330)
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig authored Jan 13, 2025
1 parent 213a256 commit 8cb7ffd
Showing 1 changed file with 22 additions and 1 deletion.
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];
}

0 comments on commit 8cb7ffd

Please sign in to comment.