Skip to content

Commit

Permalink
[core-http] delay saving of DOM parser error namespace
Browse files Browse the repository at this point in the history
We eagerly save the namespace of "parsererror" element on module load. It's
unnecessary because in most scenarios we would not encounter a parse error.
Furthermore, it caused Content Security Policy error in Chrome version 86.0 or
newer because the returned DOM object for parser errors contains some CSS
styles.

This change fixes the issue by lazily loading the error namespace when the
parsed DOM document contains "parsererror" elements.

Fixes Azure#13268
  • Loading branch information
jeremymeng committed Jan 25, 2021
1 parent 832c98f commit e1cbf2f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
1 change: 1 addition & 0 deletions sdk/core/core-http/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.2.3 (Unreleased)

- Browser XML parser now lazily load parser error namespace into cache. Fixed [issue 13268](https://github.com/Azure/azure-sdk-for-js/issues/13268)

## 1.2.2 (2021-01-07)

Expand Down
30 changes: 20 additions & 10 deletions sdk/core/core-http/src/util/xml.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,29 @@ export function parseXML(str: string, opts: SerializerOptions = {}): Promise<any
}
}

let errorNS = "";
try {
errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI!;
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
let errorNS: string | undefined;

function getErrorNamespace(): string {
if (errorNS === undefined) {
try {
errorNS =
parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI! ?? "";
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
errorNS = "";
}
}
return errorNS;
}

function throwIfError(dom: Document): void {
if (errorNS) {
const parserErrors = dom.getElementsByTagNameNS(errorNS, "parsererror");
if (parserErrors.length) {
throw new Error(parserErrors.item(0)!.innerHTML);
const parserErrors = dom.getElementsByTagName("parsererror");
if (parserErrors.length > 0 && getErrorNamespace()) {
for (let i = 0; i < parserErrors.length; i++) {
if (parserErrors[i].namespaceURI === errorNS) {
throw new Error(parserErrors[i].innerHTML);
}
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions sdk/core/core-http/test/xmlTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ describe("XML serializer", function() {
assert.deepStrictEqual(xml, ``);
});

it("with <parsererror> element", async function() {
const xml: any = await parseXML(`<errors><parsererror>error 1</parsererror></errors>`);
assert.deepStrictEqual(xml, { parsererror: "error 1" });
});

it("with empty element with attribute", async function() {
const xml: any = await parseXML(`<fruit healthy="true" />`);
assert.deepStrictEqual(xml, {
Expand Down
31 changes: 21 additions & 10 deletions sdk/core/core-xml/src/xml.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,30 @@ export function parseXML(str: string, opts: XmlOptions = {}): Promise<any> {
}
}

let errorNS = "";
try {
errorNS = parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI!;
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
let errorNS: string | undefined;

function getErrorNamespace(): string {
if (errorNS === undefined) {
try {
errorNS =
parser.parseFromString("INVALID", "text/xml").getElementsByTagName("parsererror")[0]
.namespaceURI! ?? "";
} catch (ignored) {
// Most browsers will return a document containing <parsererror>, but IE will throw.
errorNS = "";
}
}

return errorNS;
}

function throwIfError(dom: Document): void {
if (errorNS) {
const parserErrors = dom.getElementsByTagNameNS(errorNS, "parsererror");
if (parserErrors.length) {
throw new Error(parserErrors.item(0)!.innerHTML);
const parserErrors = dom.getElementsByTagName("parsererror");
if (parserErrors.length > 0 && getErrorNamespace()) {
for (let i = 0; i < parserErrors.length; i++) {
if (parserErrors[i].namespaceURI === errorNS) {
throw new Error(parserErrors[i].innerHTML);
}
}
}
}
Expand Down

0 comments on commit e1cbf2f

Please sign in to comment.