| 1 |
# SPDX-License-Identifier: MPL-2.0 |
| 2 |
# pylint: disable=too-many-lines |
| 3 |
# -*- coding: utf-8 -*- |
| 4 |
|
| 5 |
import os |
| 6 |
|
| 7 |
import unittest |
| 8 |
|
| 9 |
import datetime |
| 10 |
|
| 11 |
from cryptodatahub.common.algorithm import Hash |
| 12 |
from cryptodatahub.common.exception import InvalidValue |
| 13 |
|
| 14 |
from cryptoparser.common.exception import InvalidType |
| 15 |
from cryptoparser.httpx.header import ( |
| 16 |
ContentSecurityPolicyDirectiveBaseUri, |
| 17 |
ContentSecurityPolicyDirectiveBlockAllMixedContent, |
| 18 |
ContentSecurityPolicyDirectiveChildSrc, |
| 19 |
ContentSecurityPolicyDirectiveConnectSrc, |
| 20 |
ContentSecurityPolicyDirectiveDefaultSrc, |
| 21 |
ContentSecurityPolicyDirectiveFontSrc, |
| 22 |
ContentSecurityPolicyDirectiveFormAction, |
| 23 |
ContentSecurityPolicyDirectiveFrameAncestors, |
| 24 |
ContentSecurityPolicyDirectiveFrameSrc, |
| 25 |
ContentSecurityPolicyDirectiveImgSrc, |
| 26 |
ContentSecurityPolicyDirectiveManifestSrc, |
| 27 |
ContentSecurityPolicyDirectiveMediaSrc, |
| 28 |
ContentSecurityPolicyDirectiveObjectSrc, |
| 29 |
ContentSecurityPolicyDirectivePluginTypes, |
| 30 |
ContentSecurityPolicyDirectivePrefetchSrc, |
| 31 |
ContentSecurityPolicyDirectiveReferrer, |
| 32 |
ContentSecurityPolicyDirectiveReportTo, |
| 33 |
ContentSecurityPolicyDirectiveReportUri, |
| 34 |
ContentSecurityPolicyDirectiveRequireTrustedTypesFor, |
| 35 |
ContentSecurityPolicyDirectiveSandbox, |
| 36 |
ContentSecurityPolicyDirectiveScriptSrc, |
| 37 |
ContentSecurityPolicyDirectiveScriptSrcAttr, |
| 38 |
ContentSecurityPolicyDirectiveScriptSrcElem, |
| 39 |
ContentSecurityPolicyDirectiveStyleSrc, |
| 40 |
ContentSecurityPolicyDirectiveStyleSrcAttr, |
| 41 |
ContentSecurityPolicyDirectiveStyleSrcElem, |
| 42 |
ContentSecurityPolicyDirectiveUpgradeInsecureRequests, |
| 43 |
ContentSecurityPolicyDirectiveWebrtc, |
| 44 |
ContentSecurityPolicyDirectiveWorkerSrc, |
| 45 |
ContentSecurityPolicyReferrerPolicy, |
| 46 |
ContentSecurityPolicySourceHash, |
| 47 |
ContentSecurityPolicySourceHost, |
| 48 |
ContentSecurityPolicySourceKeyword, |
| 49 |
ContentSecurityPolicySourceNonce, |
| 50 |
ContentSecurityPolicySourceScheme, |
| 51 |
ContentSecurityPolicyTrustedTypeSinkGroup, |
| 52 |
ContentSecurityPolicyWebRtcType, |
| 53 |
FieldValueMimeType, |
| 54 |
HttpHeaderFields, |
| 55 |
HttpHeaderFieldAge, |
| 56 |
HttpHeaderFieldCacheControlResponse, |
| 57 |
HttpHeaderFieldContentType, |
| 58 |
HttpHeaderFieldContentSecurityPolicy, |
| 59 |
HttpHeaderFieldContentSecurityPolicyReportOnly, |
| 60 |
HttpHeaderFieldDate, |
| 61 |
HttpHeaderFieldETag, |
| 62 |
HttpHeaderFieldExpectCT, |
| 63 |
HttpHeaderFieldExpectStaple, |
| 64 |
HttpHeaderFieldExpires, |
| 65 |
HttpHeaderFieldLastModified, |
| 66 |
HttpHeaderFieldName, |
| 67 |
HttpHeaderFieldNetworkErrorLogging, |
| 68 |
HttpHeaderFieldPragma, |
| 69 |
HttpHeaderFieldPublicKeyPinning, |
| 70 |
HttpHeaderFieldReferrerPolicy, |
| 71 |
HttpHeaderFieldServer, |
| 72 |
HttpHeaderFieldSetCookie, |
| 73 |
HttpHeaderFieldSTS, |
| 74 |
HttpHeaderFieldUnparsed, |
| 75 |
HttpHeaderFieldValueCacheControlResponse, |
| 76 |
HttpHeaderFieldValueContentSecurityPolicy, |
| 77 |
HttpHeaderFieldValueContentType, |
| 78 |
HttpHeaderFieldValueExpectCT, |
| 79 |
HttpHeaderFieldValueExpectStaple, |
| 80 |
HttpHeaderFieldValueNetworkErrorLogging, |
| 81 |
HttpHeaderFieldValuePragma, |
| 82 |
HttpHeaderFieldValuePublicKeyPinning, |
| 83 |
HttpHeaderFieldValueReferrerPolicy, |
| 84 |
HttpHeaderFieldValueSetCookie, |
| 85 |
HttpHeaderFieldValueSTS, |
| 86 |
HttpHeaderFieldValueXContentTypeOptions, |
| 87 |
HttpHeaderFieldValueXFrameOptions, |
| 88 |
HttpHeaderFieldValueXXSSProtection, |
| 89 |
HttpHeaderFieldXContentSecurityPolicy, |
| 90 |
HttpHeaderFieldXContentTypeOptions, |
| 91 |
HttpHeaderFieldXFrameOptions, |
| 92 |
HttpHeaderFieldXXSSProtection, |
| 93 |
HttpHeaderReferrerPolicy, |
| 94 |
HttpHeaderPragma, |
| 95 |
HttpHeaderSetCookieComponentSameSite, |
| 96 |
HttpHeaderXContentTypeOptions, |
| 97 |
HttpHeaderXFrameOptions, |
| 98 |
HttpHeaderXXSSProtectionMode, |
| 99 |
HttpHeaderXXSSProtectionState, |
| 100 |
MimeTypeRegistry, |
| 101 |
) |
| 102 |
|
| 103 |
from .classes import TestCasesBasesHttpHeader |
| 104 |
|
| 105 |
|
| 106 |
class TestHttpHeaderFieldValueCacheControlResponse( |
| 107 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 108 |
TestCasesBasesHttpHeader.FullHeader, |
| 109 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 110 |
|
| 111 |
_header_minimal = HttpHeaderFieldValueCacheControlResponse() |
| 112 |
_header_minimal_bytes = b'' |
| 113 |
_header_minimal_markdown = '' |
| 114 |
|
| 115 |
_header_full = HttpHeaderFieldValueCacheControlResponse( |
| 116 |
max_age=datetime.timedelta(seconds=1), |
| 117 |
s_maxage=datetime.timedelta(seconds=2), |
| 118 |
must_revalidate=True, |
| 119 |
no_cache=True, |
| 120 |
no_store=True, |
| 121 |
public=True, |
| 122 |
private=True, |
| 123 |
no_transform=True, |
| 124 |
) |
| 125 |
_header_full_bytes = b'max-age=1, s-maxage=2, must-revalidate, no-cache, no-store, public, private, no-transform' |
| 126 |
_header_full_upper_case_bytes = b', '.join([ |
| 127 |
b'MAX-AGE=1', |
| 128 |
b'S-MAXAGE=2', |
| 129 |
b'MUST-REVALIDATE', |
| 130 |
b'NO-CACHE', |
| 131 |
b'NO-STORE', |
| 132 |
b'PUBLIC', |
| 133 |
b'PRIVATE', |
| 134 |
b'NO-TRANSFORM', |
| 135 |
]) |
| 136 |
_header_full_lower_case_bytes = b', '.join([ |
| 137 |
b'max-age=1', |
| 138 |
b's-maxage=2', |
| 139 |
b'must-revalidate', |
| 140 |
b'no-cache', |
| 141 |
b'no-store', |
| 142 |
b'public', |
| 143 |
b'private', |
| 144 |
b'no-transform', |
| 145 |
]) |
| 146 |
_header_minimal_markdown = os.linesep.join([ |
| 147 |
'* Max Age: n/a', |
| 148 |
'* S Maxage: n/a', |
| 149 |
'* Must Revalidate: no', |
| 150 |
'* Proxy Revalidate: no', |
| 151 |
'* No Cache: no', |
| 152 |
'* No Store: no', |
| 153 |
'* Public: no', |
| 154 |
'* Private: no', |
| 155 |
'* No Transform: no', |
| 156 |
'', |
| 157 |
]) |
| 158 |
|
| 159 |
|
| 160 |
class TestContentSecurityPolicySourceHash(unittest.TestCase): |
| 161 |
def test_error_wrong_prefix(self): |
| 162 |
with self.assertRaises(InvalidType): |
| 163 |
ContentSecurityPolicySourceHash.parse_exact_size(b'notavalidhashalgorithm') |
| 164 |
|
| 165 |
def test_parse(self): |
| 166 |
self.assertEqual( |
| 167 |
ContentSecurityPolicySourceHash.parse_exact_size(b'sha256-bGlnaHQgd29yay4='), |
| 168 |
ContentSecurityPolicySourceHash(Hash.SHA2_256, bytearray(b'light work.')) |
| 169 |
) |
| 170 |
|
| 171 |
def test_compose(self): |
| 172 |
self.assertEqual( |
| 173 |
ContentSecurityPolicySourceHash(Hash.SHA2_256, bytearray(b'light work.')).compose(), |
| 174 |
b'sha256-bGlnaHQgd29yay4=' |
| 175 |
) |
| 176 |
|
| 177 |
|
| 178 |
class TestContentSecurityPolicySourceHost(unittest.TestCase): |
| 179 |
def test_parse(self): |
| 180 |
self.assertEqual( |
| 181 |
ContentSecurityPolicySourceHost.parse_exact_size(b'http://example.com'), |
| 182 |
ContentSecurityPolicySourceHost('http://example.com') |
| 183 |
) |
| 184 |
|
| 185 |
def test_compose(self): |
| 186 |
self.assertEqual( |
| 187 |
ContentSecurityPolicySourceHost('http://example.com').compose(), |
| 188 |
b'http://example.com' |
| 189 |
) |
| 190 |
|
| 191 |
|
| 192 |
class TestContentSecurityPolicySourceNonce(unittest.TestCase): |
| 193 |
def test_error_wrong_prefix(self): |
| 194 |
with self.assertRaises(InvalidType): |
| 195 |
ContentSecurityPolicySourceNonce.parse_exact_size(b'bGlnaHQgd29yay4=') |
| 196 |
|
| 197 |
def test_parse(self): |
| 198 |
self.assertEqual( |
| 199 |
ContentSecurityPolicySourceNonce.parse_exact_size(b'nonce-bGlnaHQgd29yay4='), |
| 200 |
ContentSecurityPolicySourceNonce(bytearray(b'light work.')) |
| 201 |
) |
| 202 |
|
| 203 |
def test_compose(self): |
| 204 |
self.assertEqual( |
| 205 |
ContentSecurityPolicySourceNonce(bytearray(b'light work.')).compose(), |
| 206 |
b'nonce-bGlnaHQgd29yay4=' |
| 207 |
) |
| 208 |
|
| 209 |
|
| 210 |
class TestContentSecurityPolicySourceScheme(unittest.TestCase): |
| 211 |
def test_parse(self): |
| 212 |
self.assertEqual( |
| 213 |
ContentSecurityPolicySourceScheme.parse_exact_size(b'http:'), |
| 214 |
ContentSecurityPolicySourceScheme('http') |
| 215 |
) |
| 216 |
|
| 217 |
def test_compose(self): |
| 218 |
self.assertEqual( |
| 219 |
ContentSecurityPolicySourceScheme('http').compose(), |
| 220 |
b'http:' |
| 221 |
) |
| 222 |
|
| 223 |
|
| 224 |
class TestContentSecurityPolicyDirectivesFetch( |
| 225 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 226 |
TestCasesBasesHttpHeader.FullHeader): |
| 227 |
|
| 228 |
_header_minimal = ContentSecurityPolicyDirectiveDefaultSrc([ContentSecurityPolicySourceKeyword.SELF]) |
| 229 |
_header_minimal_bytes = ' '.join([ |
| 230 |
'default-src', |
| 231 |
'\'self\'', |
| 232 |
]).encode('ascii') |
| 233 |
_header_minimal_markdown = os.linesep.join([ |
| 234 |
'* Type: default-src', |
| 235 |
'* Value:', |
| 236 |
' 1.', |
| 237 |
' * Type: KEYWORD', |
| 238 |
' * Value: \'self\'', |
| 239 |
'', |
| 240 |
]) |
| 241 |
_header_full = ContentSecurityPolicyDirectiveDefaultSrc([ |
| 242 |
ContentSecurityPolicySourceKeyword.SELF, |
| 243 |
ContentSecurityPolicySourceScheme('http'), |
| 244 |
ContentSecurityPolicySourceHost('https://example.com'), |
| 245 |
ContentSecurityPolicySourceNonce(bytearray(b'light work.')), |
| 246 |
ContentSecurityPolicySourceHash(Hash.SHA2_256, bytearray(b'light work.')), |
| 247 |
]) |
| 248 |
_header_full_bytes = ' '.join([ |
| 249 |
'default-src', |
| 250 |
'\'self\'', |
| 251 |
'http:', |
| 252 |
'https://example.com', |
| 253 |
'nonce-bGlnaHQgd29yay4=', |
| 254 |
'sha256-bGlnaHQgd29yay4=', |
| 255 |
]).encode('ascii') |
| 256 |
|
| 257 |
def test_min_source_length(self): |
| 258 |
with self.assertRaises(InvalidValue) as context_manager: |
| 259 |
ContentSecurityPolicyDirectiveDefaultSrc.parse_exact_size(b'default-src') |
| 260 |
self.assertEqual(context_manager.exception.value, b'') |
| 261 |
|
| 262 |
def test_error_invalid_value_type(self): |
| 263 |
with self.assertRaises(InvalidValue): |
| 264 |
ContentSecurityPolicyDirectiveDefaultSrc([None]) |
| 265 |
|
| 266 |
|
| 267 |
class TestContentSecurityPolicyDirectiveFrameAncestors( |
| 268 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 269 |
TestCasesBasesHttpHeader.FullHeader): |
| 270 |
|
| 271 |
_header_minimal = ContentSecurityPolicyDirectiveFrameAncestors([ContentSecurityPolicySourceKeyword.SELF]) |
| 272 |
_header_minimal_bytes = ' '.join([ |
| 273 |
'frame-ancestors', |
| 274 |
'\'self\'', |
| 275 |
]).encode('ascii') |
| 276 |
_header_minimal_markdown = os.linesep.join([ |
| 277 |
'* Type: frame-ancestors', |
| 278 |
'* Value:', |
| 279 |
' 1.', |
| 280 |
' * Type: KEYWORD', |
| 281 |
' * Value: \'self\'', |
| 282 |
'', |
| 283 |
]) |
| 284 |
_header_full = ContentSecurityPolicyDirectiveFrameAncestors([ |
| 285 |
ContentSecurityPolicySourceKeyword.SELF, |
| 286 |
ContentSecurityPolicySourceScheme('http'), |
| 287 |
ContentSecurityPolicySourceHost('https://example.com'), |
| 288 |
]) |
| 289 |
_header_full_bytes = ' '.join([ |
| 290 |
'frame-ancestors', |
| 291 |
'\'self\'', |
| 292 |
'http:', |
| 293 |
'https://example.com', |
| 294 |
]).encode('ascii') |
| 295 |
|
| 296 |
def test_error_invalid_value_type(self): |
| 297 |
with self.assertRaises(InvalidValue): |
| 298 |
ContentSecurityPolicyDirectiveFrameAncestors([ |
| 299 |
ContentSecurityPolicySourceKeyword(ContentSecurityPolicySourceKeyword.REPORT_SAMPLE) |
| 300 |
]) |
| 301 |
|
| 302 |
with self.assertRaises(InvalidValue): |
| 303 |
ContentSecurityPolicyDirectiveFrameAncestors([ |
| 304 |
ContentSecurityPolicySourceNonce(bytearray(b'light work.')) |
| 305 |
]) |
| 306 |
|
| 307 |
with self.assertRaises(InvalidValue): |
| 308 |
ContentSecurityPolicyDirectiveFrameAncestors([ |
| 309 |
ContentSecurityPolicySourceHash(Hash.SHA2_256, bytearray(b'light work.')) |
| 310 |
]) |
| 311 |
|
| 312 |
|
| 313 |
class TestContentSecurityPolicyDirectiveWebrtc( |
| 314 |
TestCasesBasesHttpHeader.MinimalHeader): |
| 315 |
|
| 316 |
_header_minimal = ContentSecurityPolicyDirectiveWebrtc(ContentSecurityPolicyWebRtcType.ALLOW) |
| 317 |
_header_minimal_bytes = ' '.join([ |
| 318 |
'webrtc', |
| 319 |
'\'allow\'', |
| 320 |
]).encode('ascii') |
| 321 |
_header_minimal_markdown = '\'allow\'' |
| 322 |
|
| 323 |
def test_error_invalid_value_type(self): |
| 324 |
with self.assertRaises(InvalidValue) as context_manager: |
| 325 |
ContentSecurityPolicyDirectiveWebrtc('not-a-webrtc-type') |
| 326 |
self.assertEqual(context_manager.exception.value, 'not-a-webrtc-type') |
| 327 |
|
| 328 |
|
| 329 |
class TestContentSecurityPolicyDirectiveRequireTrustedTypesFor( |
| 330 |
TestCasesBasesHttpHeader.MinimalHeader): |
| 331 |
|
| 332 |
_header_minimal = ContentSecurityPolicyDirectiveRequireTrustedTypesFor([ |
| 333 |
ContentSecurityPolicyTrustedTypeSinkGroup.SCRIPT |
| 334 |
]) |
| 335 |
_header_minimal_bytes = ' '.join([ |
| 336 |
'require-trusted-types-for', |
| 337 |
'\'script\'', |
| 338 |
]).encode('ascii') |
| 339 |
_header_minimal_markdown = os.linesep.join([ |
| 340 |
'* Type: require-trusted-types-for', |
| 341 |
'* Sink Groups:', |
| 342 |
' 1. \'script\'', |
| 343 |
'', |
| 344 |
]) |
| 345 |
|
| 346 |
|
| 347 |
class TestContentSecurityPolicyDirectiveReportUri( |
| 348 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 349 |
TestCasesBasesHttpHeader.FullHeader): |
| 350 |
|
| 351 |
_header_minimal = ContentSecurityPolicyDirectiveReportUri(['http://example.com']) |
| 352 |
_header_minimal_bytes = ' '.join([ |
| 353 |
'report-uri', |
| 354 |
'http://example.com', |
| 355 |
]).encode('ascii') |
| 356 |
_header_minimal_markdown = os.linesep.join([ |
| 357 |
'* Type: report-uri', |
| 358 |
'* URI references:', |
| 359 |
' 1. http://example.com', |
| 360 |
'', |
| 361 |
]) |
| 362 |
_header_full = ContentSecurityPolicyDirectiveReportUri(['http://example.com/1', 'http://example.com/2']) |
| 363 |
_header_full_bytes = ' '.join([ |
| 364 |
'report-uri', |
| 365 |
'http://example.com/1', |
| 366 |
'http://example.com/2', |
| 367 |
]).encode('ascii') |
| 368 |
|
| 369 |
def test_min_reference_length(self): |
| 370 |
with self.assertRaises(InvalidValue) as context_manager: |
| 371 |
ContentSecurityPolicyDirectiveReportUri.parse_exact_size(b'report-uri') |
| 372 |
self.assertEqual(context_manager.exception.value, b'') |
| 373 |
|
| 374 |
|
| 375 |
class TestContentSecurityPolicyDirectiveReportTo( |
| 376 |
TestCasesBasesHttpHeader.MinimalHeader): |
| 377 |
|
| 378 |
_header_minimal = ContentSecurityPolicyDirectiveReportTo('token') |
| 379 |
_header_minimal_bytes = ' '.join([ |
| 380 |
'report-to', |
| 381 |
'token', |
| 382 |
]).encode('ascii') |
| 383 |
_header_minimal_markdown = os.linesep.join([ |
| 384 |
'* Token: token', |
| 385 |
'', |
| 386 |
]) |
| 387 |
|
| 388 |
|
| 389 |
class TestContentSecurityPolicyDirectiveSandbox( |
| 390 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 391 |
TestCasesBasesHttpHeader.FullHeader): |
| 392 |
|
| 393 |
_header_minimal = ContentSecurityPolicyDirectiveSandbox(['token']) |
| 394 |
_header_minimal_bytes = ' '.join([ |
| 395 |
'sandbox', |
| 396 |
'token', |
| 397 |
]).encode('ascii') |
| 398 |
_header_minimal_markdown = os.linesep.join([ |
| 399 |
'* Type: sandbox', |
| 400 |
'* Tokens:', |
| 401 |
' 1. token', |
| 402 |
'', |
| 403 |
]) |
| 404 |
_header_full = ContentSecurityPolicyDirectiveSandbox(['token1', 'token2']) |
| 405 |
_header_full_bytes = ' '.join([ |
| 406 |
'sandbox', |
| 407 |
'token1', |
| 408 |
'token2', |
| 409 |
]).encode('ascii') |
| 410 |
|
| 411 |
|
| 412 |
class TestContentSecurityPolicyDirectivePluginTypes( |
| 413 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 414 |
TestCasesBasesHttpHeader.FullHeader): |
| 415 |
|
| 416 |
_header_minimal = ContentSecurityPolicyDirectivePluginTypes([ |
| 417 |
FieldValueMimeType('html', MimeTypeRegistry.TEXT) |
| 418 |
]) |
| 419 |
_header_minimal_bytes = ' '.join([ |
| 420 |
'plugin-types', |
| 421 |
'text/html', |
| 422 |
]).encode('ascii') |
| 423 |
_header_minimal_markdown = os.linesep.join([ |
| 424 |
'* Type: plugin-types', |
| 425 |
'* MIME Types:', |
| 426 |
' 1.', |
| 427 |
' * Type: html', |
| 428 |
' * Registry: TEXT', |
| 429 |
'', |
| 430 |
]) |
| 431 |
_header_full = ContentSecurityPolicyDirectivePluginTypes([ |
| 432 |
FieldValueMimeType('html', MimeTypeRegistry.TEXT), |
| 433 |
FieldValueMimeType('csv', MimeTypeRegistry.TEXT), |
| 434 |
]) |
| 435 |
_header_full_bytes = ' '.join([ |
| 436 |
'plugin-types', |
| 437 |
'text/html', |
| 438 |
'text/csv', |
| 439 |
]).encode('ascii') |
| 440 |
|
| 441 |
|
| 442 |
class TestContentSecurityPolicyDirectiveNoValue( |
| 443 |
TestCasesBasesHttpHeader.MinimalHeader): |
| 444 |
|
| 445 |
_header_minimal = ContentSecurityPolicyDirectiveBlockAllMixedContent() |
| 446 |
_header_minimal_bytes = b'block-all-mixed-content' |
| 447 |
_header_minimal_markdown = os.linesep.join([ |
| 448 |
'* Type: block-all-mixed-content', |
| 449 |
'* Value: n/a', |
| 450 |
'', |
| 451 |
]) |
| 452 |
|
| 453 |
|
| 454 |
class TestHttpHeaderFieldValueContentSecurityPolicy( |
| 455 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 456 |
TestCasesBasesHttpHeader.FullHeader): |
| 457 |
|
| 458 |
_header_minimal = HttpHeaderFieldValueContentSecurityPolicy([ |
| 459 |
ContentSecurityPolicyDirectiveDefaultSrc([ |
| 460 |
ContentSecurityPolicySourceKeyword.SELF, |
| 461 |
ContentSecurityPolicySourceHash(Hash.SHA2_256, bytearray(b'light work.')), |
| 462 |
ContentSecurityPolicySourceNonce(bytearray(b'light work.')), |
| 463 |
ContentSecurityPolicySourceScheme('http'), |
| 464 |
ContentSecurityPolicySourceHost('http://example.com'), |
| 465 |
]) |
| 466 |
]) |
| 467 |
_header_minimal_bytes = ' '.join([ |
| 468 |
'default-src', |
| 469 |
'\'self\'', |
| 470 |
'sha256-bGlnaHQgd29yay4=', |
| 471 |
'nonce-bGlnaHQgd29yay4=', |
| 472 |
'http:', |
| 473 |
'http://example.com', |
| 474 |
]).encode('ascii') |
| 475 |
_header_minimal_markdown = os.linesep.join([ |
| 476 |
'* Directives:', |
| 477 |
' 1.', |
| 478 |
' * Type: default-src', |
| 479 |
' * Value:', |
| 480 |
' 1.', |
| 481 |
' * Type: KEYWORD', |
| 482 |
' * Value: \'self\'', |
| 483 |
' 2.', |
| 484 |
' * Type: HASH', |
| 485 |
' * Value:', |
| 486 |
' * Hash Algorithm: SHA-256', |
| 487 |
' * Hash Value: bGlnaHQgd29yay4=', |
| 488 |
' 3.', |
| 489 |
' * Type: NONCE', |
| 490 |
' * Value: bGlnaHQgd29yay4=', |
| 491 |
' 4.', |
| 492 |
' * Type: SCHEME', |
| 493 |
' * Value: http', |
| 494 |
' 5.', |
| 495 |
' * Type: HOST', |
| 496 |
' * Value: http://example.com', |
| 497 |
'', |
| 498 |
]) |
| 499 |
_header_full = HttpHeaderFieldValueContentSecurityPolicy([ |
| 500 |
ContentSecurityPolicyDirectiveChildSrc([ |
| 501 |
ContentSecurityPolicySourceKeyword.SELF, |
| 502 |
]), |
| 503 |
ContentSecurityPolicyDirectiveConnectSrc([ |
| 504 |
ContentSecurityPolicySourceKeyword.SELF, |
| 505 |
]), |
| 506 |
ContentSecurityPolicyDirectiveDefaultSrc([ |
| 507 |
ContentSecurityPolicySourceKeyword.SELF, |
| 508 |
]), |
| 509 |
ContentSecurityPolicyDirectiveFontSrc([ |
| 510 |
ContentSecurityPolicySourceKeyword.SELF, |
| 511 |
]), |
| 512 |
ContentSecurityPolicyDirectiveFrameSrc([ |
| 513 |
ContentSecurityPolicySourceKeyword.SELF, |
| 514 |
]), |
| 515 |
ContentSecurityPolicyDirectiveImgSrc([ |
| 516 |
ContentSecurityPolicySourceKeyword.SELF, |
| 517 |
]), |
| 518 |
ContentSecurityPolicyDirectiveManifestSrc([ |
| 519 |
ContentSecurityPolicySourceKeyword.SELF, |
| 520 |
]), |
| 521 |
ContentSecurityPolicyDirectiveMediaSrc([ |
| 522 |
ContentSecurityPolicySourceKeyword.SELF, |
| 523 |
]), |
| 524 |
ContentSecurityPolicyDirectiveObjectSrc([ |
| 525 |
ContentSecurityPolicySourceKeyword.SELF, |
| 526 |
]), |
| 527 |
ContentSecurityPolicyDirectivePrefetchSrc([ |
| 528 |
ContentSecurityPolicySourceKeyword.SELF, |
| 529 |
]), |
| 530 |
ContentSecurityPolicyDirectiveScriptSrc([ |
| 531 |
ContentSecurityPolicySourceKeyword.SELF, |
| 532 |
]), |
| 533 |
ContentSecurityPolicyDirectiveScriptSrcElem([ |
| 534 |
ContentSecurityPolicySourceKeyword.SELF, |
| 535 |
]), |
| 536 |
ContentSecurityPolicyDirectiveScriptSrcAttr([ |
| 537 |
ContentSecurityPolicySourceKeyword.SELF, |
| 538 |
]), |
| 539 |
ContentSecurityPolicyDirectiveStyleSrc([ |
| 540 |
ContentSecurityPolicySourceKeyword.SELF, |
| 541 |
]), |
| 542 |
ContentSecurityPolicyDirectiveStyleSrcElem([ |
| 543 |
ContentSecurityPolicySourceKeyword.SELF, |
| 544 |
]), |
| 545 |
ContentSecurityPolicyDirectiveStyleSrcAttr([ |
| 546 |
ContentSecurityPolicySourceKeyword.SELF, |
| 547 |
]), |
| 548 |
ContentSecurityPolicyDirectiveWebrtc( |
| 549 |
ContentSecurityPolicyWebRtcType.ALLOW, |
| 550 |
), |
| 551 |
ContentSecurityPolicyDirectiveWorkerSrc([ |
| 552 |
ContentSecurityPolicySourceKeyword.SELF, |
| 553 |
]), |
| 554 |
ContentSecurityPolicyDirectiveBaseUri([ |
| 555 |
ContentSecurityPolicySourceKeyword.SELF, |
| 556 |
]), |
| 557 |
ContentSecurityPolicyDirectiveSandbox([ |
| 558 |
'token' |
| 559 |
]), |
| 560 |
ContentSecurityPolicyDirectiveFormAction([ |
| 561 |
ContentSecurityPolicySourceKeyword.SELF, |
| 562 |
]), |
| 563 |
ContentSecurityPolicyDirectiveFrameAncestors([ |
| 564 |
ContentSecurityPolicySourceKeyword.SELF, |
| 565 |
]), |
| 566 |
ContentSecurityPolicyDirectiveReportUri([ |
| 567 |
'http://example.com' |
| 568 |
]), |
| 569 |
ContentSecurityPolicyDirectiveReportTo( |
| 570 |
'token' |
| 571 |
), |
| 572 |
ContentSecurityPolicyDirectiveBlockAllMixedContent(), |
| 573 |
ContentSecurityPolicyDirectiveUpgradeInsecureRequests(), |
| 574 |
ContentSecurityPolicyDirectiveReferrer( |
| 575 |
ContentSecurityPolicyReferrerPolicy.NO_REFERRER, |
| 576 |
), |
| 577 |
ContentSecurityPolicyDirectivePluginTypes([ |
| 578 |
FieldValueMimeType('html', MimeTypeRegistry.TEXT) |
| 579 |
]), |
| 580 |
]) |
| 581 |
_header_full_bytes = '; '.join([ |
| 582 |
'child-src \'self\'', |
| 583 |
'connect-src \'self\'', |
| 584 |
'default-src \'self\'', |
| 585 |
'font-src \'self\'', |
| 586 |
'frame-src \'self\'', |
| 587 |
'img-src \'self\'', |
| 588 |
'manifest-src \'self\'', |
| 589 |
'media-src \'self\'', |
| 590 |
'object-src \'self\'', |
| 591 |
'prefetch-src \'self\'', |
| 592 |
'script-src \'self\'', |
| 593 |
'script-src-elem \'self\'', |
| 594 |
'script-src-attr \'self\'', |
| 595 |
'style-src \'self\'', |
| 596 |
'style-src-elem \'self\'', |
| 597 |
'style-src-attr \'self\'', |
| 598 |
'webrtc \'allow\'', |
| 599 |
'worker-src \'self\'', |
| 600 |
'base-uri \'self\'', |
| 601 |
'sandbox token', |
| 602 |
'form-action \'self\'', |
| 603 |
'frame-ancestors \'self\'', |
| 604 |
'report-uri http://example.com', |
| 605 |
'report-to token', |
| 606 |
'block-all-mixed-content', |
| 607 |
'upgrade-insecure-requests', |
| 608 |
'referrer "no-referrer"', |
| 609 |
'plugin-types text/html', |
| 610 |
]).encode('ascii') |
| 611 |
|
| 612 |
|
| 613 |
class TestHttpHeaderFieldValueContentType( |
| 614 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 615 |
TestCasesBasesHttpHeader.FullHeader): |
| 616 |
|
| 617 |
_header_minimal = HttpHeaderFieldValueContentType( |
| 618 |
FieldValueMimeType('html', MimeTypeRegistry.TEXT) |
| 619 |
) |
| 620 |
_header_minimal_bytes = b'text/html' |
| 621 |
_header_minimal_markdown = os.linesep.join([ |
| 622 |
'* MIME type:', |
| 623 |
' * Type: html', |
| 624 |
' * Registry: TEXT', |
| 625 |
'* Charset: n/a', |
| 626 |
'* Boundary: n/a', |
| 627 |
'', |
| 628 |
]) |
| 629 |
|
| 630 |
_header_full = HttpHeaderFieldValueContentType( |
| 631 |
FieldValueMimeType('bhttp', MimeTypeRegistry.MESSAGE), |
| 632 |
charset='utf-8', |
| 633 |
boundary='boundary_pattern', |
| 634 |
) |
| 635 |
_header_full_bytes = b'message/bhttp; charset=utf-8; boundary=boundary_pattern' |
| 636 |
|
| 637 |
def test_error_invalid_parameter(self): |
| 638 |
with self.assertRaises(InvalidValue) as context_manager: |
| 639 |
HttpHeaderFieldValueContentType( |
| 640 |
FieldValueMimeType('html', MimeTypeRegistry.TEXT), |
| 641 |
boundary='pattern', |
| 642 |
) |
| 643 |
self.assertEqual(context_manager.exception.value, 'pattern') |
| 644 |
|
| 645 |
with self.assertRaises(InvalidValue) as context_manager: |
| 646 |
HttpHeaderFieldValueContentType( |
| 647 |
FieldValueMimeType('bhttp', MimeTypeRegistry.MESSAGE), |
| 648 |
) |
| 649 |
self.assertEqual(context_manager.exception.value, None) |
| 650 |
|
| 651 |
|
| 652 |
class TestHttpHeaderFieldValueNetworkErrorLogging( |
| 653 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 654 |
TestCasesBasesHttpHeader.FullHeader): |
| 655 |
_header_minimal = HttpHeaderFieldValueNetworkErrorLogging( |
| 656 |
report_to="network-errors", |
| 657 |
max_age=1, |
| 658 |
) |
| 659 |
_header_minimal_bytes = b'{"report_to": "network-errors", "max_age": 1}' |
| 660 |
_header_minimal_markdown = os.linesep.join([ |
| 661 |
'* Report To: network-errors', |
| 662 |
'* Max Age: 0:00:01', |
| 663 |
'* Include Subdomains: n/a', |
| 664 |
'* Success Fraction: n/a', |
| 665 |
'* Failure Fraction: n/a', |
| 666 |
'', |
| 667 |
]) |
| 668 |
|
| 669 |
_header_full = HttpHeaderFieldValueNetworkErrorLogging( |
| 670 |
report_to="network-errors", |
| 671 |
max_age=datetime.timedelta(1), |
| 672 |
include_subdomains=True, |
| 673 |
success_fraction=0.1, |
| 674 |
failure_fraction=0.9, |
| 675 |
) |
| 676 |
_header_full_bytes = b''.join([ |
| 677 |
b'{', |
| 678 |
b'"report_to": "network-errors", ', |
| 679 |
b'"max_age": 86400, ', |
| 680 |
b'"include_subdomains": true, ', |
| 681 |
b'"success_fraction": 0.1, ', |
| 682 |
b'"failure_fraction": 0.9', |
| 683 |
b'}', |
| 684 |
]) |
| 685 |
|
| 686 |
|
| 687 |
class TestHttpHeaderFieldValuePragma( |
| 688 |
TestCasesBasesHttpHeader.FullHeader, |
| 689 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 690 |
_header_full = HttpHeaderFieldValuePragma(HttpHeaderPragma.NO_CACHE) |
| 691 |
_header_full_bytes = b'no-cache' |
| 692 |
_header_full_upper_case_bytes = b'NO-CACHE' |
| 693 |
_header_full_lower_case_bytes = b'no-cache' |
| 694 |
|
| 695 |
|
| 696 |
class TestHttpHeaderFieldValueSTS( |
| 697 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 698 |
TestCasesBasesHttpHeader.FullHeader, |
| 699 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 700 |
_header_minimal = HttpHeaderFieldValueSTS(max_age=datetime.timedelta(seconds=1)) |
| 701 |
_header_minimal_bytes = b'max-age=1' |
| 702 |
_header_minimal_markdown = os.linesep.join([ |
| 703 |
'* Max Age: 0:00:01', |
| 704 |
'* Include Subdomains: no', |
| 705 |
'* Preload: no', |
| 706 |
'', |
| 707 |
]) |
| 708 |
|
| 709 |
_header_full = HttpHeaderFieldValueSTS(max_age=datetime.timedelta(seconds=1), include_subdomains=True, preload=True) |
| 710 |
_header_full_bytes = b'max-age=1; includeSubDomains; preload' |
| 711 |
_header_full_upper_case_bytes = b'MAX-AGE=1; INCLUDESUBDOMAINS; PRELOAD' |
| 712 |
_header_full_lower_case_bytes = b'max-age=1; includesubdomains; preload' |
| 713 |
|
| 714 |
|
| 715 |
class TestHttpHeaderFieldValueExpectStaple( |
| 716 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 717 |
TestCasesBasesHttpHeader.FullHeader, |
| 718 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 719 |
_header_minimal = HttpHeaderFieldValueExpectStaple(max_age=datetime.timedelta(seconds=1)) |
| 720 |
_header_minimal_bytes = b'max-age=1' |
| 721 |
_header_minimal_markdown = os.linesep.join([ |
| 722 |
'* Max Age: 0:00:01', |
| 723 |
'* Include Subdomains: no', |
| 724 |
'* Preload: no', |
| 725 |
'* Report Uri: n/a', |
| 726 |
'', |
| 727 |
]) |
| 728 |
|
| 729 |
_header_full = HttpHeaderFieldValueExpectStaple( |
| 730 |
max_age=datetime.timedelta(seconds=1), |
| 731 |
include_subdomains=True, |
| 732 |
preload=True, |
| 733 |
report_uri="http://example.com" |
| 734 |
) |
| 735 |
_header_full_bytes = b'max-age=1; includeSubDomains; preload; report-uri="http://example.com"' |
| 736 |
_header_full_upper_case_bytes = b'MAX-AGE=1; INCLUDESUBDOMAINS; PRELOAD; REPORT-URI="http://example.com"' |
| 737 |
_header_full_lower_case_bytes = b'max-age=1; includesubdomains; preload; report-uri="http://example.com"' |
| 738 |
|
| 739 |
|
| 740 |
class TestHttpHeaderFieldValueExpectCT( |
| 741 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 742 |
TestCasesBasesHttpHeader.FullHeader, |
| 743 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 744 |
_header_minimal = HttpHeaderFieldValueExpectCT(max_age=datetime.timedelta(seconds=1)) |
| 745 |
_header_minimal_bytes = b'max-age=1' |
| 746 |
_header_minimal_markdown = os.linesep.join([ |
| 747 |
'* Max Age: 0:00:01', |
| 748 |
'* Enforce: no', |
| 749 |
'* Report Uri: n/a', |
| 750 |
'', |
| 751 |
]) |
| 752 |
|
| 753 |
_header_full = HttpHeaderFieldValueExpectCT( |
| 754 |
max_age=datetime.timedelta(seconds=1), |
| 755 |
enforce=True, |
| 756 |
report_uri='http://example.com' |
| 757 |
) |
| 758 |
_header_full_bytes = b'max-age=1, enforce, report-uri="http://example.com"' |
| 759 |
_header_full_upper_case_bytes = b'MAX-AGE=1, ENFORCE, REPORT-URI="http://example.com"' |
| 760 |
_header_full_lower_case_bytes = b'max-age=1, enforce, report-uri="http://example.com"' |
| 761 |
|
| 762 |
|
| 763 |
class TestHttpHeaderFieldValuePublicKeyPinning( |
| 764 |
TestCasesBasesHttpHeader.MinimalHeader, |
| 765 |
TestCasesBasesHttpHeader.FullHeader, |
| 766 |
TestCasesBasesHttpHeader.CaseInsensitiveHeader): |
| 767 |
_header_minimal = HttpHeaderFieldValuePublicKeyPinning( |
| 768 |
pin_sha256='cGluLXNoYTI1Ng==', |
| 769 |
max_age=datetime.timedelta(seconds=1), |
| 770 |
) |
| 771 |
_header_minimal_bytes = b'pin-sha256="cGluLXNoYTI1Ng=="; max-age=1' |
| 772 |
_header_minimal_markdown = os.linesep.join([ |
| 773 |
'* Pin (SHA-256): cGluLXNoYTI1Ng==', |
| 774 |
'* Max Age: 0:00:01', |
| 775 |
'* Include Subdomains: no', |
| 776 |
'* Report Uri: n/a', |
| 777 |
'', |
| 778 |
]) |
| 779 |
|
| 780 |
_header_full = HttpHeaderFieldValuePublicKeyPinning( |
| 781 |
pin_sha256='cGluLXNoYTI1Ng==', |
| 782 |
max_age=datetime.timedelta(seconds=1), |
| 783 |
include_subdomains=True, |
| 784 |
report_uri='http://example.com' |
| 785 |
) |
| 786 |
_header_full_bytes = b'; '.join([ |
| 787 |
b'pin-sha256="cGluLXNoYTI1Ng=="', |
| 788 |
b'max-age=1', |
| 789 |
b'includeSubDomains', |
| 790 |
b'report-uri="http://example.com"', |
| 791 |
]) |
| 792 |
_header_full_upper_case_bytes = b'; '.join([ |
| 793 |
b'PIN-SHA256="cGluLXNoYTI1Ng=="', |
| 794 |
b'MAX-AGE=1', |
| 795 |
b'INCLUDESUBDOMAINS', |
| 796 |
b'REPORT-URI="http://example.com"', |
| 797 |
]) |
| 798 |
_header_full_lower_case_bytes = b'; '.join([ |
| 799 |
b'pin-sha256="cGluLXNoYTI1Ng=="', |
| 800 |
b'max-age=1', |
| 801 |
b'includesubdomains', |
| 802 |
b'report-uri="http://example.com"', |
| 803 |
]) |
| 804 |
|
| 805 |
|
| 806 |
class TestHttpHeaderFieldValueSetCookie(TestCasesBasesHttpHeader.MinimalHeader, TestCasesBasesHttpHeader.FullHeader): |
| 807 |
_header_minimal = HttpHeaderFieldValueSetCookie('name', 'value') |
| 808 |
_header_minimal_bytes = b'name=value' |
| 809 |
_header_minimal_markdown = os.linesep.join([ |
| 810 |
'* Name: name', |
| 811 |
'* Value: value', |
| 812 |
'* Expires: n/a', |
| 813 |
'* Max Age: n/a', |
| 814 |
'* Domain: n/a', |
| 815 |
'* Path: n/a', |
| 816 |
'* Secure: no', |
| 817 |
'* Http Only: no', |
| 818 |
'* Same Site: n/a', |
| 819 |
'' |
| 820 |
]) |
| 821 |
|
| 822 |
_header_full = HttpHeaderFieldValueSetCookie( |
| 823 |
name='name', value='value', |
| 824 |
expires=datetime.datetime.fromtimestamp(0, datetime.timezone.utc), |
| 825 |
max_age=datetime.timedelta(seconds=1), |
| 826 |
domain='example.com', |
| 827 |
path='/', |
| 828 |
secure=True, |
| 829 |
http_only=True, |
| 830 |
same_site=HttpHeaderSetCookieComponentSameSite.LAX, |
| 831 |
) |
| 832 |
_header_full_bytes = b'; '.join([ |
| 833 |
b'name=value', |
| 834 |
b'expires=Thu, 01 Jan 1970 00:00:00 GMT', |
| 835 |
b'max-age=1', |
| 836 |
b'Domain=example.com', |
| 837 |
b'Path=/', |
| 838 |
b'Secure', |
| 839 |
b'HttpOnly', |
| 840 |
b'SameSite=Lax', |
| 841 |
]) |
| 842 |
|
| 843 |
|
| 844 |
class TestHttpHeaderFieldValueXContentTypeOptions(TestCasesBasesHttpHeader.FullHeader): |
| 845 |
_header_full = HttpHeaderFieldValueXContentTypeOptions(HttpHeaderXContentTypeOptions.NOSNIFF) |
| 846 |
_header_full_bytes = b'nosniff' |
| 847 |
|
| 848 |
|
| 849 |
class TestHttpHeaderFieldValueXFrameOptions(TestCasesBasesHttpHeader.FullHeader): |
| 850 |
_header_full = HttpHeaderFieldValueXFrameOptions(HttpHeaderXFrameOptions.SAMEORIGIN) |
| 851 |
_header_full_bytes = b'SAMEORIGIN' |
| 852 |
|
| 853 |
|
| 854 |
class TestHttpHeaderFieldValueXXSSProtection(TestCasesBasesHttpHeader.FullHeader): |
| 855 |
_header_full = HttpHeaderFieldValueXXSSProtection( |
| 856 |
HttpHeaderXXSSProtectionState.ENABLED, |
| 857 |
HttpHeaderXXSSProtectionMode.BLOCK, |
| 858 |
'http://example.com' |
| 859 |
) |
| 860 |
_header_full_bytes = b'1; mode=block; report=http://example.com' |
| 861 |
|
| 862 |
|
| 863 |
class TestHttpHeaderFieldValueReferrerPolicy(TestCasesBasesHttpHeader.FullHeader): |
| 864 |
_header_full = HttpHeaderFieldValueReferrerPolicy(HttpHeaderReferrerPolicy.SAME_ORIGIN) |
| 865 |
_header_full_bytes = b'same-origin' |
| 866 |
|
| 867 |
|
| 868 |
class TestHttpHeaderFieldName(unittest.TestCase): |
| 869 |
def test_markdown(self): |
| 870 |
self.assertEqual( |
| 871 |
HttpHeaderFieldName.STRICT_TRANSPORT_SECURITY.value.as_markdown(), |
| 872 |
'Strict-Transport-Security', |
| 873 |
) |
| 874 |
|
| 875 |
def test_from_name(self): |
| 876 |
with self.assertRaises(InvalidValue) as context_manager: |
| 877 |
HttpHeaderFieldName.from_name('non-existing-name') |
| 878 |
self.assertEqual(context_manager.exception.value, 'non-existing-name') |
| 879 |
|
| 880 |
self.assertEqual( |
| 881 |
HttpHeaderFieldName.from_name('strict-transport-security'), |
| 882 |
HttpHeaderFieldName.STRICT_TRANSPORT_SECURITY |
| 883 |
) |
| 884 |
self.assertEqual( |
| 885 |
HttpHeaderFieldName.from_name('STRICT-TRANSPORT-SECURITY'), |
| 886 |
HttpHeaderFieldName.STRICT_TRANSPORT_SECURITY |
| 887 |
) |
| 888 |
self.assertEqual( |
| 889 |
HttpHeaderFieldName.from_name('Strict-Transport-Security'), |
| 890 |
HttpHeaderFieldName.STRICT_TRANSPORT_SECURITY |
| 891 |
) |
| 892 |
|
| 893 |
|
| 894 |
class TestHttpHeaderFields(unittest.TestCase): |
| 895 |
def setUp(self): |
| 896 |
self.headers_bytes = b'\r\n'.join([ |
| 897 |
b'Age: 1', |
| 898 |
b'Cache-Control: no-cache', |
| 899 |
b'Content-Type: text/html', |
| 900 |
b'Date: Thu, 01 Jan 1970 00:00:00 GMT', |
| 901 |
b'ETag: 12345678', |
| 902 |
b'Expect-CT: max-age=1', |
| 903 |
b'Expect-Staple: max-age=1', |
| 904 |
b'Expires: Thu, 01 Jan 1970 00:00:00 GMT', |
| 905 |
b'Last-Modified: Thu, 01 Jan 1970 00:00:00 GMT', |
| 906 |
b'NEL: {"report_to": "network-errors", "max_age": 1}', |
| 907 |
b'Pragma: no-cache', |
| 908 |
b'Public-Key-Pinning: pin-sha256="cGluLXNoYTI1Ng=="; max-age=1', |
| 909 |
b'Referrer-Policy: origin', |
| 910 |
b'Server: server', |
| 911 |
b'Set-Cookie: name=value', |
| 912 |
b'Strict-Transport-Security: max-age=1', |
| 913 |
b'X-Unparsed: Value', |
| 914 |
b'X-Content-Type-Options: nosniff', |
| 915 |
b'X-Frame-Options: SAMEORIGIN', |
| 916 |
b'X-XSS-Protection: 1', |
| 917 |
b'Content-Security-Policy: default-src \'self\'', |
| 918 |
b'Content-Security-Policy-Report-Only: default-src \'self\'', |
| 919 |
b'X-Content-Security-Policy: default-src \'self\'', |
| 920 |
b'', |
| 921 |
b'', |
| 922 |
]) |
| 923 |
self.headers = HttpHeaderFields([ |
| 924 |
HttpHeaderFieldAge(datetime.timedelta(seconds=1)), |
| 925 |
HttpHeaderFieldCacheControlResponse(HttpHeaderFieldValueCacheControlResponse(no_cache=True)), |
| 926 |
HttpHeaderFieldContentType(FieldValueMimeType('html', MimeTypeRegistry.TEXT)), |
| 927 |
HttpHeaderFieldDate(datetime.datetime.fromtimestamp(0, datetime.timezone.utc)), |
| 928 |
HttpHeaderFieldETag('12345678'), |
| 929 |
HttpHeaderFieldExpectCT(HttpHeaderFieldValueExpectCT(datetime.timedelta(seconds=1))), |
| 930 |
HttpHeaderFieldExpectStaple(HttpHeaderFieldValueExpectStaple(datetime.timedelta(seconds=1))), |
| 931 |
HttpHeaderFieldExpires(datetime.datetime.fromtimestamp(0, datetime.timezone.utc)), |
| 932 |
HttpHeaderFieldLastModified(datetime.datetime.fromtimestamp(0, datetime.timezone.utc)), |
| 933 |
HttpHeaderFieldNetworkErrorLogging(HttpHeaderFieldValueNetworkErrorLogging( |
| 934 |
report_to="network-errors", max_age=1 |
| 935 |
)), |
| 936 |
HttpHeaderFieldPragma(HttpHeaderPragma.NO_CACHE), |
| 937 |
HttpHeaderFieldPublicKeyPinning(HttpHeaderFieldValuePublicKeyPinning( |
| 938 |
pin_sha256='cGluLXNoYTI1Ng==', |
| 939 |
max_age=datetime.timedelta(seconds=1), |
| 940 |
)), |
| 941 |
HttpHeaderFieldReferrerPolicy(HttpHeaderReferrerPolicy.ORIGIN), |
| 942 |
HttpHeaderFieldServer('server'), |
| 943 |
HttpHeaderFieldSetCookie(HttpHeaderFieldValueSetCookie('name', 'value')), |
| 944 |
HttpHeaderFieldSTS(HttpHeaderFieldValueSTS(datetime.timedelta(seconds=1))), |
| 945 |
HttpHeaderFieldUnparsed('X-Unparsed', 'Value'), |
| 946 |
HttpHeaderFieldXContentTypeOptions(HttpHeaderXContentTypeOptions.NOSNIFF), |
| 947 |
HttpHeaderFieldXFrameOptions(HttpHeaderXFrameOptions.SAMEORIGIN), |
| 948 |
HttpHeaderFieldXXSSProtection(HttpHeaderXXSSProtectionState.ENABLED), |
| 949 |
HttpHeaderFieldContentSecurityPolicy([ |
| 950 |
ContentSecurityPolicyDirectiveDefaultSrc([ContentSecurityPolicySourceKeyword.SELF]) |
| 951 |
]), |
| 952 |
HttpHeaderFieldContentSecurityPolicyReportOnly([ |
| 953 |
ContentSecurityPolicyDirectiveDefaultSrc([ContentSecurityPolicySourceKeyword.SELF]) |
| 954 |
]), |
| 955 |
HttpHeaderFieldXContentSecurityPolicy([ |
| 956 |
ContentSecurityPolicyDirectiveDefaultSrc([ContentSecurityPolicySourceKeyword.SELF]) |
| 957 |
]), |
| 958 |
]) |
| 959 |
|
| 960 |
def test_parse(self): |
| 961 |
self.assertEqual( |
| 962 |
HttpHeaderFields.parse_exact_size(self.headers_bytes), |
| 963 |
self.headers |
| 964 |
) |
| 965 |
|
| 966 |
def test_compose(self): |
| 967 |
self.assertEqual(self.headers.compose(), self.headers_bytes) |
| 968 |
|
| 969 |
def test_markdown(self): |
| 970 |
self.assertEqual(self.headers.as_markdown(), os.linesep.join([ |
| 971 |
'1.', |
| 972 |
' * Name: Age', |
| 973 |
' * Value: 1', |
| 974 |
'2.', |
| 975 |
' * Name: Cache-Control', |
| 976 |
' * Value:', |
| 977 |
' * Max Age: n/a', |
| 978 |
' * S Maxage: n/a', |
| 979 |
' * Must Revalidate: no', |
| 980 |
' * Proxy Revalidate: no', |
| 981 |
' * No Cache: yes', |
| 982 |
' * No Store: no', |
| 983 |
' * Public: no', |
| 984 |
' * Private: no', |
| 985 |
' * No Transform: no', |
| 986 |
'3.', |
| 987 |
' * Name: Content-Type', |
| 988 |
' * Value:', |
| 989 |
' * MIME type:', |
| 990 |
' * Type: html', |
| 991 |
' * Registry: TEXT', |
| 992 |
' * Charset: n/a', |
| 993 |
' * Boundary: n/a', |
| 994 |
'4.', |
| 995 |
' * Name: Date', |
| 996 |
' * Value: 1970-01-01 00:00:00+00:00', |
| 997 |
'5.', |
| 998 |
' * Name: ETag', |
| 999 |
' * Value: 12345678', |
| 1000 |
'6.', |
| 1001 |
' * Name: Expect-CT', |
| 1002 |
' * Value:', |
| 1003 |
' * Max Age: 0:00:01', |
| 1004 |
' * Enforce: no', |
| 1005 |
' * Report Uri: n/a', |
| 1006 |
'7.', |
| 1007 |
' * Name: Expect-Staple', |
| 1008 |
' * Value:', |
| 1009 |
' * Max Age: 0:00:01', |
| 1010 |
' * Include Subdomains: no', |
| 1011 |
' * Preload: no', |
| 1012 |
' * Report Uri: n/a', |
| 1013 |
'8.', |
| 1014 |
' * Name: Expires', |
| 1015 |
' * Value: 1970-01-01 00:00:00+00:00', |
| 1016 |
'9.', |
| 1017 |
' * Name: Last-Modified', |
| 1018 |
' * Value: 1970-01-01 00:00:00+00:00', |
| 1019 |
'10.', |
| 1020 |
' * Name: NEL', |
| 1021 |
' * Value:', |
| 1022 |
' * Report To: network-errors', |
| 1023 |
' * Max Age: 0:00:01', |
| 1024 |
' * Include Subdomains: n/a', |
| 1025 |
' * Success Fraction: n/a', |
| 1026 |
' * Failure Fraction: n/a', |
| 1027 |
'11.', |
| 1028 |
' * Name: Pragma', |
| 1029 |
' * Value: no-cache', |
| 1030 |
'12.', |
| 1031 |
' * Name: Public-Key-Pinning', |
| 1032 |
' * Value:', |
| 1033 |
' * Pin (SHA-256): cGluLXNoYTI1Ng==', |
| 1034 |
' * Max Age: 0:00:01', |
| 1035 |
' * Include Subdomains: no', |
| 1036 |
' * Report Uri: n/a', |
| 1037 |
'13.', |
| 1038 |
' * Name: Referrer-Policy', |
| 1039 |
' * Value: origin', |
| 1040 |
'14.', |
| 1041 |
' * Name: Server', |
| 1042 |
' * Value: server', |
| 1043 |
'15.', |
| 1044 |
' * Name: Set-Cookie', |
| 1045 |
' * Value:', |
| 1046 |
' * Name: name', |
| 1047 |
' * Value: value', |
| 1048 |
' * Expires: n/a', |
| 1049 |
' * Max Age: n/a', |
| 1050 |
' * Domain: n/a', |
| 1051 |
' * Path: n/a', |
| 1052 |
' * Secure: no', |
| 1053 |
' * Http Only: no', |
| 1054 |
' * Same Site: n/a', |
| 1055 |
'16.', |
| 1056 |
' * Name: Strict-Transport-Security', |
| 1057 |
' * Value:', |
| 1058 |
' * Max Age: 0:00:01', |
| 1059 |
' * Include Subdomains: no', |
| 1060 |
' * Preload: no', |
| 1061 |
'17.', |
| 1062 |
' * Name: X-Unparsed', |
| 1063 |
' * Value: Value', |
| 1064 |
'18.', |
| 1065 |
' * Name: X-Content-Type-Options', |
| 1066 |
' * Value: nosniff', |
| 1067 |
'19.', |
| 1068 |
' * Name: X-Frame-Options', |
| 1069 |
' * Value: SAMEORIGIN', |
| 1070 |
'20.', |
| 1071 |
' * Name: X-XSS-Protection', |
| 1072 |
' * Value:', |
| 1073 |
' * State: enabled', |
| 1074 |
' * Mode: n/a', |
| 1075 |
' * Report: n/a', |
| 1076 |
'21.', |
| 1077 |
' * Name: Content-Security-Policy', |
| 1078 |
' * Value:', |
| 1079 |
' * Directives:', |
| 1080 |
' 1.', |
| 1081 |
' * Type: default-src', |
| 1082 |
' * Value:', |
| 1083 |
' 1.', |
| 1084 |
' * Type: KEYWORD', |
| 1085 |
' * Value: \'self\'', |
| 1086 |
'22.', |
| 1087 |
' * Name: Content-Security-Policy-Report-Only', |
| 1088 |
' * Value:', |
| 1089 |
' * Directives:', |
| 1090 |
' 1.', |
| 1091 |
' * Type: default-src', |
| 1092 |
' * Value:', |
| 1093 |
' 1.', |
| 1094 |
' * Type: KEYWORD', |
| 1095 |
' * Value: \'self\'', |
| 1096 |
'23.', |
| 1097 |
' * Name: X-Content-Security-Policy', |
| 1098 |
' * Value:', |
| 1099 |
' * Directives:', |
| 1100 |
' 1.', |
| 1101 |
' * Type: default-src', |
| 1102 |
' * Value:', |
| 1103 |
' 1.', |
| 1104 |
' * Type: KEYWORD', |
| 1105 |
' * Value: \'self\'', |
| 1106 |
'', |
| 1107 |
])) |
| 1108 |
|
| 1109 |
|
| 1110 |
class TestHttpHeaderFieldUnparsed(unittest.TestCase): |
| 1111 |
def test_error(self): |
| 1112 |
with self.assertRaises(InvalidValue) as context_manager: |
| 1113 |
HttpHeaderFieldUnparsed.parse_immutable(b'name: value') |
| 1114 |
self.assertEqual(context_manager.exception.value, b'value') |
| 1115 |
|
| 1116 |
with self.assertRaises(InvalidValue) as context_manager: |
| 1117 |
HttpHeaderFieldUnparsed.parse_immutable(b'name value') |
| 1118 |
self.assertEqual(context_manager.exception.value, b'') |
| 1119 |
|
| 1120 |
def test_parse(self): |
| 1121 |
parsable = b'name: value\r\n' |
| 1122 |
header, parsed_length = HttpHeaderFieldUnparsed.parse_immutable(parsable) |
| 1123 |
self.assertEqual(header.name, 'name') |
| 1124 |
self.assertEqual(header.value, 'value') |
| 1125 |
self.assertEqual(parsable[parsed_length:], b'\r\n') |
| 1126 |
|
| 1127 |
parsable = b'name: value\r\n' |
| 1128 |
header, parsed_length = HttpHeaderFieldUnparsed.parse_immutable(parsable) |
| 1129 |
self.assertEqual(header.name, 'name') |
| 1130 |
self.assertEqual(header.value, 'value') |
| 1131 |
self.assertEqual(parsable[parsed_length:], b'\r\n') |
| 1132 |
|
| 1133 |
def test_compose(self): |
| 1134 |
header = HttpHeaderFieldUnparsed('name', 'value') |
| 1135 |
self.assertEqual(header.compose(), b'name: value') |
| 1136 |
|
| 1137 |
def test_markdown(self): |
| 1138 |
header = HttpHeaderFieldUnparsed('name', 'value') |
| 1139 |
self.assertEqual(header.as_markdown(), os.linesep.join([ |
| 1140 |
'* Name: name', |
| 1141 |
'* Value: value', |
| 1142 |
'' |
| 1143 |
])) |