Skip to content

Commit 1dc3669

Browse files
authored
Merge pull request #43 from bit-docs/escape
Escape _all_ closing script tags
2 parents 1de6e14 + a224ce5 commit 1dc3669

File tree

2 files changed

+62
-41
lines changed

2 files changed

+62
-41
lines changed

build/make_default_helpers.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
279279
//this allows linking to a specific section with the hash syntax (#27)
280280
hashParts = name.split("#");
281281
name = hashParts.shift();
282-
282+
283283
docObject = docMap[name]
284284
if (docObject) {
285285
linkText = parts && parts[2] ? parts[2] : docObject.title || name;
@@ -408,7 +408,9 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
408408
},
409409
docObjectString: function(){
410410
this.pathToRoot = pathToRoot(this.name);
411-
return JSON.stringify(deepExtendWithoutBody(this)).replace("</script>", "<\\/script>");
411+
412+
return JSON.stringify(deepExtendWithoutBody(this))
413+
.replace(/<\/script>/g, "<\\/script>");
412414
},
413415
pathToDest: function(){
414416
var currentDir = path.dirname( path.join(config.dest, docsFilename( getCurrent(), config)) );

html_test.js

Lines changed: 58 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -96,48 +96,67 @@ describe("documentjs/lib/generators/html",function(){
9696
});
9797
});
9898

99-
it("closing script tags are properly escaped", function(done){
99+
it("closing script tags are properly escaped", function() {
100100
this.timeout(40000);
101-
rmdir(path.join(__dirname,"test","tmp"), function(e){
102-
if(e) {
103-
return done(e);
104-
}
105-
var options = {
106-
dest: path.join(__dirname, "test","tmp"),
107-
parent: "index",
108-
templateRender: true
109-
};
110-
111-
112-
var docMap = Q.Promise(function(resolve){
113-
resolve(_.assign({
114-
index: {
115-
name: "index",
116-
type: "page",
117-
body: "Hello `{{thing.params.0.script}}`"
118-
},
119-
thing: {
120-
name: "thing",
121-
params: [
122-
{script: "<script>function() {return true; }</script>"}
123-
]
124-
}
125-
}));
126-
});
127101

128-
html.generate(docMap,options).then(function(){
129-
fs.readFile(
130-
path.join(__dirname,"test","tmp","index.html"),
131-
function(err, data){
132-
if(err) {
133-
done(err);
102+
return Q.denodeify(rmdir)(path.join(__dirname,"test","tmp"))
103+
.then(function() {
104+
var options = {
105+
dest: path.join(__dirname, "test","tmp"),
106+
parent: "index",
107+
templateRender: true
108+
};
109+
110+
var docMap = Q.Promise(function(resolve){
111+
resolve(_.assign({
112+
index: {
113+
name: "index",
114+
type: "page",
115+
body: [
116+
"Hello `{{thing.params.0.script}}`",
117+
"Load steal using \n\n `{{thing.params.1.script}}`"
118+
].join("\n")
119+
},
120+
thing: {
121+
name: "thing",
122+
params: [
123+
{script: "<script>function() {return true; }</script>"},
124+
{script: "<script src=\"./dist/steal/steal.js\"></script>"}
125+
]
134126
}
135-
assert.ok( (""+data).includes("<code>&amp;lt;script&amp;gt;function() {return true; }&amp;lt;\/script&amp;gt;<\/code>"), "script closing tag escaped" );
136-
done();
137-
});
138-
139-
},done);
140-
});
127+
}));
128+
});
129+
130+
return html.generate(docMap, options);
131+
})
132+
.then(function() {
133+
return readFile(path.join(__dirname, "test", "tmp", "index.html"));
134+
})
135+
.then(function(data) {
136+
var index = data.toString();
137+
138+
assert.ok(
139+
index.includes("<code>&amp;lt;script&amp;gt;function() {return true; }&amp;lt;\/script&amp;gt;<\/code>"),
140+
"script closing tag escaped"
141+
);
142+
})
143+
.then(function() {
144+
return readFile(path.join(__dirname, "test", "tmp", "thing.html"));
145+
})
146+
.then(function(data) {
147+
var content = data.toString();
148+
var rx = /<\/script>/g;
149+
150+
var docObject = content.substring(
151+
content.indexOf("var docObject = "),
152+
content.indexOf("};", content.indexOf("var docObject = "))
153+
);
154+
155+
assert.ok(
156+
!rx.test(docObject),
157+
"docObject should not have unscaped closing script tags"
158+
);
159+
});
141160
});
142161

143162
it("slashes get put in a folder and can link correctly", function(done){

0 commit comments

Comments
 (0)