Skip to content

Commit

Permalink
fix security bug
Browse files Browse the repository at this point in the history
  • Loading branch information
amitguptagwl committed Jun 5, 2023
1 parent e7728af commit 39b0e05
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
18 changes: 18 additions & 0 deletions spec/entities_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,24 @@ describe("XMLParser Entities", function() {

expect(result).toEqual(expected);
});
it("should throw error if an entity name contains special char", function() {
const xmlData = `
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE note [
<!ENTITY nj$ "writer;">
<!ENTITY wr?er "Writer: Donald Duck.">
]>`;

const options = {
processEntities: true,
};

expect(() =>{
const parser = new XMLParser(options);
parser.parse(xmlData);
}).toThrowError("Invalid character $ in entity name")
});
});

describe("XMLParser External Entites", function() {
Expand Down
14 changes: 13 additions & 1 deletion src/xmlparser/DocTypeReader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function readDocType(xmlData, i){
i += 7;
[entityName, val,i] = readEntityExp(xmlData,i+1);
if(val.indexOf("&") === -1) //Parameter entities are not supported
entities[ entityName ] = {
entities[ validateEntityName(entityName) ] = {
regx : RegExp( `&${entityName};`,"g"),
val: val
};
Expand Down Expand Up @@ -140,4 +140,16 @@ function isNotation(xmlData, i){
return false
}

//an entity name should not contains special characters that may be used in regex
//Eg !?\\\/[]$%{}^&*()<>
const specialChar = "!?\\\/[]$%{}^&*()<>";

function validateEntityName(name){
for (let i = 0; i < specialChar.length; i++) {
const ch = specialChar[i];
if(name.indexOf(ch) !== -1) throw new Error(`Invalid character ${ch} in entity name`);
}
return name;
}

module.exports = readDocType;

0 comments on commit 39b0e05

Please sign in to comment.