csp_builder 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8495dd0155b2432348fbdcfc120b023c6f2da4bc
4
+ data.tar.gz: 380b90a23551c232282fcfaa6981fcabab582990
5
+ SHA512:
6
+ metadata.gz: 2d63e8e4175bd1d244eab1c29827ee23436717ac3a55d6e04c3b0d19bdcbb664a9b8a55f7e935762fc656c41fb9b42bfd1b501d69157c108395e1bf0571a38a6
7
+ data.tar.gz: 629d5859c8a214378fa7e4c41c3d9ddd468ad6bdda93e1fc7008cc3973ebf01964ebf8884c8d22cdafe4ad1983f770a2be1e09b2acea6cbdb97cadea310bea17
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ cache: bundler
3
+ install: bundle install --jobs=3 --retry=3
4
+ rvm:
5
+ - "2.3.3"
6
+ - "2.4.0"
7
+ script:
8
+ - bundle exec rake test
@@ -0,0 +1,49 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, and in the interest of
4
+ fostering an open and welcoming community, we pledge to respect all people who
5
+ contribute through reporting issues, posting feature requests, updating
6
+ documentation, submitting pull requests or patches, and other activities.
7
+
8
+ We are committed to making participation in this project a harassment-free
9
+ experience for everyone, regardless of level of experience, gender, gender
10
+ identity and expression, sexual orientation, disability, personal appearance,
11
+ body size, race, ethnicity, age, religion, or nationality.
12
+
13
+ Examples of unacceptable behavior by participants include:
14
+
15
+ * The use of sexualized language or imagery
16
+ * Personal attacks
17
+ * Trolling or insulting/derogatory comments
18
+ * Public or private harassment
19
+ * Publishing other's private information, such as physical or electronic
20
+ addresses, without explicit permission
21
+ * Other unethical or unprofessional conduct
22
+
23
+ Project maintainers have the right and responsibility to remove, edit, or
24
+ reject comments, commits, code, wiki edits, issues, and other contributions
25
+ that are not aligned to this Code of Conduct, or to ban temporarily or
26
+ permanently any contributor for other behaviors that they deem inappropriate,
27
+ threatening, offensive, or harmful.
28
+
29
+ By adopting this Code of Conduct, project maintainers commit themselves to
30
+ fairly and consistently applying these principles to every aspect of managing
31
+ this project. Project maintainers who do not follow or enforce the Code of
32
+ Conduct may be permanently removed from the project team.
33
+
34
+ This code of conduct applies both within project spaces and in public spaces
35
+ when an individual is representing the project or its community.
36
+
37
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
38
+ reported by contacting a project maintainer at mikeycgto@gmail.com. All
39
+ complaints will be reviewed and investigated and will result in a response that
40
+ is deemed necessary and appropriate to the circumstances. Maintainers are
41
+ obligated to maintain confidentiality with regard to the reporter of an
42
+ incident.
43
+
44
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
45
+ version 1.3.0, available at
46
+ [http://contributor-covenant.org/version/1/3/0/][version]
47
+
48
+ [homepage]: http://contributor-covenant.org
49
+ [version]: http://contributor-covenant.org/version/1/3/0/
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
3
+
4
+ group :development do
5
+ gem 'guard'
6
+ gem 'guard-minitest'
7
+ end
data/Guardfile ADDED
@@ -0,0 +1,6 @@
1
+ guard :minitest do
2
+ # with Minitest::Unit
3
+ watch(%r{^test/(.*)\/?(.*)_test\.rb$})
4
+ watch(%r{^lib/(.*/)?([^/]+)\.rb$}) { |m| "test/#{m[1]}#{m[2]}_test.rb" }
5
+ watch(%r{^test/test_helper\.rb$}) { 'test' }
6
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Michael Coyne
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
13
+ all 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
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,71 @@
1
+ # CspBuilder
2
+
3
+ [![Travis Build Status](https://travis-ci.org/Sigient/csp_builder.svg)](https://travis-ci.org/Sigient/csp_builder)
4
+ [![MIT licensed](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE)
5
+
6
+ Content Security Policy string builder. Create rich Content Security
7
+ Policies headers using this easy to use builder class.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'csp_builder'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install csp_builder
24
+
25
+ ## Usage
26
+
27
+ Here is a quick example. Please see the
28
+ [docs](http://www.rubydoc.info/github/Sigient/csp_builder) for more
29
+ details!
30
+
31
+
32
+ ```ruby
33
+ csp = CspBuilder.new.
34
+ script_src("https://*.cloudfront.net", :self).
35
+ style_src("https://*.cloudfront.net").
36
+ img_src('*').
37
+ frame_ancestors(:self).
38
+ upgrade_insecure_requests
39
+
40
+ # Get the compiled CSP string:
41
+ # "script-src https://*.cloudfront.net 'self'; style-src https://*.cloudfront.net; img-src *; frame-ancestors 'self'; upgrade-insecure-requests"
42
+ csp.compile!
43
+ ```
44
+
45
+ More examples using middleware and controllers coming soon!
46
+
47
+ ## Development
48
+
49
+ After checking out the repo, run `bin/setup` to install dependencies.
50
+ Then, run `rake test` to run the tests. You can also run `bin/console`
51
+ for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bundle exec rake
54
+ install`. To release a new version, update the version number in
55
+ `version.rb`, and then run `bundle exec rake release`, which will create
56
+ a git tag for the version, push git commits and tags, and push the
57
+ `.gem` file to [rubygems.org](https://rubygems.org).
58
+
59
+ ## Contributing
60
+
61
+ Bug reports and pull requests are welcome on GitHub at
62
+ https://github.com/Sigient/csp_builder. This project is intended
63
+ to be a safe, welcoming space for collaboration, and contributors are
64
+ expected to adhere to the [Contributor
65
+ Covenant](http://contributor-covenant.org) code of conduct.
66
+
67
+
68
+ ## License
69
+
70
+ The gem is available as open source under the terms of the [MIT
71
+ License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "csp_builder"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'csp_builder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'csp_builder'
8
+ spec.version = CspBuilder::VERSION
9
+ spec.authors = ['Michael Coyne', 'Sigient']
10
+ spec.email = ['mikeycgto@gmail.com']
11
+
12
+ spec.summary = 'Content Security Policy string builder'
13
+ spec.description = 'Create rich Content Security Policies using this easy to use builder class'
14
+ spec.homepage = 'https://github.com/Sigient/csp_builder'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.14'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'minitest', '~> 5.0'
25
+ end
@@ -0,0 +1,23 @@
1
+ class CspBuilder
2
+ # Fetch directive define the locations where various
3
+ # resource types can be loaded from. These directive
4
+ # all end in with "-src"
5
+ FETCH_DIRECTIVES = %i[
6
+ child connect default font frame img
7
+ manifest media object script style worker
8
+ ].freeze
9
+
10
+ # Value directives are either document, navigation,
11
+ # reporting or other type of directives that require
12
+ # a value
13
+ VALUE_DIRECTIVES = %i[
14
+ base-uri form-action frame-ancestors
15
+ plugin-types report-uri require-sri-for
16
+ ].freeze
17
+
18
+ # Meta directives do not require a value and can be
19
+ # used in a <meta> tag in the document's <head>
20
+ META_DIRECTIVES = %i[
21
+ block-all-mixed-content upgrade-insecure-requests
22
+ ].freeze
23
+ end
@@ -0,0 +1,3 @@
1
+ class CspBuilder
2
+ VERSION = '0.1.0'.freeze
3
+ end
@@ -0,0 +1,142 @@
1
+ require 'csp_builder/version'
2
+ require 'csp_builder/constants'
3
+
4
+ # Content Security Policy builder class. This class provides a lot
5
+ # of methods for making it easier to compose Content Security Policies
6
+ # for your web applications.
7
+ #
8
+ # @example Creating a CSP string
9
+ # csp = CspBuilder.new.
10
+ # script_src("https://*.cloudfront.net", :self).
11
+ # style_src("https://*.cloudfront.net").
12
+ # img_src('*').
13
+ # frame_ancestors(:self).
14
+ # upgrade_insecure_requests
15
+ #
16
+ # # Get the compiled CSP string:
17
+ # # "script-src https://*.cloudfront.net 'self'; style-src https://*.cloudfront.net; img-src *; frame-ancestors 'self'; upgrade-insecure-requests"
18
+ # csp.compile!
19
+ class CspBuilder
20
+ # Final result string. This is set by +compile!+
21
+ attr_reader :result
22
+
23
+ # Returns a new instance of +CspBuilder+
24
+ def initialize
25
+ @directives = {}
26
+ @result = nil
27
+ end
28
+
29
+ # Returns whether the result has been compiled or not
30
+ # @return [Boolean]
31
+ def compiled?
32
+ !@result.nil?
33
+ end
34
+
35
+ # Compile Content Security Policy with all of the defined directives
36
+ # @return [String] compiled CSP string
37
+ def compile!
38
+ @directives.freeze unless @directives.frozen?
39
+
40
+ @result ||= compile.freeze
41
+ end
42
+
43
+ # @!macro [new] method_doc
44
+ # Generated method
45
+ # @param values [Array<String, Symbol>] one or more value; Symbols are wrapped in single quotes
46
+ # @return [CspBuilder] self
47
+
48
+ # @!method child_src(*values)
49
+ # @macro method_doc
50
+ # @!method connect_src(*values)
51
+ # @macro method_doc
52
+ # @!method default_src(*values)
53
+ # @macro method_doc
54
+ # @!method font_src(*values)
55
+ # @macro method_doc
56
+ # @!method frame_src(*values)
57
+ # @macro method_doc
58
+ # @!method img_src(*values)
59
+ # @macro method_doc
60
+ # @!method manifest_src(*values)
61
+ # @macro method_doc
62
+ # @!method media_src(*values)
63
+ # @macro method_doc
64
+ # @!method object_src(*values)
65
+ # @macro method_doc
66
+ # @!method script_src(*values)
67
+ # @macro method_doc
68
+ # @!method style_src(*values)
69
+ # @macro method_doc
70
+ # @!method worker_src(*values)
71
+ # @macro method_doc
72
+ FETCH_DIRECTIVES.each do |type|
73
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
74
+ def #{type}_src(*values)
75
+ values.each { |value| set_directive! :'#{type}-src', value }
76
+
77
+ self
78
+ end
79
+ RUBY_EVAL
80
+ end
81
+
82
+ # @!method base_uri(*values)
83
+ # @macro method_doc
84
+ # @!method form_action(*values)
85
+ # @macro method_doc
86
+ # @!method frame_ancestors(*values)
87
+ # @macro method_doc
88
+ # @!method plugin_types(*values)
89
+ # @macro method_doc
90
+ # @!method report_uri(*values)
91
+ # @macro method_doc
92
+ # @!method require_sri_for(*values)
93
+ # @macro method_doc
94
+ VALUE_DIRECTIVES.each do |type|
95
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
96
+ def #{type.to_s.gsub('-', '_')}(*values)
97
+ values.each { |value| set_directive! :'#{type}', value }
98
+
99
+ self
100
+ end
101
+ RUBY_EVAL
102
+ end
103
+
104
+ # @!method block_all_mixed_content(*values)
105
+ # @macro method_doc
106
+ # @!method upgrade_insecure_requests(*values)
107
+ # @macro method_doc
108
+ META_DIRECTIVES.each do |type|
109
+ class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
110
+ def #{type.to_s.gsub('-', '_')}
111
+ @directives[:'#{type}'] = true
112
+
113
+ self
114
+ end
115
+ RUBY_EVAL
116
+ end
117
+
118
+ private
119
+
120
+ # @private
121
+ def compile
122
+ @directives.map { |key, val|
123
+ META_DIRECTIVES.include?(key) ? key.to_s : "#{key} #{val}"
124
+ }.join('; ')
125
+ end
126
+
127
+ # @private
128
+ def set_directive!(key, value)
129
+ if Symbol === value
130
+ value = "'#{value}'"
131
+ else
132
+ value = value.dup
133
+ end
134
+
135
+ if @directives.has_key? key
136
+ @directives[key] << " "
137
+ @directives[key] << value
138
+ else
139
+ @directives[key] = value
140
+ end
141
+ end
142
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: csp_builder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Michael Coyne
8
+ - Sigient
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2017-01-31 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '1.14'
21
+ type: :development
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - "~>"
26
+ - !ruby/object:Gem::Version
27
+ version: '1.14'
28
+ - !ruby/object:Gem::Dependency
29
+ name: rake
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '10.0'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '10.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: minitest
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '5.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '5.0'
56
+ description: Create rich Content Security Policies using this easy to use builder
57
+ class
58
+ email:
59
+ - mikeycgto@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - ".travis.yml"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - Guardfile
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - csp_builder.gemspec
75
+ - lib/csp_builder.rb
76
+ - lib/csp_builder/constants.rb
77
+ - lib/csp_builder/version.rb
78
+ homepage: https://github.com/Sigient/csp_builder
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.6
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Content Security Policy string builder
102
+ test_files: []