<commit_before>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"

#include <set>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/trace_event.h"
#include "base/file_util.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/ui/webui/shared_resources_data_source.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/url_util.h"
#include "grit/platform_locale_settings.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_file_job.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_factory.h"

using content::BrowserThread;

namespace {

// X-WebKit-CSP is our development name for Content-Security-Policy.
// TODO(tsepez) rename when Content-security-policy is done.
// TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
// TODO(tsepez) chrome-extension: permits the ChromeVox screen reader
//     extension to function on these pages.  Remove it when the extension
//     is updated to stop injecting script into the pages.
const char kChromeURLContentSecurityPolicyHeaderBase[] =
    "X-WebKit-CSP: script-src chrome://resources "
    "chrome-extension://mndnfokpggljbaajbnioimlmbfngpief "
    "'self' 'unsafe-eval'; ";

// TODO(tsepez) The following should be replaced with a centralized table.
// See crbug.com/104631.

// If you are inserting new exemptions into this list, then you have a bug.
// It is not acceptable to disable content-security-policy on chrome:// pages
// to permit functionality excluded by the above policy, such as inline script.
// Instead, you must go back and change your WebUI page so that it is compliant
// with the policy. This typically involves ensuring that all script is
// delivered through the data manager backend.
class ChromeURLContentSecurityPolicyExceptionSet
    : public std::set<std::string> {
 public:
  ChromeURLContentSecurityPolicyExceptionSet() : std::set<std::string>() {
    // TODO(tsepez) whittle this list down to nothing.
    insert(chrome::kChromeUICloudPrintResourcesHost);
    insert(chrome::kChromeUICloudPrintSetupHost);
    insert(chrome::kChromeUIDevToolsHost);
    insert(chrome::kChromeUIDialogHost);
    insert(chrome::kChromeUIInputWindowDialogHost);
    insert(chrome::kChromeUINewTabHost);
#if defined(OS_CHROMEOS)
    insert(chrome::kChromeUIMobileSetupHost);
    insert(chrome::kChromeUIOobeHost);
    insert(chrome::kChromeUIOSCreditsHost);
    insert(chrome::kChromeUIProxySettingsHost);
    insert(chrome::kChromeUIRegisterPageHost);
    insert(chrome::kChromeUISimUnlockHost);
    insert(chrome::kChromeUISystemInfoHost);
#endif
#if defined(OS_CHROMEOS) || defined(USE_AURA)
    insert(chrome::kChromeUICollectedCookiesHost);
    insert(chrome::kChromeUIHttpAuthHost);
    insert(chrome::kChromeUITabModalConfirmDialogHost);
#endif
  }
};

// It is OK to add URLs to these maps which map specific URLs to custom CSP
// directives thereby slightly reducing the protection applied to the page.
class ChromeURLObjectSrcExceptionMap
    : public std::map<std::string, std::string> {
 public:
  ChromeURLObjectSrcExceptionMap() : std::map<std::string, std::string>() {
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIPrintHost, "object-src 'self';"));
  }
};

class ChromeURLFrameSrcExceptionMap
    : public std::map<std::string, std::string> {
 public:
  ChromeURLFrameSrcExceptionMap() : std::map<std::string, std::string>() {
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIUberHost, "frame-src chrome:;"));
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIUberFrameHost, "frame-src chrome:;"));
  }
};

base::LazyInstance<ChromeURLContentSecurityPolicyExceptionSet>
    g_chrome_url_content_security_policy_exception_set =
        LAZY_INSTANCE_INITIALIZER;

base::LazyInstance<ChromeURLObjectSrcExceptionMap>
    g_chrome_url_object_src_exception_map = LAZY_INSTANCE_INITIALIZER;

base::LazyInstance<ChromeURLFrameSrcExceptionMap>
    g_chrome_url_frame_src_exception_map = LAZY_INSTANCE_INITIALIZER;

// Determine the least-privileged content security policy header, if any,
// that is compatible with a given WebUI URL, and append it to the existing
// response headers.
void AddContentSecurityPolicyHeader(
    const GURL& url, net::HttpResponseHeaders* headers) {
  ChromeURLContentSecurityPolicyExceptionSet* exceptions =
      g_chrome_url_content_security_policy_exception_set.Pointer();

  if (exceptions->find(url.host()) == exceptions->end()) {
    std::string base = kChromeURLContentSecurityPolicyHeaderBase;

    ChromeURLObjectSrcExceptionMap* object_map =
        g_chrome_url_object_src_exception_map.Pointer();
    ChromeURLObjectSrcExceptionMap::iterator object_iter =
        object_map->find(url.host());
    base.append(object_iter == object_map->end() ?
                "object-src 'none';" : object_iter->second);

    ChromeURLFrameSrcExceptionMap* frame_map =
        g_chrome_url_frame_src_exception_map.Pointer();
    ChromeURLFrameSrcExceptionMap::iterator frame_iter =
        frame_map->find(url.host());
    base.append(frame_iter == frame_map->end() ?
                "frame-src 'none';" : frame_iter->second);

    headers->AddHeader(base);
  }
}

