Skip to content

Commit

Permalink
Sanitize directory traversal in middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioisidoro committed Jun 30, 2023
1 parent 6c07c71 commit fc353ef
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion rswag-api/lib/rswag/api/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def initialize(app, config)

def call(env)
path = env['PATH_INFO']
filename = "#{@config.resolve_swagger_root(env)}/#{path}"
# Sanitize the filename for directory traversal by expanding, and matching the swagger root directory
filename = File.expand_path(File.join(@config.resolve_swagger_root(env), path))
unless filename.match Regexp.new('^' + Regexp.escape(@config.resolve_swagger_root(env)))
return @app.call(env)
end

if env['REQUEST_METHOD'] == 'GET' && File.file?(filename)
swagger = parse_file(filename)
Expand Down

0 comments on commit fc353ef

Please sign in to comment.