Skip to content

Commit

Permalink
Use starts_with? and simplify file expansion
Browse files Browse the repository at this point in the history
  • Loading branch information
sergioisidoro committed Jul 6, 2023
1 parent e0b3863 commit efd4ea4
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions rswag-api/lib/rswag/api/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ def initialize(app, config)

def call(env)
path = env['PATH_INFO']
# 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)))
# Sanitize the filename for directory traversal by expanding, and ensuring
# its starts with the root directory.
filename = File.expand_path(path, @config.resolve_swagger_root(env))
unless filename.start_with? @config.resolve_swagger_root(env)
return @app.call(env)
end

Expand Down

0 comments on commit efd4ea4

Please sign in to comment.