Skip to content

Commit

Permalink
Support Content-Security-Policy syntax highlighting (#3511)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattes authored and nightwing committed Jan 31, 2018
1 parent 5d3596a commit e22e0f8
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 0 deletions.
94 changes: 94 additions & 0 deletions lib/ace/mode/_test/tokens_csp.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
[[
"start",
["constant.language","child-src"]
],[
"start",
["constant.language","connect-src"]
],[
"start",
["constant.language","default-src"]
],[
"start",
["constant.language","font-src"]
],[
"start",
["constant.language","frame-src"]
],[
"start",
["constant.language","img-src"]
],[
"start",
["constant.language","manifest-src"]
],[
"start",
["constant.language","media-src"]
],[
"start",
["constant.language","object-src"]
],[
"start",
["constant.language","script-src"]
],[
"start",
["constant.language","style-src"]
],[
"start",
["constant.language","worker-src"]
],[
"start",
["constant.language","base-uri"]
],[
"start",
["constant.language","plugin-types"]
],[
"start",
["constant.language","sandbox"]
],[
"start",
["constant.language","disown-opener"]
],[
"start",
["constant.language","form-action"]
],[
"start",
["constant.language","frame-ancestors"]
],[
"start",
["constant.language","report-uri"]
],[
"start",
["constant.language","report-to"]
],[
"start",
["constant.language","upgrade-insecure-requests"]
],[
"start",
["constant.language","block-all-mixed-content"]
],[
"start",
["constant.language","require-sri-for"]
],[
"start",
["constant.language","reflected-xss"]
],[
"start",
["constant.language","referrer"]
],[
"start",
["constant.language","policy-uri"]
],[
"start",
["variable","'self'"]
],[
"start",
["variable","'unsafe-inline'"]
],[
"start",
["variable","'unsafe-eval'"]
],[
"start",
["variable","'strict-dynamic'"]
],[
"start",
["variable","'unsafe-hashed-attributes'"]
]]
53 changes: 53 additions & 0 deletions lib/ace/mode/csp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2012, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */

/*
THIS FILE WAS AUTOGENERATED BY mode.tmpl.js
*/

define(function(require, exports, module) {
"use strict";

var TextMode = require("./text").Mode;
var CspHighlightRules = require("./csp_highlight_rules").CspHighlightRules;
var oop = require("../lib/oop");

var Mode = function() {
this.HighlightRules = CspHighlightRules;
};

oop.inherits(Mode, TextMode);

(function() {
this.$id = "ace/mode/csp";
}).call(Mode.prototype);

exports.Mode = Mode;
});
70 changes: 70 additions & 0 deletions lib/ace/mode/csp_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* ***** BEGIN LICENSE BLOCK *****
* Distributed under the BSD license:
*
* Copyright (c) 2012, Ajax.org B.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * Neither the name of Ajax.org B.V. nor the
* names of its contributors may be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL AJAX.ORG B.V. BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ***** END LICENSE BLOCK ***** */

/*
EXPLANATION
This highlight rules were created to help developer spot typos when working
with Content-Security-Policy (CSP). See:
https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy/
*/

define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;

var CspHighlightRules = function() {
var keywordMapper = this.createKeywordMapper({
"constant.language": "child-src|connect-src|default-src|font-src|frame-src|img-src|manifest-src|media-src|object-src"
+ "|script-src|style-src|worker-src|base-uri|plugin-types|sandbox|disown-opener|form-action|frame-ancestors|report-uri"
+ "|report-to|upgrade-insecure-requests|block-all-mixed-content|require-sri-for|reflected-xss|referrer|policy-uri",
"variable": "'none'|'self'|'unsafe-inline'|'unsafe-eval'|'strict-dynamic'|'unsafe-hashed-attributes'"
}, "identifier", true);

this.$rules = {
start: [{
token: "string.link",
regex: /https?:[^;\s]*/
}, {
token: "operator.punctuation",
regex: /;/
}, {
token: keywordMapper,
regex: /[^\s;]+/
}]
};
};

oop.inherits(CspHighlightRules, TextHighlightRules);

exports.CspHighlightRules = CspHighlightRules;
});
7 changes: 7 additions & 0 deletions lib/ace/snippets/csp.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
define(function(require, exports, module) {
"use strict";

exports.snippetText = require("../requirejs/text!./.snippets");
exports.scope = "";

});

0 comments on commit e22e0f8

Please sign in to comment.