-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtmlParser.js
More file actions
130 lines (115 loc) · 3.9 KB
/
htmlParser.js
File metadata and controls
130 lines (115 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
var fs = require('fs')
var htmlParser = require('parse5');
// This parser will return a list of ids of DOM elements that accept user inputs
module.exports = function (htmlFileName) {
var data = fs.readFileSync(htmlFileName, 'utf-8');
// There should only be one HTML node
var htmlNode = getNodes('html', htmlParser.parse(data).childNodes)[0];
var bodyNode = getNodes('body', htmlNode.childNodes)[0];
var listOfFormNodes = [].concat.apply([],DFS_formNodes(bodyNode, []));
var listOfVulnerableDomIds = [];
for (var index in listOfFormNodes) {
var formNode = listOfFormNodes[index];
var listOfInputIds = getVulnerableInputIds(formNode);
var listOfTextAreaIds = getVulnerableTextAreaIds(formNode);
listOfVulnerableDomIds = listOfVulnerableDomIds.concat(listOfInputIds.concat(listOfTextAreaIds));
}
return listOfVulnerableDomIds;
}
// ============For testing================
// var name = process.argv[2];
// fs.readFile(name, 'utf8', function (err,data) {
// if (err) {
// return console.log(err);
// }
//
// // There should only be one HTML node
// var htmlNode = getNodes('html', htmlParser.parse(data).childNodes)[0];
// var bodyNode = getNodes('body', htmlNode.childNodes)[0];
//
// var listOfFormNodes = [].concat.apply([],DFS_formNodes(bodyNode, []));
// var listOfVulnerableDomIds = [];
//
// for (var index in listOfFormNodes) {
// var formNode = listOfFormNodes[index];
// var listOfInputIds = getVulnerableInputIds(formNode);
// var listOfTextAreaIds = getVulnerableTextAreaIds(formNode);
// listOfVulnerableDomIds = listOfVulnerableDomIds.concat(listOfInputIds.concat(listOfTextAreaIds));
// }
// console.log(listOfVulnerableDomIds);
// });
function DFS_formNodes(node, accum) {
if (node.childNodes.length === 0) {
return [];
} else { // nodes has childNodes,
// Need to recursively look for formNodes
var listOfFirstLevelFormNodes = getNodes('form', node.childNodes);
accum.push(listOfFirstLevelFormNodes);
var otherChildNodes = node.childNodes.filter(dummy);
for (var index in otherChildNodes) {
var child = otherChildNodes[index];
var childList = DFS_formNodes(child, accum);
accum.push(childList);
}
return accum;
}
}
function dummy(node) {
return node.nodeName !== 'form' && node.nodeName !== 'script' && node.nodeName !== '#text';
}
// Returns the node, where nodeName === name
function getNodes(name, arr) {
var listOfElements = [];
for (var index in arr) {
var node = arr[index];
if (node.nodeName === name) {
listOfElements.push(node);
}
}
return listOfElements
}
function getVulnerableTextAreaIds(formNode) {
var listOfTextAreaNodes = getNodes('textarea', formNode.childNodes);
var listOfIds = [];
for (var index in listOfTextAreaNodes) {
var textAreaNode = listOfTextAreaNodes[index];
listOfIds.push(getIdOfNode(textAreaNode));
}
return listOfIds;
}
function getVulnerableInputIds(formNode) {
var listOfInputTextType = getNodes('input', formNode.childNodes).filter(isInputTextType);
var listOfIds = [];
for (var index in listOfInputTextType) {
var inputTextNode = listOfInputTextType[index];
listOfIds.push(getIdOfNode(inputTextNode));
}
return listOfIds;
}
// This function assumes (isInputTextType(node) === true ||
// node.nodeName === 'textarea') && the node's id is defined
function getIdOfNode(node) {
var attributes = node.attrs;
for (var index in attributes) {
var attribute = attributes[index];
if (attribute.name === 'id') {
return attribute.value;
}
}
}
function isInputTextType(node) {
if (node.nodeName === 'input') {
return isTextType(node);
}
}
// This function assumes that node.nodeName === 'input'
function isTextType(node) {
var attributes = node.attrs;
for (var index in attributes) {
var attribute = attributes[index];
if (attribute.name === 'type') {
return attribute.value === 'text';
}
}
return false;
}