This repository has been archived by the owner on Jul 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathapp.rb
290 lines (250 loc) · 6.9 KB
/
app.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
require 'sinatra/base'
require 'sinatra/reloader'
require 'sinatra/content_for'
require 'sprockets'
require 'sinatra/sprockets-helpers'
require 'erubis'
require 'zendesk_api'
require './models/forms'
# App is the main Sinatra application
class App < Sinatra::Base
set :sprockets, Sprockets::Environment.new
set :title, 'GOV.UK Platform as a Service'
set :erb, :escape_html => true
# IMPORTANT: The app does not have CSRF protection enabled and if
# you need sessions you should set that up appropriately using
# Rack::Protection.
set :sessions, false
# Critical protection against Path Traversal.
use Rack::Protection::PathTraversal
# Protection to limit potential XSS attacks and effects.
use Rack::Protection::ContentSecurityPolicy,
:default_src => "none",
:script_src => "'self' www.google-analytics.com",
:style_src => "'self' 'unsafe-inline'",
:img_src => "'self' www.google-analytics.com",
:connect_src => "'self' www.google-analytics.com",
:frame_src => "'self'",
:font_src => "'self' data:",
:object_src => "'self'",
:media_src => "'self'"
configure do
sprockets.append_path File.join(root, 'assets', 'stylesheets', 'govuk_frontend_toolkit')
sprockets.append_path File.join(root, 'assets', 'stylesheets', 'govuk_elements')
sprockets.append_path File.join(root, 'assets', 'stylesheets', 'govuk_template')
sprockets.append_path File.join(root, 'assets', 'stylesheets')
sprockets.append_path File.join(root, 'assets', 'fonts')
sprockets.append_path File.join(root, 'assets', 'js')
sprockets.append_path File.join(root, 'assets')
Sprockets::Helpers.configure do |config|
config.debug = true if development?
config.prefix = "/"
config.environment = sprockets
end
register Sinatra::Reloader if development?
end
helpers Sinatra::ContentFor
helpers Sprockets::Helpers
helpers Forms::Helpers
get '/?' do
erb :index
end
get '/contact-us' do
@errors = {}
@form = Forms::Contact.new
erb :'forms/contact-us'
end
post '/contact-us' do
@errors = {}
@form = Forms::Contact.new(params)
if not @form.valid?
@errors = @form.errors
status 400
erb :'forms/contact-us'
else
begin
send_ticket @form
@msg = "We’ll contact you in the next working day"
erb :'forms/thanks'
rescue => ex
status 500
@errors[:fatal] = [Forms::UNKNOWN_ERROR_MESSAGE]
erb :'forms/contact-us'
end
end
end
get '/signup' do
@errors = {}
@form = Forms::Signup.new({
:person_is_manager => true,
:invites => 3.times.map{ Forms::Invite.new({
:person_email => '',
:person_is_manager => false
}) }
})
erb :'forms/signup'
end
post '/signup' do
@errors = {}
# Sanitise invites
default_invite_params = {'0': {:person_email => '', :person_is_manager => false}}
params[:invites] = (params[:invites] || default_invite_params).map{ |indexKey, invite|
Forms::Invite.new(invite)
}.reject{|invite| invite.person_email.empty? }
# delete step
params.delete(:step)
@form = Forms::Signup.new(params)
if not @form.valid?
@errors = @form.errors
status 400
return erb :'forms/signup'
else
begin
send_ticket @form
@msg = "We’ll email you with your organisation account details in the next working day."
erb :'forms/thanks'
rescue => ex
status 500
@errors[:fatal] = [Forms::UNKNOWN_ERROR_MESSAGE]
erb :'forms/signup'
end
end
end
post '/sign-in' do
case params[:region]
when 'london'
redirect('https://admin.london.cloud.service.gov.uk/', 302)
when 'ireland'
redirect('https://admin.cloud.service.gov.uk/', 302)
else
erb :'sign-in'
end
end
get '/support' do
@errors = {}
erb :'forms/support'
end
post '/support' do
if params[:support_form].nil? or params[:support_form].empty?
@errors = {support_form: "Please select an option"}
status 400
erb :'forms/support'
else
redirect("/support/#{params[:support_form]}", 302)
end
end
# Get the model class form for the support page
def get_support_form_class(name)
case name
when "something-wrong-with-service"
Forms::SupportSomethingWrongWithService
when "help-using-paas"
Forms::SupportHelpUsingPaas
when "find-out-more"
Forms::SupportFindOutMore
else
nil
end
end
get '/support/*' do
form_name = params[:splat].first
path = "forms/" + form_name
if !/^[\/a-zA-Z0-9_-]+$/.match(form_name) or ! File.exist? "views/#{path}.erb"
return not_found
end
form_class = get_support_form_class(form_name)
if form_class.nil?
return not_found
end
@form = form_class.new
content_type 'text/html;charset=utf8'
erb(path.to_sym)
end
post '/support/*' do
form_name = params[:splat].first
path = "forms/" + form_name
if !/^[\/a-zA-Z0-9_-]+$/.match(form_name) or ! File.exist? "views/#{path}.erb"
return not_found
end
form_class = get_support_form_class(form_name)
if form_class.nil?
return not_found
end
@errors = {}
params.delete(:splat)
@form = form_class.new(params)
if not @form.valid?
@errors = @form.errors
status 400
return erb(path.to_sym)
else
begin
send_ticket @form
submitted_path = path + '_submitted'
if ! File.exist? "views/#{submitted_path}.erb"
@msg = "We try to reply to all queries by the end of the next working day."
erb :'forms/thanks'
else
erb(submitted_path.to_sym)
end
rescue => ex
status 500
@errors[:fatal] = [Forms::UNKNOWN_ERROR_MESSAGE]
erb(path.to_sym)
end
end
end
get '/*' do
path = params[:splat].first
# Check for a relevant erb template
view_name = path.sub(/\.html$/, '')
# Protected against directory traversal by Rack::Protection::PathTraversal
# We also restrict view names to alphanumeric, dash and underscore characters.
# These are definitively safe against being used for directory traversal/etc.
if !/[^a-zA-Z0-9_-]/.match(view_name)
# Check for an appropriately-named view
if File.exist? "views/#{view_name}.erb"
# Strip `.html` extension if present
if path.match?(/\.html/)
return redirect("/#{view_name}", 301)
end
# Render the view's erb template
content_type 'text/html;charset=utf8'
return erb(view_name.to_sym)
end
end
# Check for an appropriately-named Sprocket asset
res = settings.sprockets.call(env)
return res if res && res[0] != 404
# Return 404 Not Found
not_found
end
not_found do
content_type 'text/html;charset=utf8'
erb :not_found
end
error do
if settings.development?
@error = env['sinatra.error']
end
content_type 'text/html;charset=utf8'
erb :error
end
helpers do
# create a zendesk client
def zendesk
ZendeskAPI::Client.new do |config|
config.url = ENV['ZENDESK_URL']
config.username = ENV['ZENDESK_USER']
config.token = ENV['ZENDESK_TOKEN']
end
end
def send_ticket(form)
if ENV['FAKE_ZENDESK'].nil? or ENV['FAKE_ZENDESK'].empty?
zendesk.tickets.create! form.to_zendesk_ticket
else
pp form.to_zendesk_ticket()
end
end
end
end