Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sanitize inputs #1060

Merged
merged 1 commit into from
Dec 6, 2023
Merged
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
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