Sha256: a29866ac22623db58797a108e02a93a1279212f03bb1c5143813e79ce38961e3

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module BridgetownContentSecurityPolicy
  mattr_reader :policies, default: {}

  def self.configure(name, &block)
    @@policies[name.to_sym] = BridgetownContentSecurityPolicy::Policy.new(&block)
  end

  class Builder < Bridgetown::Builder
    def build
      require_relative site.in_root_dir("config", "content_security_policy.config.rb")

      unless default_policy
        # rubocop:disable Layout/LineLength
        Bridgetown.logger.error "\nDefault Content Security Policy not configured"
        Bridgetown.logger.info "Please configure a default CSP in content_security_policy.config.rb\n"
        # rubocop:enable Layout/LineLength
      end

      liquid_tag "content_security_policy" do |_attributes, tag|
        render tag.context["page"]["content_security_policy"]
      end

      helper "content_security_policy" do
        render helpers.view.page.data.content_security_policy
      end
    end

    private

    def render(policy_name = nil)
      return "" unless default_policy

      page_specific_policy_name = policy_name&.to_sym
      page_specific_policy = BridgetownContentSecurityPolicy.policies[page_specific_policy_name]

      if page_specific_policy_name && page_specific_policy.nil?
        Bridgetown.logger.warn "Unknown Content Security Policy:", page_specific_policy_name.to_s
      end

      policy = default_policy.merge(page_specific_policy)

      markup_for_policy policy
    end

    def markup_for_policy(policy)
      "<meta http-equiv=\"Content-Security-Policy\" content=\"#{policy.build}\">".html_safe
    end

    def default_policy
      BridgetownContentSecurityPolicy.policies[:default]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bridgetown-content-security-policy-2.0.0 lib/bridgetown-content-security-policy/builder.rb