const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";

// It is OK to add exceptions to this set as needed.
class ChromeURLXFrameOptionsExceptionSet
    : public std::set<std::string> {
 public:
  ChromeURLXFrameOptionsExceptionSet() : std::set<std::string>() {
    insert(chrome::kChromeUIExtensionsFrameHost);
    insert(chrome::kChromeUIHelpFrameHost);
    insert(chrome::kChromeUIHistoryFrameHost);
    insert(chrome::kChromeUISettingsFrameHost);
    insert(chrome::kChromeUIUberFrameHost);
  }
};

base::LazyInstance<ChromeURLXFrameOptionsExceptionSet>
    g_chrome_url_x_frame_options_exception_set = LAZY_INSTANCE_INITIALIZER;

void AddXFrameOptionsHeader(
    const GURL& url, net::HttpResponseHeaders* headers) {
  ChromeURLXFrameOptionsExceptionSet* exceptions =<commit_msg>Add chrome://terms to X-Frame-Options exception list<commit_after>// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/ui/webui/chrome_url_data_manager_backend.h"

#include <set>

#include "base/basictypes.h"
#include "base/bind.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/debug/trace_event.h"
#include "base/file_util.h"
#include "base/lazy_instance.h"
#include "base/memory/ref_counted.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/weak_ptr.h"
#include "base/message_loop.h"
#include "base/path_service.h"
#include "base/string_util.h"
#include "chrome/browser/net/chrome_url_request_context.h"
#include "chrome/browser/ui/webui/shared_resources_data_source.h"
#include "chrome/common/chrome_paths.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/url_constants.h"
#include "content/public/browser/browser_thread.h"
#include "googleurl/src/url_util.h"
#include "grit/platform_locale_settings.h"
#include "net/base/io_buffer.h"
#include "net/base/net_errors.h"
#include "net/http/http_response_headers.h"
#include "net/url_request/url_request.h"
#include "net/url_request/url_request_context.h"
#include "net/url_request/url_request_file_job.h"
#include "net/url_request/url_request_job.h"
#include "net/url_request/url_request_job_factory.h"

using content::BrowserThread;

namespace {

// X-WebKit-CSP is our development name for Content-Security-Policy.
// TODO(tsepez) rename when Content-security-policy is done.
// TODO(tsepez) remove unsafe-eval when bidichecker_packaged.js fixed.
// TODO(tsepez) chrome-extension: permits the ChromeVox screen reader
//     extension to function on these pages.  Remove it when the extension
//     is updated to stop injecting script into the pages.
const char kChromeURLContentSecurityPolicyHeaderBase[] =
    "X-WebKit-CSP: script-src chrome://resources "
    "chrome-extension://mndnfokpggljbaajbnioimlmbfngpief "
    "'self' 'unsafe-eval'; ";

// TODO(tsepez) The following should be replaced with a centralized table.
// See crbug.com/104631.

// If you are inserting new exemptions into this list, then you have a bug.
// It is not acceptable to disable content-security-policy on chrome:// pages
// to permit functionality excluded by the above policy, such as inline script.
// Instead, you must go back and change your WebUI page so that it is compliant
// with the policy. This typically involves ensuring that all script is
// delivered through the data manager backend.
class ChromeURLContentSecurityPolicyExceptionSet
    : public std::set<std::string> {
 public:
  ChromeURLContentSecurityPolicyExceptionSet() : std::set<std::string>() {
    // TODO(tsepez) whittle this list down to nothing.
    insert(chrome::kChromeUICloudPrintResourcesHost);
    insert(chrome::kChromeUICloudPrintSetupHost);
    insert(chrome::kChromeUIDevToolsHost);
    insert(chrome::kChromeUIDialogHost);
    insert(chrome::kChromeUIInputWindowDialogHost);
    insert(chrome::kChromeUINewTabHost);
#if defined(OS_CHROMEOS)
    insert(chrome::kChromeUIMobileSetupHost);
    insert(chrome::kChromeUIOobeHost);
    insert(chrome::kChromeUIOSCreditsHost);
    insert(chrome::kChromeUIProxySettingsHost);
    insert(chrome::kChromeUIRegisterPageHost);
    insert(chrome::kChromeUISimUnlockHost);
    insert(chrome::kChromeUISystemInfoHost);
#endif
#if defined(OS_CHROMEOS) || defined(USE_AURA)
    insert(chrome::kChromeUICollectedCookiesHost);
    insert(chrome::kChromeUIHttpAuthHost);
    insert(chrome::kChromeUITabModalConfirmDialogHost);
#endif
  }
};

// It is OK to add URLs to these maps which map specific URLs to custom CSP
// directives thereby slightly reducing the protection applied to the page.
class ChromeURLObjectSrcExceptionMap
    : public std::map<std::string, std::string> {
 public:
  ChromeURLObjectSrcExceptionMap() : std::map<std::string, std::string>() {
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIPrintHost, "object-src 'self';"));
  }
};

