Skip to content

Commit

Permalink
Sanitize inputs
Browse files Browse the repository at this point in the history
Don't cast user input to float or integer without sanitizing
  • Loading branch information
jcoyne committed Dec 4, 2023
1 parent 03ce4a2 commit 9961777
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 11 deletions.
28 changes: 20 additions & 8 deletions app/controllers/legacy_image_service_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ def iiif_params

def iiif_size
case
when zoom
"pct:#{zoom}"
when allowed_params[:zoom]
"pct:#{allowed_params[:zoom]}"
when allowed_params[:w]
"#{allowed_params[:w]},#{allowed_params[:h]}"
when size
Expand All @@ -71,11 +71,10 @@ def iiif_size
end

def iiif_region
zoomed_region = Region.new(params[:region], params[:zoom]) if params[:region] && params[:zoom]
case
when region && zoom
x, y, w, h = region.split(',')
zoom_percent = zoom.to_f / 100.0
[x.to_i / zoom_percent, y.to_i / zoom_percent, w.to_i / zoom_percent, h.to_i / zoom_percent].map(&:to_i).join(',')
when zoomed_region
zoomed_region.to_iiif_region
when region
region
when size == 'square'
Expand Down Expand Up @@ -105,7 +104,20 @@ def size
allowed_params[:size]
end

def zoom
allowed_params[:zoom]
# A subset of an image defined by a region and zoom level
class Region
def initialize(raw_region, raw_zoom)
raise ActionController::RoutingError, 'zoom is invalid' unless /\A\d*\.?\d+\z/.match?(raw_zoom)
raise ActionController::RoutingError, 'region is invalid' unless /\A(\d+,){0,3}\d+\z/.match?(raw_region)

@zoom_percent = raw_zoom.to_f / 100.0
@x, @y, @w, @h = raw_region.split(',')
end

attr_reader :zoom_percent, :x, :y, :w, :h

def to_iiif_region
[x.to_i / zoom_percent, y.to_i / zoom_percent, w.to_i / zoom_percent, h.to_i / zoom_percent].map(&:to_i).join(',')
end
end
end
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
config.cache_store = :null_store

# Raise exceptions instead of rendering exception templates.
config.action_dispatch.show_exceptions = false
config.action_dispatch.show_exceptions = true

# Disable request forgery protection in test environment.
config.action_controller.allow_forgery_protection = false
Expand Down
5 changes: 3 additions & 2 deletions spec/controllers/legacy_image_service_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
end
end
end
context 'delivering tiles' do

describe 'delivering tiles' do
it 'works at 100% zoom' do
page = get :show, params: { id: 'nr349ct7889',
file_name: 'nr349ct7889_00_0001',
Expand All @@ -63,7 +64,7 @@
expect(page).to redirect_to '/image/iiif/nr349ct7889/nr349ct7889_00_0001/0,0,256,256/pct:100/0/default.jpg'
end

it 'works at 50% zome' do
it 'works at 50% zoom' do
page = get :show, params: { id: 'nr349ct7889',
file_name: 'nr349ct7889_00_0001',
format: 'jpg',
Expand Down
25 changes: 25 additions & 0 deletions spec/requests/legacy_image_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe 'Legacy image service' do
context 'with an invalid zoom value' do
before do
get '/image/nr349ct7889/nr349ct7889_00_0001.jpg?zoom=test&region=256,256,256,256'
end

it 'is not found' do
expect(response).to have_http_status(:not_found)
end
end

context 'with an invalid region value' do
before do
get '/image/nr349ct7889/nr349ct7889_00_0001.jpg?zoom=50&region=test'
end

it 'is not found' do
expect(response).to have_http_status(:not_found)
end
end
end

0 comments on commit 9961777

Please sign in to comment.