private bool AnyValues() { return(BaseUri.Any() || DefaultSrc.Any() || ScriptSrc.Any() || ObjectSrc.Any() || StyleSrc.Any() || ImgSrc.Any() || MediaSrc.Any() || FrameSrc.Any() || ChildSrc.Any() || FrameAncestors.Any() || FontSrc.Any() || ConnectSrc.Any() || ManifestSrc.Any() || FormAction.Any()); }
public (string headerName, string headerValue) ToString(ICspNonceService nonceService) { string headerName; if (ReportOnly) { headerName = "Content-Security-Policy-Report-Only"; } else { headerName = "Content-Security-Policy"; } var values = new List <string> { Default.ToString(nonceService), Script.ToString(nonceService), Style.ToString(nonceService), #pragma warning disable CS0618 // Type or member is obsolete Child.ToString(nonceService), #pragma warning restore CS0618 // Type or member is obsolete Connect.ToString(nonceService), Manifest.ToString(nonceService), Font.ToString(nonceService), FormAction.ToString(nonceService), Img.ToString(nonceService), Media.ToString(nonceService), Object.ToString(nonceService), FrameAncestors.ToString(), PluginTypes.ToString(), Frame.ToString(nonceService), Worker.ToString(nonceService), Prefetch.ToString(nonceService), BaseUri.ToString(nonceService), RequireSri.ToString() }; if (BlockAllMixedContent) { values.Insert(0, "block-all-mixed-content"); } if (UpgradeInsecureRequests) { values.Insert(0, "upgrade-insecure-requests"); } if (EnableSandbox) { values.Add(Sandbox.ToString()); } if (ReportUri != null) { values.Add("report-uri " + ReportUri); } string headerValue = string.Join(";", values.Where(s => s.Length > 0)); return(headerName, headerValue); }
public Tuple <string, string> ToString(ICspNonceService nonceService) { string headerName; if (ReportOnly) { headerName = "Content-Security-Policy-Report-Only"; } else { headerName = "Content-Security-Policy"; } ICollection <string> values = new List <string> { DefaultSrc.ToString(nonceService), ScriptSrc.ToString(nonceService), StyleSrc.ToString(nonceService), ChildSrc.ToString(nonceService), ConnectSrc.ToString(nonceService), FontSrc.ToString(nonceService), FormAction.ToString(nonceService), ImgSrc.ToString(nonceService), MediaSrc.ToString(nonceService), ObjectSrc.ToString(nonceService), FrameAncestors.ToString(), PluginTypes.ToString() }; if (EnableSandbox) { values.Add(Sandbox.ToString()); } if (ReportUri != null) { values.Add("report-uri " + ReportUri); } string headerValue = string.Join(";", values.Where(s => s.Length > 0)); return(new Tuple <string, string>(headerName, headerValue)); }
internal CspOptions Build() { List <string> directives = new List <string>(); string connectSourcesString = ConnectSources.Build(); if (!string.IsNullOrEmpty(connectSourcesString)) { directives.Add($"connect-src {connectSourcesString}"); } string defaultResourcesString = DefaultSources.Build(); if (!string.IsNullOrEmpty(defaultResourcesString)) { directives.Add($"default-src {defaultResourcesString}"); } string fontSourcesString = FontSources.Build(); if (!string.IsNullOrEmpty(fontSourcesString)) { directives.Add($"font-src {fontSourcesString}"); } string frameSourcesString = FrameSources.Build(); if (!string.IsNullOrEmpty(frameSourcesString)) { directives.Add($"frame-src {frameSourcesString}"); } string imgSourcesString = ImgSources.Build(); if (!string.IsNullOrEmpty(imgSourcesString)) { directives.Add($"img-src {imgSourcesString}"); } string manifestSourcesString = ManifestSources.Build(); if (!string.IsNullOrEmpty(manifestSourcesString)) { directives.Add($"manifest-src {manifestSourcesString}"); } string mediaSourcesString = MediaSources.Build(); if (!string.IsNullOrEmpty(mediaSourcesString)) { directives.Add($"media-src {mediaSourcesString}"); } string objectSourcesString = ObjectSources.Build(); if (!string.IsNullOrEmpty(objectSourcesString)) { directives.Add($"object-src {objectSourcesString}"); } string prefetchSourcesString = PrefetchSources.Build(); if (!string.IsNullOrEmpty(prefetchSourcesString)) { directives.Add($"prefetch-src {prefetchSourcesString}"); } string scriptSourcesString = ScriptSources.Build(); if (!string.IsNullOrEmpty(scriptSourcesString)) { directives.Add($"script-src {scriptSourcesString}"); } string styleSourcesString = StyleSources.Build(); if (!string.IsNullOrEmpty(styleSourcesString)) { directives.Add($"style-src {styleSourcesString}"); } string webrtcSourcesString = WebRtcSources.Build(); if (!string.IsNullOrEmpty(webrtcSourcesString)) { directives.Add($"webrtc-src {webrtcSourcesString}"); } string workerSourcesString = WorkerSources.Build(); if (!string.IsNullOrEmpty(workerSourcesString)) { directives.Add($"worker-src {workerSourcesString}"); } string baseUriString = BaseUri.Build(); if (!string.IsNullOrEmpty(baseUriString)) { directives.Add($"base-uri {baseUriString}"); } string pluginTypesString = PluginTypes.Build(); if (!string.IsNullOrEmpty(pluginTypesString)) { directives.Add($"plugin-types {pluginTypesString}"); } string sanboxOptionsString = Sandbox.Build(); if (!string.IsNullOrEmpty(sanboxOptionsString)) { directives.Add($"sandbox {sanboxOptionsString}"); } string formActionString = FormAction.Build(); if (!string.IsNullOrEmpty(formActionString)) { directives.Add($"form-action {formActionString}"); } string frameAncestors = FrameAncestors.Build(); if (!string.IsNullOrEmpty(frameAncestors)) { directives.Add($"frame-ancestors {frameAncestors}"); } if (upgrateInsecureRequests) { directives.Add("upgrade-insecure-requests"); } if (blockAllMixedContent) { directives.Add("block-all-mixed-content"); } string requireSriForString = RequireSriFor.Build(); if (!string.IsNullOrEmpty(requireSriForString)) { directives.Add($"require-sri-for {requireSriForString}"); } if (reportGroup != null) { directives.Add($"report-to {reportGroup.Group}"); } CspOptions options = new CspOptions { Content = string.Join("; ", directives), ReportingGroup = reportGroup }; return(options); }