class ChromeURLFrameSrcExceptionMap
    : public std::map<std::string, std::string> {
 public:
  ChromeURLFrameSrcExceptionMap() : std::map<std::string, std::string>() {
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIUberHost, "frame-src chrome:;"));
    insert(std::pair<std::string, std::string>(
        chrome::kChromeUIUberFrameHost, "frame-src chrome:;"));
  }
};

base::LazyInstance<ChromeURLContentSecurityPolicyExceptionSet>
    g_chrome_url_content_security_policy_exception_set =
        LAZY_INSTANCE_INITIALIZER;

base::LazyInstance<ChromeURLObjectSrcExceptionMap>
    g_chrome_url_object_src_exception_map = LAZY_INSTANCE_INITIALIZER;

base::LazyInstance<ChromeURLFrameSrcExceptionMap>
    g_chrome_url_frame_src_exception_map = LAZY_INSTANCE_INITIALIZER;

// Determine the least-privileged content security policy header, if any,
// that is compatible with a given WebUI URL, and append it to the existing
// response headers.
void AddContentSecurityPolicyHeader(
    const GURL& url, net::HttpResponseHeaders* headers) {
  ChromeURLContentSecurityPolicyExceptionSet* exceptions =
      g_chrome_url_content_security_policy_exception_set.Pointer();

  if (exceptions->find(url.host()) == exceptions->end()) {
    std::string base = kChromeURLContentSecurityPolicyHeaderBase;

    ChromeURLObjectSrcExceptionMap* object_map =
        g_chrome_url_object_src_exception_map.Pointer();
    ChromeURLObjectSrcExceptionMap::iterator object_iter =
        object_map->find(url.host());
    base.append(object_iter == object_map->end() ?
                "object-src 'none';" : object_iter->second);

    ChromeURLFrameSrcExceptionMap* frame_map =
        g_chrome_url_frame_src_exception_map.Pointer();
    ChromeURLFrameSrcExceptionMap::iterator frame_iter =
        frame_map->find(url.host());
    base.append(frame_iter == frame_map->end() ?
                "frame-src 'none';" : frame_iter->second);

    headers->AddHeader(base);
  }
}

const char kChromeURLXFrameOptionsHeader[] = "X-Frame-Options: DENY";

// It is OK to add exceptions to this set as needed.
class ChromeURLXFrameOptionsExceptionSet
    : public std::set<std::string> {
 public:
  ChromeURLXFrameOptionsExceptionSet() : std::set<std::string>() {
    insert(chrome::kChromeUIExtensionsFrameHost);
    insert(chrome::kChromeUIHelpFrameHost);
    insert(chrome::kChromeUIHistoryFrameHost);
    insert(chrome::kChromeUISettingsFrameHost);
    insert(chrome::kChromeUIUberFrameHost);
#if defined(OS_CHROMEOS)
    // chrome://terms page is embedded in iframe to chrome://oobe.
    insert(chrome::kChromeUITermsHost);
#endif
  }
};

base::LazyInstance<ChromeURLXFrameOptionsExceptionSet>
    g_chrome_url_x_frame_options_exception_set = LAZY_INSTANCE_INITIALIZER;

void AddXFrameOptionsHeader(
    const GURL& url, net::HttpResponseHeaders* headers) {
  ChromeURLXFrameOptionsExceptionSet* exceptions =
{'fraction_non_alphanumeric': 0.11158151549942595, 'fraction_numerical': 0.0014351320321469576, 'mean_word_length': 5.452314814814815, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '934277', 'n_tokens_mistral': 4422, 'n_tokens_neox': 4374, 'n_words': 893}