diff --git a/.travis.yml b/.travis.yml index 11a0afa18..80bb5bf49 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,7 +3,6 @@ before_script: - git submodule update --init - npm install -g codeclimate-test-reporter node_js: - - 8 - 10 - 12 - 14 diff --git a/README.md b/README.md index 10487a386..5e502db93 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ ajv.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json')); - [Getting started](#getting-started) - [Frequently Asked Questions](https://github.com/ajv-validator/ajv/blob/master/FAQ.md) - [Using in browser](#using-in-browser) + - [Ajv and Content Security Policies (CSP)](#ajv-and-content-security-policies-csp) - [Command line interface](#command-line-interface) - Validation - [Keywords](#validation-keywords) @@ -238,6 +239,16 @@ Ajv is tested with these browsers: __Please note__: some frameworks, e.g. Dojo, may redefine global require in such way that is not compatible with CommonJS module format. In such case Ajv bundle has to be loaded before the framework and then you can use global Ajv (see issue [#234](https://github.com/ajv-validator/ajv/issues/234)). +### Ajv and Content Security Policies (CSP) + +If you're using Ajv to compile a schema (the typical use) in a browser document that is loaded with a Content Security Policy (CSP), that policy will require a `script-src` directive that includes the value `'unsafe-eval'`. +:warning: NOTE, however, that `unsafe-eval` is NOT recommended in a secure CSP[[1]](https://developer.chrome.com/extensions/contentSecurityPolicy#relaxing-eval), as it has the potential to open the document to cross-site scripting (XSS) attacks. + +In order to make use of Ajv without easing your CSP, you can [pre-compile a schema using the CLI](https://github.com/ajv-validator/ajv-cli#compile-schemas). This will transpile the schema JSON into a JavaScript file that exports a `validate` function that works simlarly to a schema compiled at runtime. + +Note that pre-compilation of schemas is performed using [ajv-pack](https://github.com/ajv-validator/ajv-pack) and there are [some limitations to the schema features it can compile](https://github.com/ajv-validator/ajv-pack#limitations). A successfully pre-compiled schema is equivalent to the same schema compiled at runtime. + + ## Command line interface CLI is available as a separate npm package [ajv-cli](https://github.com/ajv-validator/ajv-cli). It supports: @@ -321,7 +332,7 @@ You can add additional formats and replace any of the formats above using [addFo The option `unknownFormats` allows changing the default behaviour when an unknown format is encountered. In this case Ajv can either fail schema compilation (default) or ignore it (default in versions before 5.0.0). You also can whitelist specific format(s) to be ignored. See [Options](#options) for details. -You can find regular expressions used for format validation and the sources that were used in [formats.js](https://github.com/ajv-validatorv/ajv/blob/master/lib/compile/formats.js). +You can find regular expressions used for format validation and the sources that were used in [formats.js](https://github.com/ajv-validator/ajv/blob/master/lib/compile/formats.js). ## Combining schemas with $ref @@ -722,6 +733,10 @@ isSchemaSecure(schema2); // true __Please note__: following all these recommendation is not a guarantee that validation of untrusted data is safe - it can still lead to some undesirable results. +##### Content Security Policies (CSP) +See [Ajv and Content Security Policies (CSP)](#ajv-and-content-security-policies-csp) + + ## ReDoS attack Certain regular expressions can lead to the exponential evaluation time even with relatively short strings. diff --git a/lib/compile/util.js b/lib/compile/util.js index 702f6e19d..ef07b8c75 100644 --- a/lib/compile/util.js +++ b/lib/compile/util.js @@ -13,8 +13,6 @@ module.exports = { ucs2length: require('./ucs2length'), varOccurences: varOccurences, varReplace: varReplace, - cleanUpCode: cleanUpCode, - finalCleanUpCode: finalCleanUpCode, schemaHasRules: schemaHasRules, schemaHasRulesExcept: schemaHasRulesExcept, schemaUnknownRules: schemaUnknownRules, @@ -139,42 +137,6 @@ function varReplace(str, dataVar, expr) { } -var EMPTY_ELSE = /else\s*{\s*}/g - , EMPTY_IF_NO_ELSE = /if\s*\([^)]+\)\s*\{\s*\}(?!\s*else)/g - , EMPTY_IF_WITH_ELSE = /if\s*\(([^)]+)\)\s*\{\s*\}\s*else(?!\s*if)/g; -function cleanUpCode(out) { - return out.replace(EMPTY_ELSE, '') - .replace(EMPTY_IF_NO_ELSE, '') - .replace(EMPTY_IF_WITH_ELSE, 'if (!($1))'); -} - - -var ERRORS_REGEXP = /[^v.]errors/g - , REMOVE_ERRORS = /var errors = 0;|var vErrors = null;|validate.errors = vErrors;/g - , REMOVE_ERRORS_ASYNC = /var errors = 0;|var vErrors = null;/g - , RETURN_VALID = 'return errors === 0;' - , RETURN_TRUE = 'validate.errors = null; return true;' - , RETURN_ASYNC = /if \(errors === 0\) return data;\s*else throw new ValidationError\(vErrors\);/ - , RETURN_DATA_ASYNC = 'return data;' - , ROOTDATA_REGEXP = /[^A-Za-z_$]rootData[^A-Za-z0-9_$]/g - , REMOVE_ROOTDATA = /if \(rootData === undefined\) rootData = data;/; - -function finalCleanUpCode(out, async) { - var matches = out.match(ERRORS_REGEXP); - if (matches && matches.length == 2) { - out = async - ? out.replace(REMOVE_ERRORS_ASYNC, '') - .replace(RETURN_ASYNC, RETURN_DATA_ASYNC) - : out.replace(REMOVE_ERRORS, '') - .replace(RETURN_VALID, RETURN_TRUE); - } - - matches = out.match(ROOTDATA_REGEXP); - if (!matches || matches.length !== 3) return out; - return out.replace(REMOVE_ROOTDATA, ''); -} - - function schemaHasRules(schema, rules) { if (typeof schema == 'boolean') return !schema; for (var key in schema) if (rules[key]) return true; @@ -253,7 +215,7 @@ function getData($data, lvl, paths) { function joinPaths (a, b) { if (a == '""') return b; - return (a + ' + ' + b).replace(/' \+ '/g, ''); + return (a + ' + ' + b).replace(/([^\\])' \+ '/g, '$1'); } diff --git a/lib/dot/_limit.jst b/lib/dot/_limit.jst index e10806fd3..f15218922 100644 --- a/lib/dot/_limit.jst +++ b/lib/dot/_limit.jst @@ -17,6 +17,15 @@ , $op = $isMax ? '<' : '>' , $notOp = $isMax ? '>' : '<' , $errorKeyword = undefined; + + if (!($isData || typeof $schema == 'number' || $schema === undefined)) { + throw new Error($keyword + ' must be number'); + } + if (!($isDataExcl || $schemaExcl === undefined + || typeof $schemaExcl == 'number' + || typeof $schemaExcl == 'boolean')) { + throw new Error($exclusiveKeyword + ' must be number or boolean'); + } }} {{? $isDataExcl }} diff --git a/lib/dot/_limitItems.jst b/lib/dot/_limitItems.jst index a3e078e51..741329e77 100644 --- a/lib/dot/_limitItems.jst +++ b/lib/dot/_limitItems.jst @@ -3,6 +3,8 @@ {{# def.setupKeyword }} {{# def.$data }} +{{# def.numberKeyword }} + {{ var $op = $keyword == 'maxItems' ? '>' : '<'; }} if ({{# def.$dataNotType:'number' }} {{=$data}}.length {{=$op}} {{=$schemaValue}}) { {{ var $errorKeyword = $keyword; }} diff --git a/lib/dot/_limitLength.jst b/lib/dot/_limitLength.jst index cfc8dbb01..285c66bd2 100644 --- a/lib/dot/_limitLength.jst +++ b/lib/dot/_limitLength.jst @@ -3,6 +3,8 @@ {{# def.setupKeyword }} {{# def.$data }} +{{# def.numberKeyword }} + {{ var $op = $keyword == 'maxLength' ? '>' : '<'; }} if ({{# def.$dataNotType:'number' }} {{# def.strLength }} {{=$op}} {{=$schemaValue}}) { {{ var $errorKeyword = $keyword; }} diff --git a/lib/dot/_limitProperties.jst b/lib/dot/_limitProperties.jst index da7ea776f..c4c21551a 100644 --- a/lib/dot/_limitProperties.jst +++ b/lib/dot/_limitProperties.jst @@ -3,6 +3,8 @@ {{# def.setupKeyword }} {{# def.$data }} +{{# def.numberKeyword }} + {{ var $op = $keyword == 'maxProperties' ? '>' : '<'; }} if ({{# def.$dataNotType:'number' }} Object.keys({{=$data}}).length {{=$op}} {{=$schemaValue}}) { {{ var $errorKeyword = $keyword; }} diff --git a/lib/dot/allOf.jst b/lib/dot/allOf.jst index 4c2836311..0e782fe98 100644 --- a/lib/dot/allOf.jst +++ b/lib/dot/allOf.jst @@ -30,5 +30,3 @@ {{= $closingBraces.slice(0,-1) }} {{?}} {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/anyOf.jst b/lib/dot/anyOf.jst index 086cf2b33..ea909ee62 100644 --- a/lib/dot/anyOf.jst +++ b/lib/dot/anyOf.jst @@ -39,8 +39,6 @@ } else { {{# def.resetErrors }} {{? it.opts.allErrors }} } {{?}} - - {{# def.cleanUp }} {{??}} {{? $breakOnError }} if (true) { diff --git a/lib/dot/contains.jst b/lib/dot/contains.jst index 925d2c84b..4dc996741 100644 --- a/lib/dot/contains.jst +++ b/lib/dot/contains.jst @@ -53,5 +53,3 @@ var {{=$valid}}; {{# def.resetErrors }} {{?}} {{? it.opts.allErrors }} } {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/definitions.def b/lib/dot/definitions.def index b68e064e8..db4ea6f32 100644 --- a/lib/dot/definitions.def +++ b/lib/dot/definitions.def @@ -112,12 +112,6 @@ #}} -{{## def.cleanUp: {{ out = it.util.cleanUpCode(out); }} #}} - - -{{## def.finalCleanUp: {{ out = it.util.finalCleanUpCode(out, $async); }} #}} - - {{## def.$data: {{ var $isData = it.opts.$data && $schema && $schema.$data @@ -144,6 +138,13 @@ #}} +{{## def.numberKeyword: + {{? !($isData || typeof $schema == 'number') }} + {{ throw new Error($keyword + ' must be number'); }} + {{?}} +#}} + + {{## def.beginDefOut: {{ var $$outStack = $$outStack || []; diff --git a/lib/dot/dependencies.jst b/lib/dot/dependencies.jst index c41f33422..e4bdddec8 100644 --- a/lib/dot/dependencies.jst +++ b/lib/dot/dependencies.jst @@ -19,6 +19,7 @@ , $ownProperties = it.opts.ownProperties; for ($property in $schema) { + if ($property == '__proto__') continue; var $sch = $schema[$property]; var $deps = Array.isArray($sch) ? $propertyDeps : $schemaDeps; $deps[$property] = $sch; @@ -76,5 +77,3 @@ var missing{{=$lvl}}; {{= $closingBraces }} if ({{=$errs}} == errors) { {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/if.jst b/lib/dot/if.jst index 7ccc9b7f7..adb503612 100644 --- a/lib/dot/if.jst +++ b/lib/dot/if.jst @@ -65,8 +65,6 @@ {{# def.extraError:'if' }} } {{? $breakOnError }} else { {{?}} - - {{# def.cleanUp }} {{??}} {{? $breakOnError }} if (true) { diff --git a/lib/dot/items.jst b/lib/dot/items.jst index 8c0f5acb5..acc932a26 100644 --- a/lib/dot/items.jst +++ b/lib/dot/items.jst @@ -96,5 +96,3 @@ var {{=$valid}}; {{= $closingBraces }} if ({{=$errs}} == errors) { {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/multipleOf.jst b/lib/dot/multipleOf.jst index 5f8dd33b5..6d88a456f 100644 --- a/lib/dot/multipleOf.jst +++ b/lib/dot/multipleOf.jst @@ -3,6 +3,8 @@ {{# def.setupKeyword }} {{# def.$data }} +{{# def.numberKeyword }} + var division{{=$lvl}}; if ({{?$isData}} {{=$schemaValue}} !== undefined && ( diff --git a/lib/dot/properties.jst b/lib/dot/properties.jst index 862067e75..5cebb9b12 100644 --- a/lib/dot/properties.jst +++ b/lib/dot/properties.jst @@ -28,9 +28,9 @@ , $nextData = 'data' + $dataNxt , $dataProperties = 'dataProperties' + $lvl; - var $schemaKeys = Object.keys($schema || {}) + var $schemaKeys = Object.keys($schema || {}).filter(notProto) , $pProperties = it.schema.patternProperties || {} - , $pPropertyKeys = Object.keys($pProperties) + , $pPropertyKeys = Object.keys($pProperties).filter(notProto) , $aProperties = it.schema.additionalProperties , $someProperties = $schemaKeys.length || $pPropertyKeys.length , $noAdditional = $aProperties === false @@ -42,8 +42,11 @@ , $currentBaseId = it.baseId; var $required = it.schema.required; - if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) + if ($required && !(it.opts.$data && $required.$data) && $required.length < it.opts.loopRequired) { var $requiredHash = it.util.toHash($required); + } + + function notProto(p) { return p !== '__proto__'; } }} @@ -240,5 +243,3 @@ var {{=$nextValid}} = true; {{= $closingBraces }} if ({{=$errs}} == errors) { {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/propertyNames.jst b/lib/dot/propertyNames.jst index ee52b2151..d456ccafc 100644 --- a/lib/dot/propertyNames.jst +++ b/lib/dot/propertyNames.jst @@ -50,5 +50,3 @@ var {{=$errs}} = errors; {{= $closingBraces }} if ({{=$errs}} == errors) { {{?}} - -{{# def.cleanUp }} diff --git a/lib/dot/validate.jst b/lib/dot/validate.jst index bae653ff6..fd833a535 100644 --- a/lib/dot/validate.jst +++ b/lib/dot/validate.jst @@ -254,12 +254,6 @@ var {{=$valid}} = errors === errs_{{=$lvl}}; {{?}} -{{# def.cleanUp }} - -{{? $top }} - {{# def.finalCleanUp }} -{{?}} - {{ function $shouldUseGroup($rulesGroup) { var rules = $rulesGroup.rules; diff --git a/spec/ajv.spec.js b/spec/ajv.spec.js index e3bf766b7..118a827ad 100644 --- a/spec/ajv.spec.js +++ b/spec/ajv.spec.js @@ -512,5 +512,56 @@ describe('Ajv', function () { }); }); }); + + describe('sub-schema validation outside of definitions during compilation', function() { + it('maximum', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {maximum: 'bar'} + }); + }); + + it('exclusiveMaximum', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {exclusiveMaximum: 'bar'} + }); + }); + + it('maxItems', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {maxItems: 'bar'} + }); + }); + + it('maxLength', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {maxLength: 'bar'} + }); + }); + + it('maxProperties', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {maxProperties: 'bar'} + }); + }); + + it('multipleOf', function() { + passValidationThrowCompile({ + $ref: '#/foo', + foo: {maxProperties: 'bar'} + }); + }); + + function passValidationThrowCompile(schema) { + ajv.validateSchema(schema) .should.equal(true); + should.throw(function() { + ajv.compile(schema); + }); + } + }); }); }); diff --git a/spec/issues/388_code_clean-up.spec.js b/spec/issues/388_code_clean-up.spec.js deleted file mode 100644 index 9a0288362..000000000 --- a/spec/issues/388_code_clean-up.spec.js +++ /dev/null @@ -1,28 +0,0 @@ -'use strict'; - -var Ajv = require('../ajv'); -var should = require('../chai').should(); - - -describe('issue #388, code clean-up not working', function() { - it('should remove assignement to rootData if it is not used', function() { - var ajv = new Ajv; - var validate = ajv.compile({ - type: 'object', - properties: { - foo: { type: 'string' } - } - }); - var code = validate.toString(); - code.match(/rootData/g).length .should.equal(1); - }); - - it('should remove assignement to errors if they are not used', function() { - var ajv = new Ajv; - var validate = ajv.compile({ - type: 'object' - }); - var code = validate.toString(); - should.equal(code.match(/[^.]errors|vErrors/g), null); - }); -});