| 1 | /* |
| 2 | * Copyright (C) 2018 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * 1. Redistributions of source code must retain the above copyright |
| 8 | * notice, this list of conditions and the following disclaimer. |
| 9 | * 2. Redistributions in binary form must reproduce the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer in the |
| 11 | * documentation and/or other materials provided with the distribution. |
| 12 | * |
| 13 | * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 16 | * DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY |
| 17 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 18 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 19 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 20 | * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 21 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 22 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 23 | */ |
| 24 | |
| 25 | #include "config.h" |
| 26 | #include "ReferrerPolicy.h" |
| 27 | |
| 28 | #include "HTTPParsers.h" |
| 29 | #include <wtf/Optional.h> |
| 30 | |
| 31 | namespace WebCore { |
| 32 | |
| 33 | enum class ShouldParseLegacyKeywords { No, Yes }; |
| 34 | |
| 35 | static Optional<ReferrerPolicy> parseReferrerPolicyToken(StringView policy, ShouldParseLegacyKeywords shouldParseLegacyKeywords) |
| 36 | { |
| 37 | // "never" / "default" / "always" are legacy keywords that we support and still defined in the HTML specification: |
| 38 | // https://html.spec.whatwg.org/#meta-referrer |
| 39 | if (shouldParseLegacyKeywords == ShouldParseLegacyKeywords::Yes) { |
| 40 | if (equalLettersIgnoringASCIICase(policy, "never" )) |
| 41 | return ReferrerPolicy::NoReferrer; |
| 42 | if (equalLettersIgnoringASCIICase(policy, "always" )) |
| 43 | return ReferrerPolicy::UnsafeUrl; |
| 44 | if (equalLettersIgnoringASCIICase(policy, "default" )) |
| 45 | return ReferrerPolicy::NoReferrerWhenDowngrade; |
| 46 | } |
| 47 | |
| 48 | if (equalLettersIgnoringASCIICase(policy, "no-referrer" )) |
| 49 | return ReferrerPolicy::NoReferrer; |
| 50 | if (equalLettersIgnoringASCIICase(policy, "unsafe-url" )) |
| 51 | return ReferrerPolicy::UnsafeUrl; |
| 52 | if (equalLettersIgnoringASCIICase(policy, "origin" )) |
| 53 | return ReferrerPolicy::Origin; |
| 54 | if (equalLettersIgnoringASCIICase(policy, "origin-when-cross-origin" )) |
| 55 | return ReferrerPolicy::OriginWhenCrossOrigin; |
| 56 | if (equalLettersIgnoringASCIICase(policy, "same-origin" )) |
| 57 | return ReferrerPolicy::SameOrigin; |
| 58 | if (equalLettersIgnoringASCIICase(policy, "strict-origin" )) |
| 59 | return ReferrerPolicy::StrictOrigin; |
| 60 | if (equalLettersIgnoringASCIICase(policy, "strict-origin-when-cross-origin" )) |
| 61 | return ReferrerPolicy::StrictOriginWhenCrossOrigin; |
| 62 | if (equalLettersIgnoringASCIICase(policy, "no-referrer-when-downgrade" )) |
| 63 | return ReferrerPolicy::NoReferrerWhenDowngrade; |
| 64 | if (!policy.isNull() && policy.isEmpty()) |
| 65 | return ReferrerPolicy::EmptyString; |
| 66 | |
| 67 | return WTF::nullopt; |
| 68 | } |
| 69 | |
| 70 | Optional<ReferrerPolicy> parseReferrerPolicy(StringView policyString, ReferrerPolicySource source) |
| 71 | { |
| 72 | switch (source) { |
| 73 | case ReferrerPolicySource::HTTPHeader: { |
| 74 | // Implementing https://www.w3.org/TR/2017/CR-referrer-policy-20170126/#parse-referrer-policy-from-header. |
| 75 | Optional<ReferrerPolicy> result; |
| 76 | for (auto tokenView : policyString.split(',')) { |
| 77 | auto token = parseReferrerPolicyToken(stripLeadingAndTrailingHTTPSpaces(tokenView), ShouldParseLegacyKeywords::No); |
| 78 | if (token && token.value() != ReferrerPolicy::EmptyString) |
| 79 | result = token.value(); |
| 80 | } |
| 81 | return result; |
| 82 | } |
| 83 | case ReferrerPolicySource::MetaTag: |
| 84 | return parseReferrerPolicyToken(policyString, ShouldParseLegacyKeywords::Yes); |
| 85 | case ReferrerPolicySource::ReferrerPolicyAttribute: |
| 86 | return parseReferrerPolicyToken(policyString, ShouldParseLegacyKeywords::No); |
| 87 | } |
| 88 | ASSERT_NOT_REACHED(); |
| 89 | return WTF::nullopt; |
| 90 | } |
| 91 | |
| 92 | } // namespace WebCore |
| 93 | |