Add docs and another allowlist example

This commit is contained in:
Brian P O'Rourke 2020-07-01 10:02:52 -07:00
parent a25ae50bc1
commit 089f8dc619
No known key found for this signature in database
GPG key ID: 5B0C73433C16416F
3 changed files with 22 additions and 3 deletions

View file

@ -2,6 +2,7 @@
=== Changes
* Rename `url_whitelist` to `url_allowlist`
* Allowlist now supports regular expressions
=== Breaking changes
* Failed checks against the allowlist now raise `UrlNotAllowed` rather than `NotWhitelistedUrl`

View file

@ -349,10 +349,13 @@ to one of the values specified in the url allowlist like so:
DatabaseCleaner.url_allowlist = ['postgres://postgres@localhost', 'postgres://foo@bar']
```
Allowlist elements may be specified as regular expressions if extra flexibility is required::
Allowlist elements are matched with case equality (`===`), so regular expressions or procs may be used:
```ruby
DatabaseCleaner.url_allowlist = [%r{^postgres://postgres@localhost}]
DatabaseCleaner.url_allowlist = [
%r{^postgres://postgres@localhost}, # match any db with this prefix
proc {|uri| URI.parse(uri).user == "test" } # match any db authenticating with the 'test' user
]
```
## COPYRIGHT

View file

@ -122,9 +122,24 @@ module DatabaseCleaner
expect { cleaner.start }.to raise_error(Safeguard::Error::UrlNotAllowed)
end
end
describe 'A url that matches a proc' do
let(:database_url) { 'redis://test:test@foo.bar' }
it 'does not raise' do
expect { cleaner.start }.to_not raise_error
end
end
end
let(:url_allowlist) { ['postgres://postgres@localhost', 'postgres://foo.bar', %r{^postgres://bar.baz}] }
let(:url_allowlist) do
[
'postgres://postgres@localhost',
'postgres://foo.bar',
%r{^postgres://bar.baz},
proc { |x| URI.parse(x).user == 'test' }
]
end
describe 'url_allowlist' do
before { DatabaseCleaner.url_allowlist = url_allowlist }