Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions lib/securitytxt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

# Rails SecurityTXT generator
module SecurityTxt
SECTIONS = %i[acknowledgment contact encryption signature policy].freeze
SECTIONS = %i[acknowledgment expires prefered_languages contact encryption signature policy].freeze
SECTIONS.each do |section|
if defined?(Rails)
mattr_accessor section
Expand All @@ -13,16 +13,17 @@ module SecurityTxt
end

if defined?(Rails)
# Rails engine that plugs middleware in application
class Application < Rails::Application
class Railtie < ::Rails::Railtie
config = proc do
SecurityTxt::SECTIONS.inject({}) do |acc, v|
vd = SecurityTxt.send(v)
acc[v] = vd unless vd.blank?
acc
end
end
Rails.application.config.middleware.use SecurityTxt::Middleware, config
initializer "security-txt.middleware" do |app|
app.middleware.use(SecurityTxt::Middleware, config)
end
end
end
end
5 changes: 3 additions & 2 deletions lib/securitytxt/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ def initialize(data = {})
def generate
ret = StringIO.new
sections.each do |name, value|
key = name.to_s.split("_").map { |k| capitalize(k) }.join('-')
next if value.nil? || value.empty?
if value.is_a?(Array)
value.each { |subvalue| ret << "#{capitalize(name)}: #{subvalue}\n" }
value.each { |subvalue| ret << "#{key}: #{subvalue}\n" }
else
ret << "#{capitalize(name)}: #{value}\n"
ret << "#{key}: #{value}\n"
end
end
ret.string
Expand Down
7 changes: 5 additions & 2 deletions lib/securitytxt/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ def call(env)
sections = @sections
sections = @sections.call if @sections.respond_to?(:call)
if req.path == '/.well-known/security.txt' && !sections.empty?
return Rack::Response.new(Generator.new(sections).generate, 200,
'Content-Type' => 'text/plain')
headers = { 'Content-Type' => 'text/plain' }
response_text = Generator.new(sections).generate

return [200, headers, [response_text]]
end

@app.call(env)
end
end
Expand Down