csp-util 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 541626754ec321c45e1140c383c0c3848fee01ccd6d814055b72713845a8ffed
4
+ data.tar.gz: ad1604712fa929d9d753e213709b9c998be4e5416ee7442918caea86aa5ab227
5
+ SHA512:
6
+ metadata.gz: 96f5afc3ae55b1b7727cc74fcd50cf3fe60dd5e33cda1664d44446f0f64a67ecfdda342892610b2ce850f0ba1e93f384ccb417da4b1d51aa9e7df3fc7de23893
7
+ data.tar.gz: b7afbc2ca5d3a4778f62b2659edca6c2c8428f1d5aa9b23ceb3f336de31670d4cbd01b9b3f80c17519de08a3400ce59b68af10b2dcbad99ed32b360d315b4915
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ Gemfile.lock
2
+ coverage/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --format doc
3
+ --require spec_helper
data/.rubocop.yml ADDED
@@ -0,0 +1,8 @@
1
+ Style/Documentation:
2
+ Enabled: false
3
+
4
+ Metrics/BlockLength:
5
+ ExcludedMethods:
6
+ - 'describe'
7
+ - 'context'
8
+ - 'let'
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.4.3
data/.travis.yml ADDED
@@ -0,0 +1,20 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - 2.2.9
5
+ - 2.3.6
6
+ - 2.4.3
7
+ - 2.5.0
8
+
9
+ script:
10
+ - bundle exec rspec
11
+
12
+ deploy:
13
+ provider: rubygems
14
+ gem: csp-util
15
+ api_key:
16
+ secure: "2SRrvn+9nAB3kpRBIcgNAbLk2UHC2cmg9Q9pkDDdpA2D79Y8ywXv3KLOWYHK742z/iUF5Vlv1zFo5hXgGFQBjqZmNI/HkRiKC1OEToP4XMMz/M2J02fyYW7EH/bFTdwzcy+URWPSI3i8m9PE4Ab6gmqRaTY7MxhD1WJpvXgCGGCv3nCtix9VKoUT8fw9MgX8f5NWQBBJ2LdXI5vpnbhVaGfr+Ux4mfJBrOCL88EIfU601lBeIJ3yGXhFd6ElqCOo3fobqvB7NQ5p2HRBJ2wvASgV0b30WeV0OMgIJIDWhBoAvctsmHezmlNb0KMLTaRH6rKbTwJrenWTabV3t09VoGyaPcltyJ7A2I2qFoYF1neJok9hvAbM8LS4FXxOKq3skF8x0S1Qztvn13b/ZisF6u+YJjc27xBO4Oz3KpMa4+ETz+7wx2+0Y3VyYA8F1/UJVWSgWMUQGwJfYcwA9U1KC9l8lx0adFnM2gZOuDaXI9wrS9u+dOZO8fUCFVUi8uD1KO242jChD30AjM+M8R66pGT05a011EAW3y9dTmC/vrSDFUmoaGZumVXLxKeHAvjxZZfufui01xviOglSijME2SXUEMxICuI7S0GUQ9lza6VHabVZfRjOFEoZD792wxj8DXJ62nMH4PofD8lz+qfroIl34V/yiDswPuPY2/YTcOs="
17
+ on:
18
+ tags: true
19
+ repo: templarbit/ruby-csp-util
20
+ ruby: "2.4.3"
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2018 Templarbit
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ # ruby-csp-util
2
+
3
+ [![Build Status](https://travis-ci.org/templarbit/ruby-csp-util.svg?branch=master)](https://travis-ci.org/templarbit/ruby-csp-util)
4
+
5
+ Content-Security-Policy utils, i.e. CSP parser in compliance with the W3C
6
+ [CSP Level 2](https://www.w3.org/TR/CSP2/)
7
+ and [CSP Level 3](https://www.w3.org/TR/CSP3/) specs.
8
+
9
+ **ABNF**
10
+ see https://www.w3.org/TR/CSP2/#policy-syntax
11
+ and https://www.w3.org/TR/CSP3/#framework
12
+
13
+ ```
14
+ serialized-policy = serialized-directive *( OWS ";" [ OWS serialized-directive ] )
15
+ serialized-directive = directive-name [ RWS directive-value ]
16
+ directive-name = 1*( ALPHA / DIGIT / "-" )
17
+ directive-value = *( %x09 / %x20-%x2B / %x2D-%x3A / %x3C-%7E )
18
+ ; Directive values may contain whitespace and VCHAR characters,
19
+ ; excluding ";" and ","
20
+ ```
21
+
22
+
23
+ ## Installation
24
+
25
+ Add `gem 'csp-util'` to your `Gemfile`.
26
+
27
+ ## Usage
28
+
29
+ ```ruby
30
+ require 'csp_util'
31
+
32
+ directives = CSPUtil.parse_directives("default-src 'self'; script-src 'self'; object-src 'self'; base-uri 'none'; report-uri https://logs.templarbit.com/csp/xxkey/reports")
33
+ ```
34
+
35
+ ## Other languages
36
+
37
+ * [Go](https://github.com/templarbit/go-csp-util)
38
+ * [Javascript](https://github.com/templarbit/javascript-csp-util)
39
+
40
+ ## Docs
41
+
42
+ * Chromium Content Security Policy implementation
43
+ https://cs.chromium.org/chromium/src/content/common/content_security_policy/?type=cs&sq=package:chromium
data/csp-util.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'lib/csp_util/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'csp-util'
7
+ s.licenses = ['MIT']
8
+ s.version = CSPUtil::VERSION
9
+ s.summary = 'Content-Security-Policy utils'
10
+ s.authors = ['Templarbit']
11
+ s.homepage = 'https://github.com/templarbit/ruby-csp-util'
12
+ s.files = `git ls-files -z`.split("\x0")
13
+ .reject do |f|
14
+ f.match(%r{^(test|spec|features)/})
15
+ end
16
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ s.required_ruby_version = '~> 2.2'
18
+
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_development_dependency 'pry'
22
+ s.add_development_dependency 'rspec', '~> 3.7'
23
+ s.add_development_dependency 'rubocop'
24
+ s.add_development_dependency 'simplecov'
25
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CSPUtil
4
+ class Directive
5
+ # More info:
6
+ # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy
7
+ VALID_NAMES =
8
+ %w[
9
+ child-src connect-src default-src font-src frame-src img-src
10
+ manifest-src media-src object-src script-src style-src worker-src
11
+ base-uri plugin-types sandbox disown-opener form-action frame-ancestors
12
+ report-uri report-to upgrade-insecure-requests block-all-mixed-content
13
+ require-sri-for
14
+ ].freeze
15
+ DEPRECATED_NAMES = %w[reflected-xss referrer].freeze
16
+ FULLY_DEPRECATED_NAMES = %w[policy-uri].freeze
17
+
18
+ attr_reader :name, :value
19
+
20
+ def initialize(token)
21
+ dir_name, dir_value = token.split(' ', 2)
22
+ validate_name!(dir_name)
23
+
24
+ @name = dir_name
25
+ @value =
26
+ if dir_value
27
+ dir_value.split(' ').map(&:strip)
28
+ else
29
+ []
30
+ end
31
+ end
32
+
33
+ def same_name?(another_directive)
34
+ return false unless name
35
+
36
+ name.casecmp(another_directive.name).zero?
37
+ end
38
+
39
+ def to_h
40
+ { name: name, value: value }
41
+ end
42
+
43
+ private
44
+
45
+ def validate_name!(directive_name)
46
+ return unless directive_name
47
+
48
+ name = directive_name.downcase
49
+ return if VALID_NAMES.include?(name)
50
+ # Show warning for deprecated names?
51
+ return if DEPRECATED_NAMES.include?(name)
52
+
53
+ if FULLY_DEPRECATED_NAMES.include?(name)
54
+ raise(DirectiveNameDeprecated, directive_name)
55
+ end
56
+
57
+ raise(DirectiveNameUnknown, directive_name)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CSPUtil
4
+ class CSPUtilError < StandardError; end
5
+
6
+ class DirectiveError < CSPUtilError
7
+ def initialize(directive_name)
8
+ @directive_name = directive_name
9
+ end
10
+ end
11
+
12
+ class DirectiveNameUnknown < DirectiveError
13
+ def message
14
+ "#{@directive_name} has been removed and is not supported"
15
+ end
16
+ end
17
+
18
+ class DirectiveNameDeprecated < DirectiveError
19
+ def message
20
+ "directive name #{@directive_name} is unknown"
21
+ end
22
+ end
23
+
24
+ class DuplicateDirective < DirectiveError
25
+ def message
26
+ "directive #{@directive_name} is a duplicate"
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CSPUtil
4
+ class << self
5
+ def parse_directives(serialized_policy)
6
+ tokens = serialized_policy.split(';')
7
+
8
+ tokens.each_with_object([]) do |token, directives|
9
+ token.strip!
10
+ next if token.empty?
11
+
12
+ directive = Directive.new(token)
13
+ validate_uniqueness!(directive, directives)
14
+
15
+ directives << directive
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def validate_uniqueness!(directive, directives)
22
+ return if directives.none? { |d| d.same_name?(directive) }
23
+
24
+ raise(DuplicateDirective, directive.name)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module CSPUtil
4
+ VERSION = '1.0.2'.freeze
5
+ end
data/lib/csp_util.rb ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'csp_util/errors'
4
+ require_relative 'csp_util/directive'
5
+ require_relative 'csp_util/parse_directives'
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csp-util
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Templarbit
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-01-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pry
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '3.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rubocop
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: simplecov
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description:
70
+ email:
71
+ executables: []
72
+ extensions: []
73
+ extra_rdoc_files: []
74
+ files:
75
+ - ".gitignore"
76
+ - ".rspec"
77
+ - ".rubocop.yml"
78
+ - ".ruby-version"
79
+ - ".travis.yml"
80
+ - Gemfile
81
+ - LICENSE
82
+ - README.md
83
+ - csp-util.gemspec
84
+ - lib/csp_util.rb
85
+ - lib/csp_util/directive.rb
86
+ - lib/csp_util/errors.rb
87
+ - lib/csp_util/parse_directives.rb
88
+ - lib/csp_util/version.rb
89
+ homepage: https://github.com/templarbit/ruby-csp-util
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - "~>"
100
+ - !ruby/object:Gem::Version
101
+ version: '2.2'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.7.4
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Content-Security-Policy utils
113
+ test_files: []