Skip to content

Commit 841116a

Browse files
committed
Add optional middleware for disabling HTTP TRACE
1 parent fea6988 commit 841116a

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@ To get going clone this repository and perform the following steps:
2727
* [New Relic](https://newrelic.com) is pre configured in `config/newrelic.yml`,
2828
but you need to comment in the environment variables for it work on Heroku
2929
(lines 10 and 17).
30+
31+
## Configuration options
32+
33+
| Option | Comment |
34+
| --- | --- |
35+
| Disable HTTP TRACE method | Set BLOCK_HTTP_TRACE env var to true/t/1 |
3036

3137
## Contents
3238

config/application.rb

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
require 'rails/all'
44

5+
require_relative '../lib/rack/reject_methods'
6+
57
# Require the gems listed in Gemfile, including any gems
68
# you've limited to :test, :development, or :production.
79
Bundler.require(*Rails.groups)
@@ -31,6 +33,10 @@ class Application < Rails::Application
3133
g.javascripts false
3234
g.stylesheets false
3335
g.view_specs false
36+
37+
if ENV['BLOCK_HTTP_TRACE'].in?(%w(true t 1))
38+
config.middleware.use Rack::RejectMethods
39+
end
3440
end
3541
end
3642
end

lib/rack/reject_methods.rb

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module Rack
2+
class RejectMethods
3+
METHOD_BLACKLIST = %w(TRACE).freeze
4+
5+
def initialize(app)
6+
@app = app
7+
@blacklist = blacklist
8+
end
9+
10+
def call(env)
11+
return @app.call(env) unless @blacklist.include?(env['REQUEST_METHOD'])
12+
[405, {}, ["TRACE requests not allowed!\n"]]
13+
end
14+
15+
private def blacklist
16+
ENV.fetch('HTTP_METHOD_BLACKLIST'.split(',')) { METHOD_BLACKLIST }
17+
end
18+
end
19+
end

0 commit comments

Comments
 (0)