Skip to content

Escape _all_ closing script tags #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions build/make_default_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
//this allows linking to a specific section with the hash syntax (#27)
hashParts = name.split("#");
name = hashParts.shift();

docObject = docMap[name]
if (docObject) {
linkText = parts && parts[2] ? parts[2] : docObject.title || name;
Expand Down Expand Up @@ -408,7 +408,9 @@ module.exports = function(docMap, config, getCurrent, Handlebars){
},
docObjectString: function(){
this.pathToRoot = pathToRoot(this.name);
return JSON.stringify(deepExtendWithoutBody(this)).replace("</script>", "<\\/script>");

return JSON.stringify(deepExtendWithoutBody(this))
.replace(/<\/script>/g, "<\\/script>");
},
pathToDest: function(){
var currentDir = path.dirname( path.join(config.dest, docsFilename( getCurrent(), config)) );
Expand Down
97 changes: 58 additions & 39 deletions html_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,48 +96,67 @@ describe("documentjs/lib/generators/html",function(){
});
});

it("closing script tags are properly escaped", function(done){
it("closing script tags are properly escaped", function() {
this.timeout(40000);
rmdir(path.join(__dirname,"test","tmp"), function(e){
if(e) {
return done(e);
}
var options = {
dest: path.join(__dirname, "test","tmp"),
parent: "index",
templateRender: true
};


var docMap = Q.Promise(function(resolve){
resolve(_.assign({
index: {
name: "index",
type: "page",
body: "Hello `{{thing.params.0.script}}`"
},
thing: {
name: "thing",
params: [
{script: "<script>function() {return true; }</script>"}
]
}
}));
});

html.generate(docMap,options).then(function(){
fs.readFile(
path.join(__dirname,"test","tmp","index.html"),
function(err, data){
if(err) {
done(err);
return Q.denodeify(rmdir)(path.join(__dirname,"test","tmp"))
.then(function() {
var options = {
dest: path.join(__dirname, "test","tmp"),
parent: "index",
templateRender: true
};

var docMap = Q.Promise(function(resolve){
resolve(_.assign({
index: {
name: "index",
type: "page",
body: [
"Hello `{{thing.params.0.script}}`",
"Load steal using \n\n `{{thing.params.1.script}}`"
].join("\n")
},
thing: {
name: "thing",
params: [
{script: "<script>function() {return true; }</script>"},
{script: "<script src=\"./dist/steal/steal.js\"></script>"}
]
}
assert.ok( (""+data).includes("<code>&amp;lt;script&amp;gt;function() {return true; }&amp;lt;\/script&amp;gt;<\/code>"), "script closing tag escaped" );
done();
});

},done);
});
}));
});

return html.generate(docMap, options);
})
.then(function() {
return readFile(path.join(__dirname, "test", "tmp", "index.html"));
})
.then(function(data) {
var index = data.toString();

assert.ok(
index.includes("<code>&amp;lt;script&amp;gt;function() {return true; }&amp;lt;\/script&amp;gt;<\/code>"),
"script closing tag escaped"
);
})
.then(function() {
return readFile(path.join(__dirname, "test", "tmp", "thing.html"));
})
.then(function(data) {
var content = data.toString();
var rx = /<\/script>/g;

var docObject = content.substring(
content.indexOf("var docObject = "),
content.indexOf("};", content.indexOf("var docObject = "))
);

assert.ok(
!rx.test(docObject),
"docObject should not have unscaped closing script tags"
);
});
});

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