bad_json_request_handler 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ruby.yml +27 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +77 -0
- data/LICENSE.txt +21 -0
- data/README.md +69 -0
- data/Rakefile +10 -0
- data/_config.yml +1 -0
- data/bad_json_request_handler.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/bad_json_request_handler.rb +27 -0
- data/lib/bad_json_request_handler/version.rb +3 -0
- metadata +76 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 828695f1714cfbe7b30d337e49e13e0a5c36dc7ac50866db054afe14c414611e
|
4
|
+
data.tar.gz: ef2f2e806893faa54c2cfbb41763f4aca0883da79f8c4503f44ae09235e449cb
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dbb14fa92d33257692854ab0539b597d261931318ca2058d6d3a974e9b17071000b4c1f4fce81d9a8d067e7ea51e42b3ba7b62df95dade75b0144f07046cf23a
|
7
|
+
data.tar.gz: 8980908b61450051ac73a0dc21659137ae491f6710a1f0ea5752352efe28c155bc908969b98934422b1cca89133974b707195127f83ee3724eb1e83be6f7af09
|
@@ -0,0 +1,27 @@
|
|
1
|
+
name: Unit tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
pull_request:
|
5
|
+
branches:
|
6
|
+
- '*'
|
7
|
+
push:
|
8
|
+
branches:
|
9
|
+
- 'master'
|
10
|
+
|
11
|
+
jobs:
|
12
|
+
build:
|
13
|
+
runs-on: ubuntu-latest
|
14
|
+
strategy:
|
15
|
+
matrix:
|
16
|
+
ruby: [ '2.5', '2.6' ]
|
17
|
+
steps:
|
18
|
+
- uses: actions/checkout@v2
|
19
|
+
- name: Set up Ruby ${{matrix.ruby }}
|
20
|
+
uses: ruby/setup-ruby@v1
|
21
|
+
with:
|
22
|
+
ruby-version: ${{ matrix.ruby }}
|
23
|
+
- name: Build and test with Rake
|
24
|
+
run: |
|
25
|
+
gem install bundler
|
26
|
+
bundle install --jobs 4 --retry 3
|
27
|
+
bundle exec rake test
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at csonpatki@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
bad_json_request_handler (0.0.1)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (6.0.2.2)
|
10
|
+
actionview (= 6.0.2.2)
|
11
|
+
activesupport (= 6.0.2.2)
|
12
|
+
rack (~> 2.0, >= 2.0.8)
|
13
|
+
rack-test (>= 0.6.3)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
16
|
+
actionview (6.0.2.2)
|
17
|
+
activesupport (= 6.0.2.2)
|
18
|
+
builder (~> 3.1)
|
19
|
+
erubi (~> 1.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
22
|
+
activesupport (6.0.2.2)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 0.7, < 2)
|
25
|
+
minitest (~> 5.1)
|
26
|
+
tzinfo (~> 1.1)
|
27
|
+
zeitwerk (~> 2.2)
|
28
|
+
builder (3.2.4)
|
29
|
+
combustion (1.1.2)
|
30
|
+
activesupport (>= 3.0.0)
|
31
|
+
railties (>= 3.0.0)
|
32
|
+
thor (>= 0.14.6)
|
33
|
+
concurrent-ruby (1.1.6)
|
34
|
+
crass (1.0.6)
|
35
|
+
erubi (1.9.0)
|
36
|
+
i18n (1.8.2)
|
37
|
+
concurrent-ruby (~> 1.0)
|
38
|
+
loofah (2.4.0)
|
39
|
+
crass (~> 1.0.2)
|
40
|
+
nokogiri (>= 1.5.9)
|
41
|
+
method_source (1.0.0)
|
42
|
+
mini_portile2 (2.4.0)
|
43
|
+
minitest (5.14.0)
|
44
|
+
nokogiri (1.10.9)
|
45
|
+
mini_portile2 (~> 2.4.0)
|
46
|
+
rack (2.2.2)
|
47
|
+
rack-test (1.1.0)
|
48
|
+
rack (>= 1.0, < 3)
|
49
|
+
rails-dom-testing (2.0.3)
|
50
|
+
activesupport (>= 4.2.0)
|
51
|
+
nokogiri (>= 1.6)
|
52
|
+
rails-html-sanitizer (1.3.0)
|
53
|
+
loofah (~> 2.3)
|
54
|
+
railties (6.0.2.2)
|
55
|
+
actionpack (= 6.0.2.2)
|
56
|
+
activesupport (= 6.0.2.2)
|
57
|
+
method_source
|
58
|
+
rake (>= 0.8.7)
|
59
|
+
thor (>= 0.20.3, < 2.0)
|
60
|
+
rake (12.3.3)
|
61
|
+
thor (1.0.1)
|
62
|
+
thread_safe (0.3.6)
|
63
|
+
tzinfo (1.2.6)
|
64
|
+
thread_safe (~> 0.1)
|
65
|
+
zeitwerk (2.3.0)
|
66
|
+
|
67
|
+
PLATFORMS
|
68
|
+
ruby
|
69
|
+
|
70
|
+
DEPENDENCIES
|
71
|
+
bad_json_request_handler!
|
72
|
+
combustion (~> 1.1)
|
73
|
+
minitest (~> 5.0)
|
74
|
+
rake (~> 12.0)
|
75
|
+
|
76
|
+
BUNDLED WITH
|
77
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Prathamesh Sonpatki
|
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,69 @@
|
|
1
|
+
# BadJsonRequestHandler
|
2
|
+
|
3
|
+
[![Gem Version][gem-version-img]][gem-url]
|
4
|
+
|
5
|
+
You have a Rails API endpoint which expects JSON request payload. But some clients
|
6
|
+
send invalid JSON requests.
|
7
|
+
This typically results in following error.
|
8
|
+
|
9
|
+
```
|
10
|
+
ActionDispatch::Http::Parameters::ParseError: 767: unexpected token at '{ event: { seconds: }}'
|
11
|
+
```
|
12
|
+
|
13
|
+
This results in a 500 error from the server. But client does not get an idea that the
|
14
|
+
request payload was invalid. What if you can instead respond with 400 Bad Request response
|
15
|
+
which clearly tells the client that the request is invalid. They may start sending valid
|
16
|
+
requests going forward!
|
17
|
+
|
18
|
+
That's where this gem comes into the picture. It captures the `Parameters::ParseError`
|
19
|
+
and returns a Bad Request response back. Once you use the initialize from this gem,
|
20
|
+
the response will be as follows.
|
21
|
+
|
22
|
+
```
|
23
|
+
HTTP 400
|
24
|
+
{"errors"=>{"message"=>"Invalid request payload: 767: unexpected token at '{ event: { seconds: }}'"}}
|
25
|
+
```
|
26
|
+
|
27
|
+
## Installation
|
28
|
+
|
29
|
+
Add this line to your application's Gemfile:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
gem 'bad_json_request_handler'
|
33
|
+
```
|
34
|
+
|
35
|
+
And then execute:
|
36
|
+
|
37
|
+
$ bundle install
|
38
|
+
|
39
|
+
Or install it yourself as:
|
40
|
+
|
41
|
+
$ gem install bad_json_request_handler
|
42
|
+
|
43
|
+
## Usage
|
44
|
+
|
45
|
+
Include this middleware in your Rails application by adding it to the middleware stack as follows.
|
46
|
+
|
47
|
+
```rb
|
48
|
+
# config/initializers/bad_json_request_handler.rb
|
49
|
+
Rails.application.config.middleware.insert_after ActionDispatch::DebugExceptions, BadJsonRequestHandler::Middleware
|
50
|
+
|
51
|
+
```
|
52
|
+
## Development
|
53
|
+
|
54
|
+
After checking out the repo, run `rake test` to run the tests.
|
55
|
+
|
56
|
+
## Contributing
|
57
|
+
|
58
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/prathamesh-sonpatki/bad_json_request_handler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/prathamesh-sonpatki/bad_json_request_handler/blob/master/CODE_OF_CONDUCT.md).
|
59
|
+
|
60
|
+
|
61
|
+
## License
|
62
|
+
|
63
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
64
|
+
|
65
|
+
## Code of Conduct
|
66
|
+
|
67
|
+
Everyone interacting in the BadJsonRequestHandler project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/prathamesh-sonpatki/bad_json_request_handler/blob/master/CODE_OF_CONDUCT.md).
|
68
|
+
|
69
|
+
[gem-version-img]: https://badge.fury.io/rb/bad_json_request_handler.svg
|
data/Rakefile
ADDED
data/_config.yml
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
theme: jekyll-theme-minimal
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require_relative 'lib/bad_json_request_handler/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "bad_json_request_handler"
|
5
|
+
spec.version = BadJsonRequestHandler::VERSION
|
6
|
+
spec.authors = ["Prathamesh Sonpatki"]
|
7
|
+
spec.email = ["csonpatki@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Catch invalid JSON requests and return 400 response.}
|
10
|
+
spec.description = %q{Catch invalid JSON requests and return 400 response.}
|
11
|
+
spec.homepage = "https://prathamesh.tech"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/prathamesh-sonpatki/bad_json_request_handler"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/prathamesh-sonpatki/bad-json-request-handler/blob/master/CHANGELOG.md"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
|
30
|
+
spec.add_development_dependency 'combustion', '~> 1.1'
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "bad_json_request_handler"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require "bad_json_request_handler/version"
|
2
|
+
|
3
|
+
module BadJsonRequestHandler
|
4
|
+
class Middleware
|
5
|
+
def initialize(app)
|
6
|
+
@app = app
|
7
|
+
end
|
8
|
+
|
9
|
+
def call(env)
|
10
|
+
begin
|
11
|
+
@app.call(env)
|
12
|
+
rescue ActionDispatch::Http::Parameters::ParseError => error
|
13
|
+
error_output = "Invalid request payload: #{error}"
|
14
|
+
|
15
|
+
if env['CONTENT_TYPE'] =~ /application\/json/
|
16
|
+
return [
|
17
|
+
400,
|
18
|
+
{ "Content-Type" => "application/json" },
|
19
|
+
[ { errors: { message: error_output } }.to_json ]
|
20
|
+
]
|
21
|
+
else
|
22
|
+
raise error, error_output
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: bad_json_request_handler
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Prathamesh Sonpatki
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-03-23 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: combustion
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.1'
|
27
|
+
description: Catch invalid JSON requests and return 400 response.
|
28
|
+
email:
|
29
|
+
- csonpatki@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".github/workflows/ruby.yml"
|
35
|
+
- ".gitignore"
|
36
|
+
- ".travis.yml"
|
37
|
+
- CODE_OF_CONDUCT.md
|
38
|
+
- Gemfile
|
39
|
+
- Gemfile.lock
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- Rakefile
|
43
|
+
- _config.yml
|
44
|
+
- bad_json_request_handler.gemspec
|
45
|
+
- bin/console
|
46
|
+
- bin/setup
|
47
|
+
- lib/bad_json_request_handler.rb
|
48
|
+
- lib/bad_json_request_handler/version.rb
|
49
|
+
homepage: https://prathamesh.tech
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
allowed_push_host: https://rubygems.org
|
54
|
+
homepage_uri: https://prathamesh.tech
|
55
|
+
source_code_uri: https://github.com/prathamesh-sonpatki/bad_json_request_handler
|
56
|
+
changelog_uri: https://github.com/prathamesh-sonpatki/bad-json-request-handler/blob/master/CHANGELOG.md
|
57
|
+
post_install_message:
|
58
|
+
rdoc_options: []
|
59
|
+
require_paths:
|
60
|
+
- lib
|
61
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- - ">="
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: 2.3.0
|
66
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubygems_version: 3.1.2
|
73
|
+
signing_key:
|
74
|
+
specification_version: 4
|
75
|
+
summary: Catch invalid JSON requests and return 400 response.
|
76
|
+
test_files: []
|