kill_switch 0.2.0
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 +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +5 -0
- data/LICENSE.txt +21 -0
- data/README.md +95 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/kill_switch.gemspec +27 -0
- data/lib/kill_switch.rb +11 -0
- data/lib/kill_switch/client_blacklist.rb +109 -0
- data/lib/kill_switch/user_agent.rb +35 -0
- data/lib/kill_switch/version.rb +3 -0
- data/tasks/rspec.rake +3 -0
- metadata +130 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 095f878161e04b7eba76f8e897ed241ac70e7e7c
|
4
|
+
data.tar.gz: 259d3da3659e3ac4a2d7927b1d564d9a48247dae
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4da461d8b7bd8caf84e5835942095a5701c781432b219ae9db27635c3675bdbbfee9b04f8d9097c47c2d12863e7a023f8d0934557b1f383f76ee5a1947ac5790
|
7
|
+
data.tar.gz: a1b90a8233d7c618942c488590fc2114c29fddaac5755af7cfc9960d3b3222c707df83f0061ea3c94ccb9903d83e422ae7ef4e398ff938a36b3cec1345d85e93
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 TODO: Write your name
|
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,95 @@
|
|
1
|
+
# KillSwitch
|
2
|
+
|
3
|
+
Utility for managing blacklisted User Agent Strings.
|
4
|
+
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'kill_switch'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install kill_switch
|
21
|
+
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
1. In config/initializers create a file and define some rules
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
ClientBlacklist.configure do |config|
|
29
|
+
config.blacklist "1.03.00", "iOS", "iPhone OS 6.1", "iPhone4,1"
|
30
|
+
end
|
31
|
+
|
32
|
+
Format and optional params
|
33
|
+
config.blacklist app_version, device_platform, <optional> device_os_version, <optional> device_model
|
34
|
+
Examples:
|
35
|
+
config.blacklist "1.03.00", "iOS", "iPhone OS 6.1", "iPhone4,1"
|
36
|
+
config.blacklist "1.03.00", "iOS", "iPhone OS 6.1"
|
37
|
+
config.blacklist "1.03.00", "iOS", "iPhone4,1"
|
38
|
+
config.blacklist "1.03.00", "iOS"
|
39
|
+
```
|
40
|
+
|
41
|
+
`device_os_version` and `device_model` are optional. If either are present, they will be matched
|
42
|
+
|
43
|
+
This class matches on depth first, meaning if `device_os_version` or `device_model` are present
|
44
|
+
in the rule definition, it will only match if the user agent also fully matches the criteria.
|
45
|
+
|
46
|
+
When defining rules, note presedence.
|
47
|
+
Given the user agent string 'BleachrClient/1.03.00 (iOS; iPhone OS 6.1; iPhone5,1)'
|
48
|
+
and the following rules
|
49
|
+
config.blacklist "1.03.00", "iOS", "iPhone OS 6.1", "iPhone4,1"
|
50
|
+
config.blacklist "1.03.00", "iOS"
|
51
|
+
|
52
|
+
The user agent **will not** be blacklisted because it does not match the first definition.
|
53
|
+
The first full definition overrides the more generic definition.
|
54
|
+
|
55
|
+
|
56
|
+
2. In the ApiController, add a hook to check for blacklisted user agents.
|
57
|
+
|
58
|
+
`before_action ClientBlacklist::BlacklistFilter`
|
59
|
+
|
60
|
+
|
61
|
+
3. Set-up for RSPEC tests
|
62
|
+
|
63
|
+
Add a helper method and config setting to `rails_helper.rb`.
|
64
|
+
|
65
|
+
```ruby
|
66
|
+
RSpec.configure do |config|
|
67
|
+
|
68
|
+
...
|
69
|
+
|
70
|
+
config.before(:each, type: :controller) do
|
71
|
+
set_user_agent
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_user_agent(user_agent = 'Client/0.00.01 (iOS; iPhone OS 6.1; iPhone4,1)')
|
76
|
+
request.env["HTTP_USER_AGENT"] = user_agent
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
## Development
|
81
|
+
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake rspec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
83
|
+
|
84
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
85
|
+
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/livefront/kill_switch_gem. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
90
|
+
|
91
|
+
|
92
|
+
## License
|
93
|
+
|
94
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
95
|
+
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "kill_switch"
|
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
data/kill_switch.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'kill_switch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "kill_switch"
|
8
|
+
spec.version = KillSwitch::VERSION
|
9
|
+
spec.authors = ["Rosie Hoyem"]
|
10
|
+
spec.email = ["rosiehoyem@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Utility for managing blacklisted User Agent Strings.}
|
13
|
+
spec.description = %q{Utility for managing blacklisted User Agent Strings.}
|
14
|
+
spec.homepage = "https://github.com/livefront/kill_switch_gem"
|
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.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "rspec"
|
25
|
+
spec.add_development_dependency "pry"
|
26
|
+
spec.add_development_dependency "pry-nav"
|
27
|
+
end
|
data/lib/kill_switch.rb
ADDED
@@ -0,0 +1,109 @@
|
|
1
|
+
require 'kill_switch/version'
|
2
|
+
require 'singleton'
|
3
|
+
|
4
|
+
class ClientBlacklist
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def self.configure(&block)
|
8
|
+
raise ArgumentError, "Block not provided" unless block_given?
|
9
|
+
yield instance
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.rules
|
13
|
+
instance.rules
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.clear_rules
|
17
|
+
instance.instance_variable_set(:@rules, {})
|
18
|
+
end
|
19
|
+
|
20
|
+
def rules
|
21
|
+
@rules ||= {}
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.user_agent_valid?(user_agent_string)
|
25
|
+
UserAgent.new(user_agent_string).valid?
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.agent_blacklisted?(user_agent_string)
|
29
|
+
agent = UserAgent.new(user_agent_string)
|
30
|
+
raise RuntimeError, "User Agent Invalid" unless agent.valid?
|
31
|
+
|
32
|
+
base_rules = rules.fetch("blacklist", {}).fetch(agent.app_version, {}).fetch(agent.device_platform, nil)
|
33
|
+
# no rules found for version and device
|
34
|
+
return false if base_rules.nil?
|
35
|
+
|
36
|
+
if device_os_version_rules = base_rules[agent.device_os_version]
|
37
|
+
if device_os_version_rules.empty?
|
38
|
+
# No further criteria to match on. Found final match on device_os_version
|
39
|
+
return true
|
40
|
+
end
|
41
|
+
if device_os_version_rules[agent.device_model]
|
42
|
+
# Found a match on the nested device model
|
43
|
+
return true
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
if base_rules[agent.device_model]
|
48
|
+
# 3 rules, excluding devise_os_version
|
49
|
+
return true
|
50
|
+
end
|
51
|
+
|
52
|
+
if base_rules.any? # this means we have nested rules, not just the version and platform
|
53
|
+
return false # we have nested rules, nothing matched - good to go
|
54
|
+
end
|
55
|
+
|
56
|
+
return true # If we made it this far, we matched on the base_rules only
|
57
|
+
end
|
58
|
+
|
59
|
+
def blacklist(app_version, device_platform, device_os_version = nil, device_model = nil)
|
60
|
+
rules["blacklist"] ||= {}
|
61
|
+
rules["blacklist"][app_version] ||= {}
|
62
|
+
rules["blacklist"][app_version][device_platform] ||= {}
|
63
|
+
|
64
|
+
if device_os_version
|
65
|
+
rules["blacklist"][app_version][device_platform][device_os_version] ||= {}
|
66
|
+
end
|
67
|
+
if device_model
|
68
|
+
if device_os_version
|
69
|
+
# all 4 rules included - nest under device_os_version
|
70
|
+
rules["blacklist"][app_version][device_platform][device_os_version][device_model] ||= {}
|
71
|
+
else
|
72
|
+
# device_os_version excluded - rule is to exclude a particular device_platform
|
73
|
+
rules["blacklist"][app_version][device_platform][device_model] ||= {}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
# Rails Before Filter Class Implementation
|
79
|
+
class BlacklistFilter
|
80
|
+
def self.before(controller)
|
81
|
+
user_agent = controller.request.user_agent
|
82
|
+
unless ClientBlacklist.user_agent_valid?(user_agent)
|
83
|
+
controller.render(
|
84
|
+
json: {
|
85
|
+
data: {
|
86
|
+
message: "User agent string is missing or invalid. Received [#{user_agent}]" },
|
87
|
+
metadata: nil,
|
88
|
+
},
|
89
|
+
status: 400,
|
90
|
+
)
|
91
|
+
return false
|
92
|
+
end
|
93
|
+
|
94
|
+
if ClientBlacklist.agent_blacklisted?(user_agent)
|
95
|
+
controller.render(
|
96
|
+
json: {
|
97
|
+
data: {
|
98
|
+
message: "This version [#{user_agent}] is no longer supported. Please visit the App Store to upgrade to the most recent version."
|
99
|
+
},
|
100
|
+
metadata: nil,
|
101
|
+
},
|
102
|
+
status: 403,
|
103
|
+
)
|
104
|
+
return false
|
105
|
+
end
|
106
|
+
return true # All checks passed
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class UserAgent
|
2
|
+
attr_reader :app_version
|
3
|
+
attr_reader :device_model
|
4
|
+
attr_reader :device_os_version
|
5
|
+
attr_reader :device_platform
|
6
|
+
attr_reader :string
|
7
|
+
|
8
|
+
def initialize(user_agent_string)
|
9
|
+
@string = user_agent_string
|
10
|
+
parse_string
|
11
|
+
end
|
12
|
+
|
13
|
+
def valid?
|
14
|
+
@parsed_successfully &&
|
15
|
+
!@app_version.to_s.empty? &&
|
16
|
+
!@device_model.to_s.empty? &&
|
17
|
+
!@device_os_version.to_s.empty? &&
|
18
|
+
!@device_platform.to_s.empty?
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def parse_string
|
24
|
+
if match = @string.match(/\/([^ ]+) \(([^)]+)\)/)
|
25
|
+
@app_version, rest = match.captures
|
26
|
+
rest = rest.split(';')
|
27
|
+
@device_platform = rest[0].strip
|
28
|
+
@device_os_version = rest[1].strip
|
29
|
+
@device_model = rest[2].strip
|
30
|
+
@parsed_successfully = true
|
31
|
+
end
|
32
|
+
rescue Exception
|
33
|
+
@parsed_successfully = false
|
34
|
+
end
|
35
|
+
end
|
data/tasks/rspec.rake
ADDED
metadata
ADDED
@@ -0,0 +1,130 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: kill_switch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Rosie Hoyem
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-07-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
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: pry
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry-nav
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: Utility for managing blacklisted User Agent Strings.
|
84
|
+
email:
|
85
|
+
- rosiehoyem@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- LICENSE.txt
|
96
|
+
- README.md
|
97
|
+
- Rakefile
|
98
|
+
- bin/console
|
99
|
+
- bin/setup
|
100
|
+
- kill_switch.gemspec
|
101
|
+
- lib/kill_switch.rb
|
102
|
+
- lib/kill_switch/client_blacklist.rb
|
103
|
+
- lib/kill_switch/user_agent.rb
|
104
|
+
- lib/kill_switch/version.rb
|
105
|
+
- tasks/rspec.rake
|
106
|
+
homepage: https://github.com/livefront/kill_switch_gem
|
107
|
+
licenses:
|
108
|
+
- MIT
|
109
|
+
metadata: {}
|
110
|
+
post_install_message:
|
111
|
+
rdoc_options: []
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - ">="
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0'
|
119
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
requirements: []
|
125
|
+
rubyforge_project:
|
126
|
+
rubygems_version: 2.4.6
|
127
|
+
signing_key:
|
128
|
+
specification_version: 4
|
129
|
+
summary: Utility for managing blacklisted User Agent Strings.
|
130
|
+
test_files: []
|