forked from google/csp-evaluator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.ts
100 lines (86 loc) · 2.94 KB
/
parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* @license
* Copyright 2016 Google Inc. All rights reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @author [email protected] (Lukas Weichselbaum)
*/
import * as csp from './csp';
/**
* A class to hold a parser for CSP in string format.
* @unrestricted
*/
export class CspParser {
csp: csp.Csp;
/**
* @param unparsedCsp A Content Security Policy as string.
*/
constructor(unparsedCsp: string) {
/**
* Parsed CSP
*/
this.csp = new csp.Csp();
this.parse(unparsedCsp);
}
/**
* Parses a CSP from a string.
* @param unparsedCsp CSP as string.
*/
parse(unparsedCsp: string): csp.Csp {
// Reset the internal state:
this.csp = new csp.Csp();
// Split CSP into directive tokens.
const directiveTokens = unparsedCsp.split(';');
for (let i = 0; i < directiveTokens.length; i++) {
const directiveToken = directiveTokens[i].trim();
// Split directive tokens into directive name and directive values.
const directiveParts = directiveToken.match(/\S+/g);
if (Array.isArray(directiveParts)) {
const directiveName = directiveParts[0].toLowerCase();
// If the set of directives already contains a directive whose name is a
// case insensitive match for directive name, ignore this instance of
// the directive and continue to the next token.
if (directiveName in this.csp.directives) {
continue;
}
if (!csp.isDirective(directiveName)) {
}
const directiveValues: string[] = [];
for (let directiveValue, j = 1; (directiveValue = directiveParts[j]);
j++) {
directiveValue = normalizeDirectiveValue(directiveValue);
if (!directiveValues.includes(directiveValue)) {
directiveValues.push(directiveValue);
}
}
this.csp.directives[directiveName] = directiveValues;
}
}
return this.csp;
}
}
/**
* Remove whitespaces and turn to lower case if CSP keyword or protocol
* handler.
* @param directiveValue directive value.
* @return normalized directive value.
*/
function normalizeDirectiveValue(directiveValue: string): string {
directiveValue = directiveValue.trim();
const directiveValueLower = directiveValue.toLowerCase();
if (csp.isKeyword(directiveValueLower) || csp.isUrlScheme(directiveValue)) {
return directiveValueLower;
}
return directiveValue;
}
export const TEST_ONLY = {normalizeDirectiveValue};