Skip to content

Commit 6c26c02

Browse files
authored
Merge pull request #1852 from psu-libraries/1851_bot_requests
Ignore Bot Requests to /resources/{uuid} That Trigger Noise & Bugsnag Alerts
2 parents 1a6c06f + d945670 commit 6c26c02

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ jobs:
121121
- run:
122122
name: "Bundle"
123123
command: |
124-
gem install bundler
124+
gem install bundler -v "$(grep -A 1 "BUNDLED WITH" Gemfile.lock | tail -n 1)"
125125
bundle check --path vendor/bundle || bundle install --deployment
126126
- run:
127127
name: "Yarn"

app/controllers/resources_controller.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# frozen_string_literal: true
22

33
class ResourcesController < ApplicationController
4+
rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found
5+
46
def show
57
@resource = ResourceDecorator.decorate(find_resource(params[:id]))
68
authorize @resource
@@ -49,4 +51,13 @@ def alternate_format_request_params
4951
:title
5052
)
5153
end
54+
55+
def resource_not_found(exception)
56+
if request.get? && request.path.match?(%r{^/resources/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$})
57+
Bugsnag.notify(exception, &:ignore!)
58+
head :not_found
59+
else
60+
raise exception
61+
end
62+
end
5263
end

spec/controllers/resources_controller_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,12 +77,24 @@
7777

7878
context 'when requesting an unknown uuid' do
7979
it do
80+
allow(Bugsnag).to receive(:notify)
8081
expect {
8182
get :show, params: { id: 'not-a-valid-uuid' }
8283
}.to raise_error(ActiveRecord::RecordNotFound)
8384
end
8485
end
8586

87+
context 'when requesting an unknown uuid that follows uuid pattern' do
88+
it do
89+
allow(Bugsnag).to receive(:notify)
90+
expect {
91+
get :show, params: { id: '123e4567-e89b-12d3-a456-426614174000' }
92+
}.not_to raise_error(ActiveRecord::RecordNotFound)
93+
expect(Bugsnag).to have_received(:notify).with(ActiveRecord::RecordNotFound, &:ignore!)
94+
expect(response).to have_http_status :not_found
95+
end
96+
end
97+
8698
context 'when the resource is valid with no access' do
8799
let(:work) { create(:work, :with_no_access) }
88100

0 commit comments

Comments
 (0)