Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
### 2.2.0 (2017-08-31)

* [#46](https://github.com/square/rails-auth/pull/46)
Add linting for YAML acl checks (i.e. prevent duplicate keys)
([@cgthornt])

### 2.1.3 (2017-08-04)

* [#44](https://github.com/square/rails-auth/pull/44)
Expand Down
9 changes: 9 additions & 0 deletions lib/rails/auth/acl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ class ACL
# @param [String] :yaml serialized YAML to load an ACL from
def self.from_yaml(yaml, **args)
require "yaml"
require "yamllint"

linter = YamlLint::Linter.new
linter.check_stream(StringIO.new(yaml))
if linter.errors?
# Always in the format of {"" => ["msg1", "msg2", ...]}
msg = linter.errors[""].join(", ")
raise ParseError, "ACL lint failed: #{msg}"
end
new(YAML.load(yaml), **args)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/rails/auth/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
module Rails
# Pluggable authentication and authorization for Rack/Rails
module Auth
VERSION = "2.1.3".freeze
VERSION = "2.2.0".freeze
end
end
1 change: 1 addition & 0 deletions rails-auth.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Gem::Specification.new do |spec|
spec.required_ruby_version = ">= 2.0.0"

spec.add_runtime_dependency "rack"
spec.add_runtime_dependency "yamllint", "~> 0.0.9"

spec.add_development_dependency "bundler", "~> 1.10"
spec.add_development_dependency "rake", "~> 10.0"
Expand Down
7 changes: 7 additions & 0 deletions spec/fixtures/example_invalid_acl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
- resources:
- method: ALL
method: POST
path: /foo/bar/.*
- path: /foo/bar
path: /bar
13 changes: 13 additions & 0 deletions spec/rails/auth/acl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,17 @@
expect(resources.first.path).to eq %r{\A/foo/bar/.*\z}
end
end

describe ".from_yaml" do
subject { example_acl }

context "when given an invalid YAML file" do
let(:example_config) { fixture_path("example_invalid_acl.yml").read }

it "raises an error" do
expect { subject }.to raise_error Rails::Auth::ParseError,
'ACL lint failed: The same key is defined more than once: 0.resources.0.method, The same key is defined more than once: 0.resources.1.path'
end
end
end
end