|
| 1 | +const appDir = require('app-root-dir').get() |
| 2 | +const fs = require('fs') |
| 3 | +const path = require('path') |
| 4 | +const Prism = require('prismjs') |
| 5 | +const prismPath = require.resolve('prismjs') |
| 6 | +const prismStyleSheet = fs.readFileSync(path.join(prismPath.split('prism.js')[0], 'themes/prism.css')) |
| 7 | +const tamper = require('tamper') |
| 8 | +const { HtmlValidate } = require('html-validate') |
| 9 | +const validatorErrorPage = fs.readFileSync(path.join(__dirname, 'templates/errorPage.html')) |
| 10 | + |
| 11 | +function templateLiteralRenderer (templateString, dataModel) { |
| 12 | + const templateFunction = new Function(...Object.keys(dataModel), `return \`${templateString}\`;`) // eslint-disable-line |
| 13 | + return templateFunction(...Object.values(dataModel)) |
| 14 | +} |
| 15 | + |
| 16 | +const { minimatch } = require('minimatch') |
| 17 | +function wildcardMatch (str, matchList) { |
| 18 | + for (let rule of matchList) { |
| 19 | + rule = path.normalize(rule).replace(/\\/g, '/') // normalize windows; including normalizing the slashes |
| 20 | + if (minimatch(str, rule)) return true |
| 21 | + } |
| 22 | + return false |
| 23 | +} |
| 24 | + |
| 25 | +module.exports = (app, params) => { |
| 26 | + if (Object.prototype.hasOwnProperty.call(app, 'listen') || typeof app.listen === 'function') params = params || {} // two arguments |
| 27 | + else { |
| 28 | + params = app // one argument |
| 29 | + app = null |
| 30 | + } |
| 31 | + let render |
| 32 | + if (app) render = app.response.render |
| 33 | + let resModel |
| 34 | + const routeException = params?.exceptions?.routes || [] |
| 35 | + let headerException = params?.exceptions?.header ? params.exceptions.header : 'Partial' |
| 36 | + headerException = headerException.toLowerCase() |
| 37 | + const modelException = params?.exceptions?.modelValue ? params.exceptions.modelValue : '_disableValidator' |
| 38 | + let rules = typeof params?.validatorConfig === 'object' ? params.validatorConfig : {} |
| 39 | + const defaultRules = { extends: ['html-validate:standard'] } // default html-validate rules to use when none are passed |
| 40 | + if (Object.keys(rules).length === 0) { // when no config is passed check for a config file |
| 41 | + const ruleFile = path.join(appDir, '.htmlValidate.json') |
| 42 | + if (fs.existsSync(ruleFile)) rules = require(ruleFile) |
| 43 | + else rules = defaultRules |
| 44 | + } |
| 45 | + const htmlValidate = new HtmlValidate(rules) |
| 46 | + |
| 47 | + function reqExemptFromValidation (req, res) { |
| 48 | + // check for route exemptions |
| 49 | + if (wildcardMatch(req.route.path, routeException)) return true |
| 50 | + |
| 51 | + // check for model exemptions |
| 52 | + if (resModel) { |
| 53 | + if (resModel[modelException]) { |
| 54 | + resModel = undefined |
| 55 | + return true |
| 56 | + } else resModel = undefined // clear out the cached model in both scenarios |
| 57 | + } |
| 58 | + |
| 59 | + // check for head exemptions |
| 60 | + if (headerException) { |
| 61 | + if (req.headers[headerException]) return true // check the request header |
| 62 | + if (res.getHeader(headerException)) return true // check the response header |
| 63 | + } |
| 64 | + |
| 65 | + return false |
| 66 | + } |
| 67 | + |
| 68 | + async function validate (body, res) { |
| 69 | + const report = await htmlValidate.validateString(body) // run the validator against the response body |
| 70 | + if (!report.valid) { |
| 71 | + // the html failed validation |
| 72 | + const errorMap = new Map() |
| 73 | + let parsedErrors = '' |
| 74 | + for (const error of report.results[0].messages) { |
| 75 | + const message = error.message.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"').replace(/'/g, ''') // escape html entities |
| 76 | + parsedErrors += `${message}\n` // first line is error message |
| 77 | + parsedErrors += `At line ${error.line}, column ${error.column}\n\n` // next line is line and column numbers |
| 78 | + errorMap.set(error.line, error.message) // add error message and line number to map |
| 79 | + } |
| 80 | + const errorList = `<h2>Errors:</h2>\n<code class="validatorErrors">${parsedErrors}</code>` |
| 81 | + |
| 82 | + // start building out stylized markup block |
| 83 | + let formattedHTML = '<pre class=\'markup\'>\n<code class="language-html">\n' |
| 84 | + const markupArray = body.split('\n') |
| 85 | + |
| 86 | + // add line number highlighting for detected errors |
| 87 | + for (let i = 0; i < markupArray.length; i++) { |
| 88 | + const markupLine = markupArray[i] |
| 89 | + if (errorMap.has(i + 1)) { |
| 90 | + formattedHTML += `<span title='${errorMap.get(i + 1)}' class='line-numbers error'>` |
| 91 | + formattedHTML += Prism.highlight(`${markupLine}`, Prism.languages.markup) |
| 92 | + formattedHTML += '</span>' |
| 93 | + } else { |
| 94 | + formattedHTML += '<span class=\'line-numbers\'>' |
| 95 | + formattedHTML += Prism.highlight(`${markupLine}`, Prism.languages.markup) |
| 96 | + formattedHTML += '</span>' |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // cap off the stylized markup blocks |
| 101 | + formattedHTML += '</code>\n</pre>' |
| 102 | + formattedHTML = `<h2>Markup used:</h2>\n${formattedHTML}` |
| 103 | + |
| 104 | + // use 500 status for the validation error |
| 105 | + if (res) res.status(500) |
| 106 | + |
| 107 | + // build a model that includes error data, markup, and styling |
| 108 | + const model = { |
| 109 | + prismStyle: prismStyleSheet.toString(), |
| 110 | + preWidth: markupArray.length.toString().length * 8, |
| 111 | + errors: errorList, |
| 112 | + markup: formattedHTML, |
| 113 | + rawMarkup: body |
| 114 | + } |
| 115 | + |
| 116 | + // parse error page template and replace response body with it |
| 117 | + body = templateLiteralRenderer(validatorErrorPage, model) |
| 118 | + } |
| 119 | + |
| 120 | + return body |
| 121 | + } |
| 122 | + |
| 123 | + if (app) { |
| 124 | + // use some method overload trickery to store a usable model reference |
| 125 | + app.response.render = function (view, model, callback) { |
| 126 | + if (model && typeof model === 'object') resModel = model // store a reference to the model if exceptions are being used and a model was set |
| 127 | + render.apply(this, arguments) |
| 128 | + } |
| 129 | + |
| 130 | + // validate responses under the right conditions |
| 131 | + app.use(tamper((req, res) => { |
| 132 | + if (res.statusCode === 200 && res.getHeader?.('Content-Type')?.includes('text/html') && !reqExemptFromValidation(req, res)) return async (body) => await validate(body, res) |
| 133 | + })) |
| 134 | + } |
| 135 | + |
| 136 | + return validate // export validate function for general use |
| 137 | +} |
0 commit comments