|
| 1 | +namespace OwaspHeaders.Core.Models |
| 2 | +{ |
| 3 | + /// <summary> |
| 4 | + /// Cross-Origin-Resource-Policy |
| 5 | + /// This response header(also named CORP) allows to define a policy that lets web sites and applications opt in to protection |
| 6 | + /// against certain requests from other origins(such as those issued with elements like<script> and <img>), to mitigate speculative |
| 7 | + /// side-channel attacks, like Spectre, as well as Cross-Site Script Inclusion(XSSI) attacks(source Mozilla MDN). |
| 8 | + /// </summary> |
| 9 | + public class CrossOriginResourcePolicy : IConfigurationBase |
| 10 | + { |
| 11 | + /// <summary> |
| 12 | + /// Only requests from the same Origin (i.e. scheme + host + port) can read the resource. |
| 13 | + /// </summary> |
| 14 | + public const string SameOriginValue = "same-origin"; |
| 15 | + /// <summary> |
| 16 | + /// Only requests from the same Site can read the resource. |
| 17 | + /// </summary> |
| 18 | + public const string SameSiteValue = "same-site"; |
| 19 | + /// <summary> |
| 20 | + /// Requests from any Origin (both same-site and cross-site) can read the resource. |
| 21 | + /// Browsers are using this policy when an CORP header is not specified. |
| 22 | + /// </summary> |
| 23 | + public const string CrossOriginValue = "cross-origin"; |
| 24 | + |
| 25 | + public enum CrossOriginResourceOptions |
| 26 | + { |
| 27 | + /// <summary> |
| 28 | + /// <see cref="SameOriginValue"/> |
| 29 | + /// </summary> |
| 30 | + SameOrigin, |
| 31 | + /// <summary> |
| 32 | + /// <see cref="SameSiteValue"/> |
| 33 | + /// </summary> |
| 34 | + SameSite, |
| 35 | + /// <summary> |
| 36 | + /// <see cref="CrossOriginValue"/> |
| 37 | + /// </summary> |
| 38 | + CrossOrigin |
| 39 | + }; |
| 40 | + |
| 41 | + public CrossOriginResourceOptions OptionValue { get; set; } |
| 42 | + |
| 43 | + public CrossOriginResourcePolicy(CrossOriginResourceOptions value = CrossOriginResourceOptions.SameOrigin) |
| 44 | + { |
| 45 | + OptionValue = value; |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Builds the HTTP header value |
| 50 | + /// </summary> |
| 51 | + /// <returns>A string representing the HTTP header value</returns> |
| 52 | + public string BuildHeaderValue() |
| 53 | + { |
| 54 | + switch (OptionValue) |
| 55 | + { |
| 56 | + case CrossOriginResourceOptions.CrossOrigin: |
| 57 | + return CrossOriginValue; |
| 58 | + case CrossOriginResourceOptions.SameSite: |
| 59 | + return SameSiteValue; |
| 60 | + case CrossOriginResourceOptions.SameOrigin: |
| 61 | + default: |
| 62 | + return SameOriginValue; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + } |
| 67 | +} |
0 commit comments