|
| 1 | +<!DOCTYPE html> |
| 2 | +<title>Service Worker: Service-Worker-Allowed header</title> |
| 3 | +<script src="/resources/testharness.js"></script> |
| 4 | +<script src="/resources/testharnessreport.js"></script> |
| 5 | +<script src="/common/get-host-info.sub.js"></script> |
| 6 | +<script src="resources/test-helpers.sub.js"></script> |
| 7 | +<script> |
| 8 | + |
| 9 | +const host_info = get_host_info(); |
| 10 | + |
| 11 | +// Returns a URL for a service worker script whose Service-Worker-Allowed |
| 12 | +// header value is set to |allowed_path|. If |origin| is specified, that origin |
| 13 | +// is used. |
| 14 | +function build_script_url(allowed_path, origin) { |
| 15 | + const script = 'resources/empty-worker.js'; |
| 16 | + const url = origin ? `${origin}${base_path()}${script}` : script; |
| 17 | + return `${url}?pipe=header(Service-Worker-Allowed,${allowed_path})`; |
| 18 | +} |
| 19 | + |
| 20 | +promise_test(async t => { |
| 21 | + const script = build_script_url('/allowed-path'); |
| 22 | + const scope = '/allowed-path'; |
| 23 | + const registration = await service_worker_unregister_and_register( |
| 24 | + t, script, scope); |
| 25 | + assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); |
| 26 | + assert_equals(registration.scope, normalizeURL(scope)); |
| 27 | + return registration.unregister(); |
| 28 | +}, 'Registering within Service-Worker-Allowed path'); |
| 29 | + |
| 30 | +promise_test(async t => { |
| 31 | + const script = build_script_url(new URL('/allowed-path', document.location)); |
| 32 | + const scope = '/allowed-path'; |
| 33 | + const registration = await service_worker_unregister_and_register( |
| 34 | + t, script, scope); |
| 35 | + assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); |
| 36 | + assert_equals(registration.scope, normalizeURL(scope)); |
| 37 | + return registration.unregister(); |
| 38 | +}, 'Registering within Service-Worker-Allowed path (absolute URL)'); |
| 39 | + |
| 40 | +promise_test(async t => { |
| 41 | + const script = build_script_url('../allowed-path-with-parent'); |
| 42 | + const scope = 'allowed-path-with-parent'; |
| 43 | + const registration = await service_worker_unregister_and_register( |
| 44 | + t, script, scope); |
| 45 | + assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); |
| 46 | + assert_equals(registration.scope, normalizeURL(scope)); |
| 47 | + return registration.unregister(); |
| 48 | +}, 'Registering within Service-Worker-Allowed path with parent reference'); |
| 49 | + |
| 50 | +promise_test(async t => { |
| 51 | + const script = build_script_url('../allowed-path'); |
| 52 | + const scope = '/disallowed-path'; |
| 53 | + await service_worker_unregister(t, scope); |
| 54 | + return promise_rejects(t, |
| 55 | + 'SecurityError', |
| 56 | + navigator.serviceWorker.register(script, {scope: scope}), |
| 57 | + 'register should fail'); |
| 58 | +}, 'Registering outside Service-Worker-Allowed path'); |
| 59 | + |
| 60 | +promise_test(async t => { |
| 61 | + const script = build_script_url('../allowed-path-with-parent'); |
| 62 | + const scope = '/allowed-path-with-parent'; |
| 63 | + await service_worker_unregister(t, scope); |
| 64 | + return promise_rejects(t, |
| 65 | + 'SecurityError', |
| 66 | + navigator.serviceWorker.register(script, {scope: scope}), |
| 67 | + 'register should fail'); |
| 68 | +}, 'Registering outside Service-Worker-Allowed path with parent reference'); |
| 69 | + |
| 70 | +promise_test(async t => { |
| 71 | + const script = build_script_url( |
| 72 | + host_info.HTTPS_REMOTE_ORIGIN + '/'); |
| 73 | + const scope = 'resources/this-scope-is-normally-allowed' |
| 74 | + const registration = await service_worker_unregister_and_register( |
| 75 | + t, script, scope); |
| 76 | + assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); |
| 77 | + assert_equals(registration.scope, normalizeURL(scope)); |
| 78 | + return registration.unregister(); |
| 79 | +}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally allowed scope'); |
| 80 | + |
| 81 | +promise_test(async t => { |
| 82 | + const script = build_script_url( |
| 83 | + host_info.HTTPS_REMOTE_ORIGIN + '/'); |
| 84 | + const scope = '/this-scope-is-normally-disallowed' |
| 85 | + const registration = await service_worker_unregister_and_register( |
| 86 | + t, script, scope); |
| 87 | + assert_true(registration instanceof ServiceWorkerRegistration, 'registered'); |
| 88 | + assert_equals(registration.scope, normalizeURL(scope)); |
| 89 | + return registration.unregister(); |
| 90 | +}, 'Service-Worker-Allowed is cross-origin to script, registering on a normally disallowed scope'); |
| 91 | + |
| 92 | +promise_test(async t => { |
| 93 | + const script = build_script_url( |
| 94 | + host_info.HTTPS_REMOTE_ORIGIN + '/cross-origin/', |
| 95 | + host_info.HTTPS_REMOTE_ORIGIN); |
| 96 | + const scope = '/cross-origin/'; |
| 97 | + await service_worker_unregister(t, scope); |
| 98 | + return promise_rejects(t, |
| 99 | + 'SecurityError', |
| 100 | + navigator.serviceWorker.register(script, {scope: scope}), |
| 101 | + 'register should fail'); |
| 102 | +}, 'Service-Worker-Allowed is cross-origin to page, same-origin to script'); |
| 103 | + |
| 104 | +</script> |
0 commit comments