Skip to content

Commit 1a810d9

Browse files
authored
Merge pull request #65 from nelsonjr/master
Protecting HTML UI from XSS injection.
2 parents f02bc4d + fad0b75 commit 1a810d9

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

lib/puppet_forge_server/app/frontend.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,18 @@ def initialize(root, http_client = PuppetForgeServer::Http::HttpClient.new)
4545

4646
get '/modules' do
4747
query = params[:query]
48+
halt(400, haml(:security, :locals => {:query => query})) \
49+
unless safe_input? query
50+
4851
modules = get("#{request.base_url}/v3/modules?query=#{query}")['results']
4952
haml :modules, :locals => {:query => query, :modules => modules}
5053
end
5154

5255
get '/module' do
5356
module_v3_name = params[:name].gsub(/\//, '-')
57+
halt(400, haml(:security, :locals => {:query => module_v3_name})) \
58+
unless safe_input? module_v3_name
59+
5460
releases = get("#{request.base_url}/v3/modules/#{module_v3_name}")['releases']
5561
if params.has_key? 'version'
5662
module_uri = releases.find {|r| r['version'] == params['version']}['uri']
@@ -63,7 +69,10 @@ def initialize(root, http_client = PuppetForgeServer::Http::HttpClient.new)
6369
rescue
6470
readme_markdown = ''
6571
end
66-
haml :module, :locals => { :module_metadata => module_metadata, :base_url => request.base_url, :readme_markdown => readme_markdown, :releases => releases }
72+
haml :module, :locals => { :module_metadata => module_metadata,
73+
:base_url => request.base_url,
74+
:readme_markdown => readme_markdown,
75+
:releases => releases }
6776
end
6877

6978
get '/upload' do
@@ -84,5 +93,10 @@ def get(relative_url)
8493
{'results' => []}
8594
end
8695
end
96+
97+
def safe_input?(query)
98+
unsafe_query = CGI::unescape(query)
99+
%w[< javascript:].none? { |q| unsafe_query.include?(q) }
100+
end
87101
end
88102
end
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-# -*- encoding: utf-8 -*-
2+
-#
3+
-# Copyright 2014 North Development AB
4+
-#
5+
-# Licensed under the Apache License, Version 2.0 (the "License");
6+
-# you may not use this file except in compliance with the License.
7+
-# You may obtain a copy of the License at
8+
-#
9+
-# http://www.apache.org/licenses/LICENSE-2.0
10+
-#
11+
-# Unless required by applicable law or agreed to in writing, software
12+
-# distributed under the License is distributed on an "AS IS" BASIS,
13+
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
-# See the License for the specific language governing permissions and
15+
-# limitations under the License.
16+
17+
%h3.search-results-title Security violation
18+
19+
%h4 The query provided contains illegal or dangerous characters.
20+
Please clean them up and try again.

0 commit comments

Comments
 (0)