15c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)/* 25c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Copyright (C) 2009 Google Inc. All rights reserved. 35c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 45c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * Redistribution and use in source and binary forms, with or without 55c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * modification, are permitted provided that the following conditions are 65c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * met: 75c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 85c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions of source code must retain the above copyright 95c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * notice, this list of conditions and the following disclaimer. 105c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Redistributions in binary form must reproduce the above 115c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * copyright notice, this list of conditions and the following disclaimer 125c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * in the documentation and/or other materials provided with the 135c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * distribution. 145c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * * Neither the name of Google Inc. nor the names of its 155c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * contributors may be used to endorse or promote products derived from 165c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * this software without specific prior written permission. 175c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * 185c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 195c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 205c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 215c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 225c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 235c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 245c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 255c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 265c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 275c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 285c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 295c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) */ 305c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 315c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)#include "config.h" 3251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/weborigin/OriginAccessEntry.h" 335c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 3451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "platform/weborigin/SecurityOrigin.h" 3551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "public/platform/Platform.h" 3651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)#include "public/platform/WebPublicSuffixList.h" 375c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 38c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)namespace blink { 3981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 4019cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles)OriginAccessEntry::OriginAccessEntry(const String& protocol, const String& host, SubdomainSetting subdomainSetting, IPAddressSetting ipAddressSetting) 415c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) : m_protocol(protocol.lower()) 425c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) , m_host(host.lower()) 435c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) , m_subdomainSettings(subdomainSetting) 4419cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles) , m_ipAddressSettings(ipAddressSetting) 4551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) , m_hostIsPublicSuffix(false) 465c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 475c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(subdomainSetting == AllowSubdomains || subdomainSetting == DisallowSubdomains); 485c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 495c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Assume that any host that ends with a digit is trying to be an IP address. 505c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) m_hostIsIPAddress = !m_host.isEmpty() && isASCIIDigit(m_host[m_host.length() - 1]); 5151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) 5251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) // Look for top-level domains, either with or without an additional dot. 5351b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) if (!m_hostIsIPAddress) { 5451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) blink::WebPublicSuffixList* suffixList = blink::Platform::current()->publicSuffixList(); 5551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) if (suffixList && m_host.length() <= suffixList->getPublicSuffixLength(m_host) + 1) 5651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) m_hostIsPublicSuffix = true; 5751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) } 585c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 595c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 6051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles)OriginAccessEntry::MatchResult OriginAccessEntry::matchesOrigin(const SecurityOrigin& origin) const 615c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles){ 625c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(origin.host() == origin.host().lower()); 635c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) ASSERT(origin.protocol() == origin.protocol().lower()); 645c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) 655c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (m_protocol != origin.protocol()) 6651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return DoesNotMatchOrigin; 6781a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 685c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Special case: Include subdomains and empty host means "all hosts, including ip addresses". 695c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (m_subdomainSettings == AllowSubdomains && m_host.isEmpty()) 7051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return MatchesOrigin; 7181a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 725c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Exact match. 735c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (m_host == origin.host()) 7451b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return MatchesOrigin; 7581a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 765c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Otherwise we can only match if we're matching subdomains. 775c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) if (m_subdomainSettings == DisallowSubdomains) 7851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return DoesNotMatchOrigin; 7981a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 8051b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) // Don't try to do subdomain matching on IP addresses (except for testing). 8119cde67944066db31e633d9e386f2aa9bf9fadb3Torne (Richard Coles) if (m_hostIsIPAddress && m_ipAddressSettings == TreatIPAddressAsIPAddress) 8251b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return DoesNotMatchOrigin; 8381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 845c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles) // Match subdomains. 8551b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) if (origin.host().length() <= m_host.length() || origin.host()[origin.host().length() - m_host.length() - 1] != '.' || !origin.host().endsWith(m_host)) 8651b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return DoesNotMatchOrigin; 8751b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) 8851b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) if (m_hostIsPublicSuffix) 8951b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return MatchesOriginButIsPublicSuffix; 9081a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 9151b2906e11752df6c18351cf520e30522d3b53a1Torne (Richard Coles) return MatchesOrigin; 925c87bf8b86a7c82ef50fb7a89697d8e02e2553beTorne (Richard Coles)} 9381a5157921f1d2a7ff6aae115bfe3c139b38a5c8Torne (Richard Coles) 94c1847b1379d12d0e05df27436bf19a9b1bf12deaTorne (Richard Coles)} // namespace blink 95