<commit_before># frozen_string_literal: true

require 'bundler/setup'
Bundler.require :app

class App < Sinatra::Application
  Bundler.require environment
  require 'sinatra/cookies'
  require 'tilt/erubis'

  Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'

  configure do
    set :sentry_dsn, ENV['SENTRY_DSN']
    set :protection, except: [:frame_options, :xss_header]

    set :root, Pathname.new(File.expand_path('../..', __FILE__))
    set :sprockets, Sprockets::Environment.new(root)

    set :assets_prefix, 'assets'
    set :assets_path, -> { File.join(public_folder, assets_prefix) }
    set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
    set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)

    require 'yajl/json_gem'
    set :docs_prefix, 'docs'
    set :docs_host, -> { File.join('', docs_prefix) }
    set :docs_path, -> { File.join(public_folder, docs_prefix) }
    set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
    set :default_docs, %w(css dom dom_events html http javascript)
    set :docs, -> {
      Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
        doc['full_name'] = doc['name'].dup
        doc['full_name'] << " #{doc['version']}" if doc['version']
        doc['slug_without_version'] = doc['slug'].split('~').first
        [doc['slug'], doc]
      }]
    }

    set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
    set :news, -> { JSON.parse(File.read(news_path)) }

    set :csp, false

    Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
      sprockets.append_path(path)
    end

    Sprockets::Helpers.configure do |config|
      config.environment = sprockets
      config.prefix = "/#{assets_prefix}"
      config.public_path = public_folder
      config.protocol = :relative
    end
  end

  configure :test, :development do
    require 'active_support/per_thread_registry'
    require 'active_support/cache'
    sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
  end

  configure :development do
    register Sinatra::Reloader

    use BetterErrors::Middleware
    BetterErrors.application_root = File.expand_path('..', __FILE__)
    BetterErrors.editor = :sublime
  end

  configure :production do
    set :static, false
    set :docs_host, '//docs.devdocs.io'
    set :csp, "default-src 'self' *; script-src 'self' 'unsafe-inline' http://cdn.devdocs.io https://cdn.devdocs.io https://www.google-analytics.com https://secure.gaug.es http://*.jquery.com https://*.jquery.com; font-src data:; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"

    use Rack::ConditionalGet
    use Rack::ETag
    use Rack::Deflater
    use Rack::Static,
      root: 'public',
      urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp),
      header_rules: [
        [:all,           {'Cache-Control' => 'no-cache, max-age=0'}],
        ['/assets',      {'Cache-Control' => 'public, max-age=604800'}],
        ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
        ['/images',      {'Cache-Control' => 'public, max-age=86400'}] ]
<commit_msg>Enforce CSP in dev environment<commit_after># frozen_string_literal: true

require 'bundler/setup'
Bundler.require :app

class App < Sinatra::Application
  Bundler.require environment
  require 'sinatra/cookies'
  require 'tilt/erubis'

  Rack::Mime::MIME_TYPES['.webapp'] = 'application/x-web-app-manifest+json'

  configure do
    set :sentry_dsn, ENV['SENTRY_DSN']
    set :protection, except: [:frame_options, :xss_header]

    set :root, Pathname.new(File.expand_path('../..', __FILE__))
    set :sprockets, Sprockets::Environment.new(root)

    set :assets_prefix, 'assets'
    set :assets_path, -> { File.join(public_folder, assets_prefix) }
    set :assets_manifest_path, -> { File.join(assets_path, 'manifest.json') }
    set :assets_compile, %w(*.png docs.js docs.json application.js application.css application-dark.css)

    require 'yajl/json_gem'
    set :docs_prefix, 'docs'
    set :docs_host, -> { File.join('', docs_prefix) }
    set :docs_path, -> { File.join(public_folder, docs_prefix) }
    set :docs_manifest_path, -> { File.join(docs_path, 'docs.json') }
    set :default_docs, %w(css dom dom_events html http javascript)
    set :docs, -> {
      Hash[JSON.parse(File.read(docs_manifest_path)).map! { |doc|
        doc['full_name'] = doc['name'].dup
        doc['full_name'] << " #{doc['version']}" if doc['version']
        doc['slug_without_version'] = doc['slug'].split('~').first
        [doc['slug'], doc]
      }]
    }

    set :news_path, -> { File.join(root, assets_prefix, 'javascripts', 'news.json') }
    set :news, -> { JSON.parse(File.read(news_path)) }

    set :csp, false

    Dir[docs_path, root.join(assets_prefix, '*/')].each do |path|
      sprockets.append_path(path)
    end

    Sprockets::Helpers.configure do |config|
      config.environment = sprockets
      config.prefix = "/#{assets_prefix}"
      config.public_path = public_folder
      config.protocol = :relative
    end
  end

  configure :test, :development do
    require 'active_support/per_thread_registry'
    require 'active_support/cache'
    sprockets.cache = ActiveSupport::Cache.lookup_store :file_store, root.join('tmp', 'cache', 'assets', environment.to_s)
  end

  configure :development do
    register Sinatra::Reloader

    use BetterErrors::Middleware
    BetterErrors.application_root = File.expand_path('..', __FILE__)
    BetterErrors.editor = :sublime

    set :csp, "default-src 'self' *; script-src 'self' 'unsafe-inline' *; font-src data:; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"
  end

  configure :production do
    set :static, false
    set :docs_host, '//docs.devdocs.io'
    set :csp, "default-src 'self' *; script-src 'self' 'unsafe-inline' http://cdn.devdocs.io https://cdn.devdocs.io https://www.google-analytics.com https://secure.gaug.es http://*.jquery.com https://*.jquery.com; font-src data:; style-src 'self' 'unsafe-inline' *; img-src 'self' * data:;"

    use Rack::ConditionalGet
    use Rack::ETag
    use Rack::Deflater
    use Rack::Static,
      root: 'public',
      urls: %w(/assets /docs/ /images /favicon.ico /robots.txt /opensearch.xml /manifest.webapp),
      header_rules: [
        [:all,           {'Cache-Control' => 'no-cache, max-age=0'}],
        ['/assets',      {'Cache-Control' => 'public, max-age=604800'}],
        ['/favicon.ico', {'Cache-Control' => 'public, max-age=86400'}],
        ['/images',      {'Cache-Control' => 'public, max-age=86400'}] ]

{'fraction_non_alphanumeric': 0.1635201427084882, 'fraction_numerical': 0.00505425895644418, 'mean_word_length': 3.4973262032085564, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 27, 'https://': 8, 'lorem ipsum': 0, 'www.': 2, 'xml': 2}, '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': '5119148', 'n_tokens_mistral': 2364, 'n_tokens_neox': 2284, 'n_words': 457}