|
| 1 | +/* |
| 2 | + * Copyright (c) 2023 Ronald Brill. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + * See the License for the specific language governing permissions and |
| 13 | + * limitations under the License. |
| 14 | + */ |
| 15 | +package org.htmlunit.csp; |
| 16 | + |
| 17 | +public enum FetchDirectiveKind { |
| 18 | + ChildSrc("child-src"), |
| 19 | + ConnectSrc("connect-src"), |
| 20 | + DefaultSrc("default-src"), |
| 21 | + FontSrc("font-src"), |
| 22 | + FrameSrc("frame-src"), |
| 23 | + ImgSrc("img-src"), |
| 24 | + ManifestSrc("manifest-src"), |
| 25 | + MediaSrc("media-src"), |
| 26 | + ObjectSrc("object-src"), |
| 27 | + PrefetchSrc("prefetch-src"), |
| 28 | + ScriptSrcAttr("script-src-attr"), |
| 29 | + ScriptSrc("script-src"), |
| 30 | + ScriptSrcElem("script-src-elem"), |
| 31 | + StyleSrcAttr("style-src-attr"), |
| 32 | + StyleSrc("style-src"), |
| 33 | + StyleSrcElem("style-src-elem"), |
| 34 | + WorkerSrc("worker-src"); |
| 35 | + |
| 36 | + private final String repr_; |
| 37 | + |
| 38 | + FetchDirectiveKind(final String repr) { |
| 39 | + this.repr_ = repr; |
| 40 | + } |
| 41 | + |
| 42 | + public String getRepr() { |
| 43 | + return repr_; |
| 44 | + } |
| 45 | + |
| 46 | + // returns null if not matched |
| 47 | + public static FetchDirectiveKind fromString(final String name) { |
| 48 | + switch (name) { |
| 49 | + case "child-src": |
| 50 | + return ChildSrc; |
| 51 | + case "connect-src": |
| 52 | + return ConnectSrc; |
| 53 | + case "default-src": |
| 54 | + return DefaultSrc; |
| 55 | + case "font-src": |
| 56 | + return FontSrc; |
| 57 | + case "frame-src": |
| 58 | + return FrameSrc; |
| 59 | + case "img-src": |
| 60 | + return ImgSrc; |
| 61 | + case "manifest-src": |
| 62 | + return ManifestSrc; |
| 63 | + case "media-src": |
| 64 | + return MediaSrc; |
| 65 | + case "object-src": |
| 66 | + return ObjectSrc; |
| 67 | + case "prefetch-src": |
| 68 | + return PrefetchSrc; |
| 69 | + case "script-src-attr": |
| 70 | + return ScriptSrcAttr; |
| 71 | + case "script-src": |
| 72 | + return ScriptSrc; |
| 73 | + case "script-src-elem": |
| 74 | + return ScriptSrcElem; |
| 75 | + case "style-src-attr": |
| 76 | + return StyleSrcAttr; |
| 77 | + case "style-src": |
| 78 | + return StyleSrc; |
| 79 | + case "style-src-elem": |
| 80 | + return StyleSrcElem; |
| 81 | + case "worker-src": |
| 82 | + return WorkerSrc; |
| 83 | + default: |
| 84 | + return null; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // https://w3c.github.io/webappsec-csp/#directive-fallback-list |
| 89 | + // Note the oddity that worker-src falls back to child-src then script-src |
| 90 | + // then directive-src, but frame-src falls back to child-src then directly default-src |
| 91 | + // Also note that `script-src` falls back to `default-src` for "unsafe-eval", but this |
| 92 | + // is done manually in prose rather than in this table |
| 93 | + // (in https://w3c.github.io/webappsec-csp/#can-compile-strings ) |
| 94 | + // It is included here only for completeness |
| 95 | + private static final FetchDirectiveKind[] ScriptSrcFallback |
| 96 | + = new FetchDirectiveKind[] {ScriptSrc, DefaultSrc}; |
| 97 | + private static final FetchDirectiveKind[] ScriptSrcElemFallback |
| 98 | + = new FetchDirectiveKind[] {ScriptSrcElem, ScriptSrc, DefaultSrc}; |
| 99 | + private static final FetchDirectiveKind[] ScriptSrcAttrFallback |
| 100 | + = new FetchDirectiveKind[] {ScriptSrcAttr, ScriptSrc, DefaultSrc}; |
| 101 | + private static final FetchDirectiveKind[] StyleSrcFallback |
| 102 | + = new FetchDirectiveKind[] {StyleSrc, DefaultSrc}; |
| 103 | + private static final FetchDirectiveKind[] StyleSrcElemFallback |
| 104 | + = new FetchDirectiveKind[] {StyleSrcElem, StyleSrc, DefaultSrc}; |
| 105 | + private static final FetchDirectiveKind[] StyleSrcAttrFallback |
| 106 | + = new FetchDirectiveKind[] {StyleSrcAttr, StyleSrc, DefaultSrc}; |
| 107 | + private static final FetchDirectiveKind[] WorkerSrcFallback |
| 108 | + = new FetchDirectiveKind[] {WorkerSrc, ChildSrc, ScriptSrc, DefaultSrc}; |
| 109 | + private static final FetchDirectiveKind[] ConnectSrcFallback |
| 110 | + = new FetchDirectiveKind[] {ConnectSrc, DefaultSrc}; |
| 111 | + private static final FetchDirectiveKind[] ManifestSrcFallback |
| 112 | + = new FetchDirectiveKind[] {ManifestSrc, DefaultSrc}; |
| 113 | + private static final FetchDirectiveKind[] PrefetchSrcFallback |
| 114 | + = new FetchDirectiveKind[] {PrefetchSrc, DefaultSrc}; |
| 115 | + private static final FetchDirectiveKind[] ObjectSrcFallback |
| 116 | + = new FetchDirectiveKind[] {ObjectSrc, DefaultSrc}; |
| 117 | + private static final FetchDirectiveKind[] FrameSrcFallback |
| 118 | + = new FetchDirectiveKind[] {FrameSrc, ChildSrc, DefaultSrc }; |
| 119 | + private static final FetchDirectiveKind[] MediaSrcFallback |
| 120 | + = new FetchDirectiveKind[] {MediaSrc, DefaultSrc }; |
| 121 | + private static final FetchDirectiveKind[] FontSrcFallback |
| 122 | + = new FetchDirectiveKind[] {FontSrc, DefaultSrc }; |
| 123 | + private static final FetchDirectiveKind[] ImgSrcFallback |
| 124 | + = new FetchDirectiveKind[] {ImgSrc, DefaultSrc }; |
| 125 | + |
| 126 | + static FetchDirectiveKind[] getFetchDirectiveFallbackList(final FetchDirectiveKind directive) { |
| 127 | + switch (directive) { |
| 128 | + case ScriptSrc: |
| 129 | + return ScriptSrcFallback; |
| 130 | + case ScriptSrcElem: |
| 131 | + return ScriptSrcElemFallback; |
| 132 | + case ScriptSrcAttr: |
| 133 | + return ScriptSrcAttrFallback; |
| 134 | + case StyleSrc: |
| 135 | + return StyleSrcFallback; |
| 136 | + case StyleSrcElem: |
| 137 | + return StyleSrcElemFallback; |
| 138 | + case StyleSrcAttr: |
| 139 | + return StyleSrcAttrFallback; |
| 140 | + case WorkerSrc: |
| 141 | + return WorkerSrcFallback; |
| 142 | + case ConnectSrc: |
| 143 | + return ConnectSrcFallback; |
| 144 | + case ManifestSrc: |
| 145 | + return ManifestSrcFallback; |
| 146 | + case PrefetchSrc: |
| 147 | + return PrefetchSrcFallback; |
| 148 | + case ObjectSrc: |
| 149 | + return ObjectSrcFallback; |
| 150 | + case FrameSrc: |
| 151 | + return FrameSrcFallback; |
| 152 | + case MediaSrc: |
| 153 | + return MediaSrcFallback; |
| 154 | + case FontSrc: |
| 155 | + return FontSrcFallback; |
| 156 | + case ImgSrc: |
| 157 | + return ImgSrcFallback; |
| 158 | + default: |
| 159 | + throw new IllegalArgumentException("Unknown fetch directive " + directive); |
| 160 | + } |
| 161 | + } |
| 162 | +} |
0 commit comments