示例#1
0
void DocumentLoader::responseReceived(Resource* resource, const ResourceResponse& response)
{
    ASSERT_UNUSED(resource, m_mainResource == resource);
    RefPtr<DocumentLoader> protect(this);

    m_applicationCacheHost->didReceiveResponseForMainResource(response);

    // The memory cache doesn't understand the application cache or its caching rules. So if a main resource is served
    // from the application cache, ensure we don't save the result for future use. All responses loaded
    // from appcache will have a non-zero appCacheID().
    if (response.appCacheID())
        memoryCache()->remove(m_mainResource.get());

    DEFINE_STATIC_LOCAL(AtomicString, xFrameOptionHeader, ("x-frame-options", AtomicString::ConstructFromLiteral));
    HTTPHeaderMap::const_iterator it = response.httpHeaderFields().find(xFrameOptionHeader);
    if (it != response.httpHeaderFields().end()) {
        String content = it->value;
        ASSERT(m_mainResource);
        unsigned long identifier = mainResourceIdentifier();
        ASSERT(identifier);
        if (frameLoader()->shouldInterruptLoadForXFrameOptions(content, response.url(), identifier)) {
            InspectorInstrumentation::continueAfterXFrameOptionsDenied(m_frame, this, identifier, response);
            String message = "Refused to display '" + response.url().elidedString() + "' in a frame because it set 'X-Frame-Options' to '" + content + "'.";
            frame()->document()->addConsoleMessageWithRequestIdentifier(SecurityMessageSource, ErrorMessageLevel, message, identifier);
            frame()->document()->enforceSandboxFlags(SandboxOrigin);
            if (HTMLFrameOwnerElement* ownerElement = frame()->ownerElement())
                ownerElement->dispatchEvent(Event::create(EventTypeNames::load));

            // The load event might have detached this frame. In that case, the load will already have been cancelled during detach.
            if (frameLoader())
                cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
            return;
        }
    }

    ASSERT(!mainResourceLoader() || !mainResourceLoader()->defersLoading());

    m_response = response;

    if (isArchiveMIMEType(m_response.mimeType()) && m_mainResource->dataBufferingPolicy() != BufferData)
        m_mainResource->setDataBufferingPolicy(BufferData);

    if (!shouldContinueForResponse()) {
        InspectorInstrumentation::continueWithPolicyIgnore(m_frame, this, m_mainResource->identifier(), m_response);
        cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
        return;
    }

    if (m_response.isHTTP()) {
        int status = m_response.httpStatusCode();
        if ((status < 200 || status >= 300) && m_frame->ownerElement() && m_frame->ownerElement()->isObjectElement()) {
            m_frame->ownerElement()->renderFallbackContent();
            // object elements are no longer rendered after we fallback, so don't
            // keep trying to process data from their load
            cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
        }
    }
}
示例#2
0
void DocumentLoader::cancelLoadAfterXFrameOptionsOrCSPDenied(const ResourceResponse& response)
{
    InspectorInstrumentation::continueAfterXFrameOptionsDenied(m_frame, this, mainResourceIdentifier(), response);

    frame()->document()->enforceSandboxFlags(SandboxOrigin);
    if (FrameOwner* owner = frame()->owner())
        owner->dispatchLoad();

    // The load event might have detached this frame. In that case, the load will already have been cancelled during detach.
    if (frameLoader())
        cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
    return;
}
void DocumentLoader::cancelLoadAfterXFrameOptionsOrCSPDenied(const ResourceResponse& response)
{
    InspectorInstrumentation::continueAfterXFrameOptionsDenied(m_frame, this, mainResourceIdentifier(), response, m_mainResource.get());

    setWasBlockedAfterXFrameOptionsOrCSP();

    // Pretend that this was an empty HTTP 200 response.  Don't reuse the
    // original URL for the empty page (https://crbug.com/622385).
    //
    // TODO(mkwst):  Remove this once XFO moves to the browser.
    // https://crbug.com/555418.
    clearMainResourceHandle();
    KURL blockedURL = SecurityOrigin::urlWithUniqueSecurityOrigin();
    m_originalRequest.setURL(blockedURL);
    m_request.setURL(blockedURL);
    m_redirectChain.removeLast();
    appendRedirect(blockedURL);
    m_response = ResourceResponse(blockedURL, "text/html", 0, nullAtom, String());
    finishedLoading(monotonicallyIncreasingTime());

    return;
}
示例#4
0
void DocumentLoader::responseReceived(Resource* resource, const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle> handle)
{
    ASSERT_UNUSED(resource, m_mainResource == resource);
    ASSERT_UNUSED(handle, !handle);
    RefPtr<DocumentLoader> protect(this);

    m_applicationCacheHost->didReceiveResponseForMainResource(response);

    // The memory cache doesn't understand the application cache or its caching rules. So if a main resource is served
    // from the application cache, ensure we don't save the result for future use. All responses loaded
    // from appcache will have a non-zero appCacheID().
    if (response.appCacheID())
        memoryCache()->remove(m_mainResource.get());

    DEFINE_STATIC_LOCAL(AtomicString, xFrameOptionHeader, ("x-frame-options", AtomicString::ConstructFromLiteral));
    HTTPHeaderMap::const_iterator it = response.httpHeaderFields().find(xFrameOptionHeader);
    if (it != response.httpHeaderFields().end()) {
        String content = it->value;
        if (frameLoader()->shouldInterruptLoadForXFrameOptions(content, response.url(), mainResourceIdentifier())) {
            String message = "Refused to display '" + response.url().elidedString() + "' in a frame because it set 'X-Frame-Options' to '" + content + "'.";
            RefPtrWillBeRawPtr<ConsoleMessage> consoleMessage = ConsoleMessage::create(SecurityMessageSource, ErrorMessageLevel, message);
            consoleMessage->setRequestIdentifier(mainResourceIdentifier());
            frame()->document()->addConsoleMessage(consoleMessage.release());

            cancelLoadAfterXFrameOptionsOrCSPDenied(response);
            return;
        }
    }

    m_contentSecurityPolicy = ContentSecurityPolicy::create();
    m_contentSecurityPolicy->setOverrideURLForSelf(response.url());
    m_contentSecurityPolicy->didReceiveHeaders(ContentSecurityPolicyResponseHeaders(response));
    if (!m_contentSecurityPolicy->allowAncestors(m_frame, response.url())) {
        cancelLoadAfterXFrameOptionsOrCSPDenied(response);
        return;
    }

    ASSERT(!mainResourceLoader() || !mainResourceLoader()->defersLoading());

    m_response = response;

    if (isArchiveMIMEType(m_response.mimeType()) && m_mainResource->dataBufferingPolicy() != BufferData)
        m_mainResource->setDataBufferingPolicy(BufferData);

    if (!shouldContinueForResponse()) {
        InspectorInstrumentation::continueWithPolicyIgnore(m_frame, this, m_mainResource->identifier(), m_response);
        cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
        return;
    }

    if (m_response.isHTTP()) {
        int status = m_response.httpStatusCode();
        // FIXME: Fallback content only works if the parent is in the same processs.
        if ((status < 200 || status >= 300) && m_frame->owner()) {
            if (!m_frame->deprecatedLocalOwner()) {
                ASSERT_NOT_REACHED();
            } else if (m_frame->deprecatedLocalOwner()->isObjectElement()) {
                m_frame->deprecatedLocalOwner()->renderFallbackContent();
                // object elements are no longer rendered after we fallback, so don't
                // keep trying to process data from their load
                cancelMainResourceLoad(ResourceError::cancelledError(m_request.url()));
            }
        }
    }
}
示例#5
0
void DocumentLoader::responseReceived(
    Resource* resource,
    const ResourceResponse& response,
    std::unique_ptr<WebDataConsumerHandle> handle) {
  DCHECK_EQ(m_mainResource, resource);
  DCHECK(!handle);
  DCHECK(frame());

  m_applicationCacheHost->didReceiveResponseForMainResource(response);

  // The memory cache doesn't understand the application cache or its caching
  // rules. So if a main resource is served from the application cache, ensure
  // we don't save the result for future use. All responses loaded from appcache
  // will have a non-zero appCacheID().
  if (response.appCacheID())
    memoryCache()->remove(m_mainResource.get());

  m_contentSecurityPolicy = ContentSecurityPolicy::create();
  m_contentSecurityPolicy->setOverrideURLForSelf(response.url());
  m_contentSecurityPolicy->didReceiveHeaders(
      ContentSecurityPolicyResponseHeaders(response));
  if (!m_contentSecurityPolicy->allowAncestors(m_frame, response.url())) {
    cancelLoadAfterXFrameOptionsOrCSPDenied(response);
    return;
  }

  // 'frame-ancestors' obviates 'x-frame-options':
  // https://w3c.github.io/webappsec/specs/content-security-policy/#frame-ancestors-and-frame-options
  if (!m_contentSecurityPolicy->isFrameAncestorsEnforced()) {
    HTTPHeaderMap::const_iterator it =
        response.httpHeaderFields().find(HTTPNames::X_Frame_Options);
    if (it != response.httpHeaderFields().end()) {
      String content = it->value;
      if (frameLoader()->shouldInterruptLoadForXFrameOptions(
              content, response.url(), mainResourceIdentifier())) {
        String message = "Refused to display '" +
                         response.url().elidedString() +
                         "' in a frame because it set 'X-Frame-Options' to '" +
                         content + "'.";
        ConsoleMessage* consoleMessage = ConsoleMessage::createForRequest(
            SecurityMessageSource, ErrorMessageLevel, message, response.url(),
            mainResourceIdentifier());
        frame()->document()->addConsoleMessage(consoleMessage);

        cancelLoadAfterXFrameOptionsOrCSPDenied(response);
        return;
      }
    }
  }

  if (RuntimeEnabledFeatures::embedderCSPEnforcementEnabled() &&
      !frameLoader()->requiredCSP().isEmpty()) {
    SecurityOrigin* parentSecurityOrigin =
        frame()->tree().parent()->securityContext()->getSecurityOrigin();
    if (ContentSecurityPolicy::shouldEnforceEmbeddersPolicy(
            response, parentSecurityOrigin)) {
      m_contentSecurityPolicy->addPolicyFromHeaderValue(
          frameLoader()->requiredCSP(), ContentSecurityPolicyHeaderTypeEnforce,
          ContentSecurityPolicyHeaderSourceHTTP);
    } else {
      String message = "Refused to display '" + response.url().elidedString() +
                       "' because it has not opted-into the following policy "
                       "required by its embedder: '" +
                       frameLoader()->requiredCSP() + "'.";
      ConsoleMessage* consoleMessage = ConsoleMessage::createForRequest(
          SecurityMessageSource, ErrorMessageLevel, message, response.url(),
          mainResourceIdentifier());
      frame()->document()->addConsoleMessage(consoleMessage);
      cancelLoadAfterXFrameOptionsOrCSPDenied(response);
      return;
    }
  }

  DCHECK(!m_frame->page()->defersLoading());

  m_response = response;

  if (isArchiveMIMEType(m_response.mimeType()) &&
      m_mainResource->getDataBufferingPolicy() != BufferData)
    m_mainResource->setDataBufferingPolicy(BufferData);

  if (!shouldContinueForResponse()) {
    InspectorInstrumentation::continueWithPolicyIgnore(
        m_frame, this, m_mainResource->identifier(), m_response,
        m_mainResource.get());
    m_fetcher->stopFetching();
    return;
  }

  if (m_response.isHTTP()) {
    int status = m_response.httpStatusCode();
    if ((status < 200 || status >= 300) && m_frame->owner())
      m_frame->owner()->renderFallbackContent();
  }
}