/
SecurityRules.scala
478 lines (454 loc) · 16.1 KB
/
SecurityRules.scala
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
/*
* Copyright 2007-2015 WorldWide Conferencing, LLC
*
* 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.
*/
package net.liftweb
package http
import java.net.URI
import scala.concurrent.duration._
import common._
import json._
import util.Props
import util.Helpers.tryo
import LiftRules._
/**
* Rules for HTTPS usage by a Lift application.
*
* Currently corresponds directly to the [[http://tools.ietf.org/html/rfc6797
* HTTP `Strict-Transport-Security` header]].
*/
final case class HttpsRules(
/**
* When set, the duration of the requirement that HTTPS be used for this
* site. It's usually a good idea for this to be a high number. If unset,
* HTTPS is not required when interacting with this site.
*/
requiredTime: Option[Duration] = None,
/**
* When set to true, the required time above includes subdomains.
*/
includeSubDomains: Boolean = false
) {
lazy val headers: List[(String, String)] = {
requiredTime.toList.map { duration =>
val age = s"max-age=${duration.toSeconds}"
val header =
if (includeSubDomains) {
s"$age ; includeSubDomains"
} else {
age
}
("Strict-Transport-Security" -> header)
}
}
/**
* Returns the headers implied by this set of HTTPS rules. If `enforce` is
* false, returns nothing.
*/
def headers(enforce: Boolean): List[(String, String)] = {
if (enforce) {
headers
} else {
Nil
}
}
}
object HttpsRules {
/**
* Creates a restrictive set of HTTPS rules requiring HTTPS for 365 days and
* including subdomains in the requirement.
*/
def secure = apply(Some(365.days), true)
}
/**
* Base trait for content source restrictions. These are different ways of
* restricting where contents of various types are allowed to come from, in
* conjunction with `ContentSecurityPolicy` categories.
*/
sealed trait ContentSourceRestriction {
/**
* The `Content-Security-Policy` string that represents this restriction.
*/
def sourceRestrictionString: String
}
/**
* Marker trait for restrictions that only apply to JavaScript.
*/
sealed trait JavaScriptSourceRestriction extends ContentSourceRestriction
/**
* Marker trait for restrictions that only apply to stylesheets.
*/
sealed trait StylesheetSourceRestriction extends ContentSourceRestriction
sealed trait GeneralSourceRestriction extends JavaScriptSourceRestriction with StylesheetSourceRestriction
object ContentSourceRestriction {
/**
* Indicates content from all sources is allowed.
*/
case object All extends GeneralSourceRestriction {
val sourceRestrictionString = "*"
}
/**
* Indicates content from the given host path is allowed. See the
* `Content-Security-Policy` spec's [[https://www.w3.org/TR/CSP/#source-list-path-patching
* matching rules for `host-source`]] for more about what this can look
* like.
*
* Example:
* {{{
* Host("https://base.*.example.com")
* }}}
*/
case class Host(hostAndPath: String) extends GeneralSourceRestriction {
val sourceRestrictionString = hostAndPath
}
/**
* Indicates content from the given scheme is allowed. The scheme should not
* include the trailing `:`.
*
* Example:
* {{{
* Scheme("data")
* }}}
*/
case class Scheme(scheme: String) extends GeneralSourceRestriction {
val sourceRestrictionString = scheme + ":"
}
/**
* Indicates content from no sources is allowed.
*/
case object None extends GeneralSourceRestriction {
val sourceRestrictionString = "'none'"
}
/**
* Indicates content from the same origin as the content is allowed.
*/
case object Self extends GeneralSourceRestriction {
val sourceRestrictionString = "'self'"
}
/**
* Indicates inline content on the page is allowed to be interpreted. It is
* highly recommended that this not be used, as it exposes your application to
* cross-site scripting and other vulnerabilities.
*
* If not specified for JavaScript, JavaScript `on*` event handler attributes,
* `<script>` elements, and `javascript:` URIs will not be executed by a
* browser that supports content security policies.
*
* If not specified for stylesheets, `<style>` elements and inline `style`
* attributes will not be read by a browser that supports content security
* policies.
*/
case object UnsafeInline extends JavaScriptSourceRestriction with StylesheetSourceRestriction {
val sourceRestrictionString = "'unsafe-inline'"
}
/**
* Indicates `eval` and related functionality can be used. Some of Lift's
* functionality, including `idMemoize` and comet handling, relies on eval,
* so not including this in your script sources will mean you won't be able to
* use those.
*
* If not specified for JavaScript, invoking `eval`, the `Function`
* constructor, or `setTimeout`/`setInterval` with a string parameter will
* all throw security exceptions in a browser that supports content security
* policies.
*/
case object UnsafeEval extends JavaScriptSourceRestriction {
val sourceRestrictionString = "'unsafe-eval'"
}
}
/**
* Specifies a `[[https://developer.mozilla.org/en-US/docs/Web/Security/CSP Content-Security-Policy]] `
* for this site. This will be sent to the client in a `Content-Security-Policy`
* header when responses are returned from Lift.
*
* In development mode, content security policy violations are only reported if
* the browser supports them, not enforced. In all other modes, content security
* policy violations are enforced if the browser supports them.
*
* Note that the `X-Webkit-CSP` header is NOT specified, due to
* potentially-broken behavior in iOS 5 and 5.1. This means iOS 6/6.1 will not
* receive a content security policy that it can
* understand. See the [[http://caniuse.com/#feat=contentsecuritypolicy caniuse
* page on content security policy]] for more.
*
* @param defaultSources A list of default source restrictions; if one of the
* other sources parameters is empty, the default sources will apply
* instead.
* @param connectSources A list of source restrictions for `XmlHttpRequest`
* (AJAX) connections.
* @param fontSources A list of source restrictions for loading fonts (e.g.,
* from CSS `font-face` declarations).
* @param frameSources A list of source restrictions for loading frames and
* iframes.
* @param imageSources A list of source restrictions for loading images.
* @param mediaSources A list of source restrictions for loading media (audio
* and video).
* @param objectSources A list of source restrictions for loading `object`,
* `embed`, `applet`, and related elements.
* @param scriptSources A list of source restrictions for loading scripts. Also
* accepts the `[[ContentSourceRestriction.UnsafeInline UnsafeInline]]`
* and `[[ContentSourceRestriction.UnsafeEval UnsafeEval]]` source
* restrictions, though these are strongly discouraged.
* @param styleSources A list of source restrictions for loading styles. Also
* accepts the `[[ContentSourceRestriction.UnsafeInline UnsafeInline]]`
* source, though it is strongly discouraged.
* @param reportUri The URI where any violation of the security policy will be
* reported. You can set the function that handles these violations in
* `[[LiftRules.contentSecurityPolicyViolationReport]]`. By default,
* reported to `[[ContentSecurityPolicy.defaultReportUri]]`.
*
* If this is `None`, violations will not be reported.
*/
final case class ContentSecurityPolicy(
defaultSources: List[ContentSourceRestriction] = List(ContentSourceRestriction.Self),
connectSources: List[ContentSourceRestriction] = Nil,
fontSources: List[ContentSourceRestriction] = Nil,
frameSources: List[ContentSourceRestriction] = Nil,
imageSources: List[ContentSourceRestriction] = List(ContentSourceRestriction.All),
mediaSources: List[ContentSourceRestriction] = Nil,
objectSources: List[ContentSourceRestriction] = Nil,
scriptSources: List[JavaScriptSourceRestriction] = List(
ContentSourceRestriction.UnsafeEval,
ContentSourceRestriction.Self
),
styleSources: List[StylesheetSourceRestriction] = Nil,
reportUri: Option[URI] = Some(ContentSecurityPolicy.defaultReportUri)
) {
/**
* The string that describes this content security policy in the syntax
* expected by the `Content-Security-Policy` header.
*/
def contentSecurityPolicyString = {
val allRestrictions =
List(
"default-src" -> defaultSources,
"connect-src" -> connectSources,
"font-src" -> fontSources,
"frame-src" -> frameSources,
"img-src" -> imageSources,
"media-src" -> mediaSources,
"object-src" -> objectSources,
"script-src" -> scriptSources,
"style-src" -> styleSources
)
val restrictionString =
allRestrictions
.collect {
case (category, restrictions) if restrictions.nonEmpty =>
category +
" " +
restrictions.map(_.sourceRestrictionString).mkString(" ")
}
.mkString("; ")
reportUri.map { uri =>
s"$restrictionString; report-uri $uri"
} getOrElse {
restrictionString
}
}
private[this] lazy val reportOnlyHeaders = {
List(
"Content-Security-Policy-Report-Only" -> contentSecurityPolicyString,
"X-Content-Security-Policy-Report-Only" -> contentSecurityPolicyString
)
}
private[this] lazy val enforcedHeaders = {
List(
"Content-Security-Policy" -> contentSecurityPolicyString,
"X-Content-Security-Policy" -> contentSecurityPolicyString
)
}
/**
* Returns the headers implied by this content security policy.
*/
def headers(enforce: Boolean = true, logViolations: Boolean = true): List[(String, String)] = {
if (enforce) {
enforcedHeaders
} else if (logViolations) {
reportOnlyHeaders
} else {
Nil
}
}
}
object ContentSecurityPolicy {
/**
* The default URI where security policy violations will be reported. This
* URI is under Lift's URI namespace, at `[[LiftRules.liftPath]]`.
*/
def defaultReportUri = {
new URI(LiftRules.liftPath + "/content-security-policy-report")
}
/**
* Creates a restrictive content security policy that disallows images from
* all sources except the page's origin.
*
* Note that the default content security policy restricts all other resources
* to the same origin, but allows images from any source; the secure one only
* differs because it adds restrictions to the image sources.
*/
def secure: ContentSecurityPolicy = {
ContentSecurityPolicy(imageSources = Nil)
}
}
/**
* The expected payload of a content security policy violation report.
*
* Parsable from the JSON POST that a browser should send when a violation
* occurs.
*/
case class ContentSecurityPolicyViolation(
documentUri: String,
referrer: String,
blockedUri: String,
violatedDirective: String,
originalPolicy: String
)
object ContentSecurityPolicyViolation extends LazyLoggable {
private[this] implicit val formats = DefaultFormats
def defaultViolationHandler: DispatchPF = {
case request @ Req(start :: "content-security-policy-report" :: Nil, _, _) if start == LiftRules.liftContextRelativePath =>
val violation =
for {
requestJson <- request.forcedBodyAsJson
camelCasedJson = requestJson.transformField {
case JField("document-uri", content) =>
JField("documentUri", content)
case JField("blocked-uri", content) =>
JField("blockedUri", content)
case JField("violated-directive", content) =>
JField("violatedDirective", content)
case JField("original-policy", content) =>
JField("originalPolicy", content)
}
violationJson = camelCasedJson \ "csp-report"
extractedViolation <- tryo(violationJson.extract[ContentSecurityPolicyViolation])
} yield {
extractedViolation
}
() => {
violation match {
case Full(violation) =>
LiftRules.contentSecurityPolicyViolationReport(violation) or
Full(OkResponse())
case _ =>
logger.warn(
s"Got a content security violation report we couldn't interpret: '${request.body.map(new String(_, "UTF-8"))}'."
)
Full(BadRequestResponse("Unrecognized format for content security policy report."))
}
}
}
}
/**
* Defines restrictions on allowing served pages to be embedded in frames.
*/
sealed trait FrameRestrictions {
def headers: List[(String,String)]
/**
* Returns the headers implied by these frame restrictions.
*
* Because of how frame restrictions are handled, if enforcement is turned
* off, no headers are generated.
*/
def headers(enforce: Boolean = false): List[(String,String)] = {
if (enforce) {
headers
} else {
Nil
}
}
}
object FrameRestrictions {
/**
* Allows other pages from the same origin as the one being served to embed
* this page in a frame.
*/
case object SameOrigin extends FrameRestrictions {
val headers = List("X-Frame-Options" -> "SAMEORIGIN")
}
/**
* Does not allow embedding the page being served in a frame at all.
*/
case object Deny extends FrameRestrictions {
val headers = List("X-Frame-Options" -> "DENY")
}
}
/**
* Specifies security rules for a Lift application. By default, HTTPS is not
* required and `Content-Security-Policy` is restricted to the current domain
* for everything except images, which are accepted from any domain.
* Additionally, served pages can only be embedded in other frames from
* the current domain.
*
* You can use `[[SecurityRules.secure]]` to enable more restrictive, but
* also more secure, defaults.
*
* @param enforceInDevMode If true, security policies and HTTPS rules are
* enforced in dev mode in addition to staging/pilot/production/etc.
* @param logInDevMode If true, dev mode violations of security policies are
* logged by default. Note that if you override
* `[[LiftRules.contentSecurityPolicyViolationReport]]` or otherwise
* change the default Lift policy violation handling behavior, it will
* be up to you to handle this property as desired.
*/
final case class SecurityRules(
https: Option[HttpsRules] = None,
content: Option[ContentSecurityPolicy] = Some(ContentSecurityPolicy()),
frameRestrictions: Option[FrameRestrictions] = Some(FrameRestrictions.SameOrigin),
enforceInOtherModes: Boolean = false,
logInOtherModes: Boolean = true,
enforceInDevMode: Boolean = false,
logInDevMode: Boolean = true
) {
private val enforce_? = {
if (Props.devMode) {
enforceInDevMode
} else {
enforceInOtherModes
}
}
private val logViolations_? = {
if (Props.devMode) {
logInDevMode
} else {
logInOtherModes
}
}
/**
* Returns the headers implied by this set of security rules.
*/
lazy val headers: List[(String, String)] = {
https.toList.flatMap(_.headers(enforce_?)) :::
content.toList.flatMap(_.headers(enforce_?, logViolations_?)) :::
frameRestrictions.toList.flatMap(_.headers(enforce_?))
}
}
object SecurityRules {
/**
* Creates a restrictive set of security rules, including required HTTPS,
* [[HttpsRules$.secure secure HTTPS rules]], and
* [[ContentSecurityPolicy$.secure secure `Content-Security-Policy` rules]].
*
* To tweak any of these settings, use the `SecurityRules` constructor
* directly.
*/
def secure = {
apply(
Some(HttpsRules.secure),
Some(ContentSecurityPolicy.secure),
enforceInOtherModes = true
)
}
}