Skip to content

Commit

Permalink
Edit: Sanitization of Markdown content
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanlelek committed Feb 21, 2024
1 parent ed4f097 commit 863aaf5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
28 changes: 28 additions & 0 deletions app/functions/sanitize_markdown.js
@@ -0,0 +1,28 @@

// Modules
// var validator = require('validator');

// Sanitize Content
// This will disallow <script> and <style> embeds
// because output will be HTML-encoded.
// If you need images, links, etc. use the Markdown format (see docs)
//
// This was the prior content sanitizer, which was too aggressive
// Markdown characters got encoded/escaped to the point of being unusable
// return validator.escape(str);
//
// Instead we now will remove problematic characters not in the Markdown spec
// https://www.markdownguide.org/cheat-sheet/
// Includes: >, &
// More may be added in the future
// Additionally, Content Security Policy when implemented will help

// TODO: Add Test
function sanitize_markdown(str) {
return str
.replace(/</g, '&lt;')
.replace(/(\s)&(\s)/g, ' &amp; ');
}

// Exports
module.exports = exports = sanitize_markdown;
9 changes: 2 additions & 7 deletions app/routes/page.edit.route.js
@@ -1,8 +1,8 @@
// Modules
var fs = require('fs-extra');
var validator = require('validator');
var get_filepath = require('../functions/get_filepath.js');
var create_meta_info = require('../functions/create_meta_info.js');
var sanitize_markdown = require('../functions/sanitize_markdown.js');

function route_page_edit(config) {
return async function (req, res) {
Expand Down Expand Up @@ -43,12 +43,7 @@ function route_page_edit(config) {
}

var complete_content = create_content(req.body);

// Sanitize Content
// This will disallow <script> and <style> embeds
// because output will be HTML-encoded.
// If you need images, links, etc. use the Markdown format (see docs)
var sanitized_content = validator.escape(complete_content);
var sanitized_content = sanitize_markdown(complete_content);

try {
await fs.writeFile(filepath, sanitized_content);
Expand Down

0 comments on commit 863aaf5

Please sign in to comment.