Skip to content

Commit 4ed37bc

Browse files
committed
Reject non json formats
1 parent 1ce6947 commit 4ed37bc

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

app/controllers/v3/application_controller.rb

+19
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class ApplicationController < ActionController::Base
6868
before_action :check_write_permissions!, if: :enforce_write_scope?
6969
before_action :hashify_params
7070
before_action :null_coalesce_body
71+
before_action :validate_content_type!
7172

7273
rescue_from CloudController::Blobstore::BlobstoreError, with: :handle_blobstore_error
7374
rescue_from CloudController::Errors::NotAuthenticated, with: :handle_not_authenticated
@@ -211,6 +212,24 @@ def null_coalesce_body
211212
hashed_params[:body] ||= {}
212213
end
213214

215+
def validate_content_type!
216+
unless request_content_type_is_json?
217+
logger.error("Content-type isn't json: #{request.content_type}")
218+
bad_request!('Content-Type must be json')
219+
end
220+
unless requested_format_is_json_or_none?
221+
bad_request!('Requested format must be json or none')
222+
end
223+
end
224+
225+
def request_content_type_is_json?
226+
Mime::Type.lookup(request.content_type) == :json
227+
end
228+
229+
def requested_format_is_json_or_none?
230+
!hashed_params.include?(:format) || hashed_params[:format] == 'json'
231+
end
232+
214233
def membership
215234
@membership ||= Membership.new(current_user)
216235
end

app/controllers/v3/space_manifests_controller.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
class SpaceManifestsController < ApplicationController
99
wrap_parameters :body, format: [:yaml]
1010

11-
before_action :validate_content_type!
12-
1311
def apply_manifest
1412
space = Space.find(guid: hashed_params[:guid])
1513
space_not_found! unless space && permission_queryer.can_read_from_space?(space.guid, space.organization.guid)
@@ -86,16 +84,23 @@ def compound_error!(error_messages)
8684
end
8785

8886
def validate_content_type!
89-
if !request_content_type_is_yaml?
87+
unless request_content_type_is_yaml?
9088
logger.error("Content-type isn't yaml: #{request.content_type}")
9189
bad_request!('Content-Type must be yaml')
9290
end
91+
unless requested_format_is_yaml_or_none?
92+
bad_request!('Requested format must be yaml or none')
93+
end
9394
end
9495

9596
def request_content_type_is_yaml?
9697
Mime::Type.lookup(request.content_type) == :yaml
9798
end
9899

100+
def requested_format_is_yaml_or_none?
101+
!hashed_params.include?(:format) || %w[yaml yml].include?(hashed_params[:format])
102+
end
103+
99104
def check_version_is_supported!
100105
version = parsed_yaml['version']
101106
raise unprocessable!('Unsupported manifest schema version. Currently supported versions: [1].') unless !version || version == 1

0 commit comments

Comments
 (0)