Skip to content

Commit 5c5d1b2

Browse files
committed
Reject non json formats
1 parent bf62b72 commit 5c5d1b2

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

app/controllers/v3/application_controller.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ 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!
72+
before_action :validate_request_format!
7173

7274
rescue_from CloudController::Blobstore::BlobstoreError, with: :handle_blobstore_error
7375
rescue_from CloudController::Errors::NotAuthenticated, with: :handle_not_authenticated
@@ -223,6 +225,19 @@ def null_coalesce_body
223225
hashed_params[:body] ||= {}
224226
end
225227

228+
def validate_content_type!
229+
return if request.content_type.nil? || Mime::Type.lookup(request.content_type) == :json
230+
231+
logger.error("Invalid content-type: #{request.content_type}")
232+
bad_request!('Invalid Content-Type')
233+
end
234+
235+
def validate_request_format!
236+
return if !hashed_params.include?(:format) || hashed_params[:format] == 'json'
237+
238+
bad_request!('Invalid format requested')
239+
end
240+
226241
def membership
227242
@membership ||= Membership.new(current_user)
228243
end

app/controllers/v3/packages_controller.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,19 @@ def send_package_blob(package)
204204
BlobDispatcher.new(blobstore: package_blobstore, controller: self).send_or_redirect(guid: package.guid)
205205
end
206206

207+
def validate_content_type!
208+
return if Mime::Type.lookup(request.content_type) == :url_encoded_form
209+
210+
logger.error("Invalid content-type: #{request.content_type}")
211+
bad_request!('Invalid Content-Type')
212+
end
213+
214+
def validate_request_format!
215+
return unless hashed_params.include?(:format)
216+
217+
bad_request!('Invalid format requested')
218+
end
219+
207220
def unprocessable_non_bits_package!
208221
unprocessable!('Cannot create Docker package for a buildpack app.')
209222
end

app/controllers/v3/space_manifests_controller.rb

Lines changed: 8 additions & 8 deletions
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.id, space.organization_id)
@@ -86,14 +84,16 @@ def compound_error!(error_messages)
8684
end
8785

8886
def validate_content_type!
89-
if !request_content_type_is_yaml?
90-
logger.error("Content-type isn't yaml: #{request.content_type}")
91-
bad_request!('Content-Type must be yaml')
92-
end
87+
return if Mime::Type.lookup(request.content_type) == :yaml
88+
89+
logger.error("Invalid content-type: #{request.content_type}")
90+
bad_request!('Invalid Content-Type')
9391
end
9492

95-
def request_content_type_is_yaml?
96-
Mime::Type.lookup(request.content_type) == :yaml
93+
def validate_request_format!
94+
return unless hashed_params.include?(:format)
95+
96+
bad_request!('Invalid format requested')
9797
end
9898

9999
def check_version_is_supported!

0 commit comments

Comments
 (0)