This repository has been archived by the owner on Feb 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 151
/
web.rb
1311 lines (1105 loc) · 40.6 KB
/
web.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##
# Copyright (c) 2016, salesforce.com, inc.
# All rights reserved.
# Licensed under the BSD 3-Clause license.
# For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause
##
require 'rubygems'
require 'sinatra/base'
require 'tilt/erb'
require 'rack/ssl'
require 'rack/csrf'
require 'base64'
require 'savon'
require 'nokogiri'
require 'resolv'
require 'ruby-saml'
require 'json'
require 'zip'
require 'chronic'
require 'net/http'
require 'uri'
require 'ipaddr'
require 'cgi'
require 'redis'
require 'active_support/core_ext/date_time/calculations'
require 'active_support/time'
require 'rufus/scheduler'
require 'date'
require 'securerandom'
require 'pony'
require 'rollbar'
require 'pdfkit'
#Load in environment vars from DotEnv
require 'dotenv'
Dotenv.load
require './lib/funcs'
require './models/init'
require './lib/auth'
require './lib/VRCron'
require './lib/VRDashConfig'
require './lib/VRLinkedObject'
if(onHeroku?)
#Only load this gem if running on Heroku. If not on Heroku
#this dep should be handled by local library install, not gem
require 'wkhtmltopdf-heroku'
end
# Load all cron files and VRDashConfigs and register them later
Dir["./crons/*.rb"].each {|cronfile| require cronfile}
Dir["./customDashes/*.rb"].each {|dcfile| require dcfile}
Dir["./linkedObjects/*.rb"].each {|lofile| require lofile}
##
# Main Vulnreport class
#
# @author Tim Bach <tim.bach@salesforce.com>, Salesforce
class Vulnreport < Sinatra::Base
use Rack::SSL
use Rack::Session::Cookie, :key => 'vr.session',
:path => '/',
:expire_after => 60*60*3, # In seconds
:secret => ((ENV['VR_SESSION_SECRET'].nil?) ? 'vrsession' : ENV['VR_SESSION_SECRET'])
use Rack::Csrf, :raise => true, :skip => ['POST:/saml/.*', 'POST:/search', 'POST:/markNotifsSeen']
#Set up Sinatra
set :root, File.dirname(__FILE__)
set :logging, true
set :method_override, true
set :inline_templates, true
set :static, true
whitelist = []
ssoUrl = getSetting('AUTH_SSO_TARGET_URL').to_s
vrRoot = getSetting('VR_ROOT').to_s
whitelist << vrRoot if(!vrRoot.nil? && !vrRoot.strip.empty?)
whitelist << "https://" + URI.parse(ssoUrl).host if(!ssoUrl.nil? && !ssoUrl.strip.empty?)
whitelist << "https://localhost"
set :protection, :origin_whitelist => whitelist, :except => [:frame_options, :remote_token]
configure do
Rollbar.configure do |config|
config.access_token = ENV['ROLLBAR_ACCESS_TOKEN'] #server access token
config.environment = Sinatra::Base.environment
config.framework = "Sinatra: #{Sinatra::VERSION}"
config.root = Dir.pwd
end
vr_mail_method = getSetting('VR_MAIL_METHOD')
if(!vr_mail_method.nil? && vr_mail_method == 'custom')
logputs "Setting mail method to custom"
Pony.options = {
:via => :smtp,
:via_options => {
:address => getSetting('VR_MAIL_ADDR'),
:port => getSetting('VR_MAIL_PORT').to_i,
:domain => getSetting('VR_MAIL_DOMAIN'),
:user_name => getSetting('VR_MAIL_USER'),
:password => getSetting('VR_MAIL_PASS'),
:authentication => :plain,
:enable_starttls_auto => true
}
}
end
set :redis, Redis.new(:url => ENV['REDIS_URL'])
set :vrurl, getSetting('VR_ROOT').to_s
set :vrname, getSetting('VR_INS_NAME').to_s
set :vrfooter, getSetting('VR_FOOTER').to_s
#Register custom dashconfigs
vrdcs = Array.new
VRDashConfig.each do |dc|
registerVRDashConfig(dc)
vrdcs << dc.vrdash_key.to_s
end
finalizeVRDashConfigs(vrdcs)
#Register custom VRLinkedObjects
vrlos = Array.new
VRLinkedObject.each do |lo|
registerVRLinkedObject(lo)
vrlos << lo.vrlo_key.to_s
end
finalizeVRLinkedObjects(vrlos)
end
configure :production do
logputs "Starting Vulnreport in PRODUCTION Environment"
set :force_ssl, true
@scheduler = Rufus::Scheduler.new
VRCron.each do |cron|
registerVRCron(cron, @scheduler)
end
end
configure :development do
logputs "WARNING: RUNNING IN DEVELOPMENT ENVIRONMENT"
logputs "Dev environment: CRON JOBS SCHEDULER NOT ENABLED"
@scheduler = Rufus::Scheduler.new
VRCron.each do |cron|
registerVRCron(cron, @scheduler, false)
end
end
helpers VulnreportAuth
helpers do
include Rack::Utils
alias_method :h, :escape_html
def csrf_token
Rack::Csrf.csrf_token(env)
end
def csrf_tag
Rack::Csrf.csrf_tag(env)
end
end
before do
@request_ip = request.ip
if(getSetting('IP_RESTRICTIONS_ON') == 'true')
ip_allow = getSetting('IP_RESTRICTIONS_ALLOWED')
if(!requestIPAllowed?(@request_ip, ip_allow))
logputs "IP address #{request.ip} does not match IP Access Restriction rules (#{ip_allow}) - REQUEST BLOCKED"
halt 401, "IP Access Restrictions do not allow access to Vulnreport from this IP address"
end
end
@session = session
@VRURL = settings.vrurl
@VRNAME = settings.vrname
@VRFOOTER = settings.vrfooter
if(session[:geo].nil?)
@geo = GEO::USA
else
@geo = session[:geo]
end
# Basic perm/auth checks based on route
if (!request.path_info.start_with?("/saml") && !(request.path_info == "/login" || request.path_info == "/login/"))
protected!
end
if (request.path_info.start_with?("/admin"))
only_admins!
end
if(request.path_info.start_with?("/reviews") || request.path_info.start_with?("/tests") || request.path_info.start_with?("/download") || request.path_info.start_with?("/cx"))
only_verified!
no_reporters!
end
if request.path_info.start_with?("/reports")
halt 401, (erb :unauth) if(!canUseReports?)
end
if(request.path_info.start_with?("/auditMonitors"))
halt 401, (erb :unauth) if(!canAuditMonitors?)
end
@bhthumb = Application.count
@user_notifs = Notification.forUser(@session[:uid])
@unaudited_mts = AuditRecord.count(:reviewed => false, :event_type => MONITOR_EVENT_TYPES)
end
# Adapter around the default RequestDataExtractor
class RequestDataExtractor
include Rollbar::RequestDataExtractor
def from_rack(env)
extract_request_data_from_rack(env).merge({
:route => env["PATH_INFO"]
})
end
end
error do
request_data = RequestDataExtractor.new.from_rack(env)
uinfo = {
:id => session[:uid],
:username => session[:username],
:email => session[:email]
}
#Rollbar.error(env['sinatra.error'], request_data, :user_info => uinfo)
Rollbar.report_exception(env['sinatra.error'], request_data, uinfo)
@errstr = "Something went wrong during this request (uncaught exception). The error has been logged and an alert has been sent. It will be debugged ASAP."
erb :error
end
not_found do
uinfo = {
:id => session[:uid],
:username => session[:username],
:email => session[:email]
}
Rollbar.scoped({:person => uinfo}) do
Rollbar.warning("404 - Route Not Found", {:route => request.path_info, :referrer => request.referrer})
end
@errstr = "Vulnreport was unable to find a route to handle this request. The error has been logged and an alert has been sent. It will be debugged ASAP."
erb :error
end
######################
# APPLICATION ROUTES #
######################
show_dash = lambda do |dcid|
cache = true
if(params['cacheref'] == '1')
cache = false
end
#Values needed for page navigation and style
@contractors = Organization.all(:contractor => true)
@user = User.get(session[:uid])
if(!@user.verified?)
return erb :unverified
end
# Dash components
# => @panels is array of hashes, each hash being panel components (title, records, hasbulkptc, zerotext, fetch_time). Rendered top-down in order
# => @statblocks is an array of 4 hashes (block_#), each hash being stat block info (icon, value, color). Rendered left-right in order
@panels = Array.new
@statblocks = Array.new
geos = @geo
if(geos == 0)
geos = GEO.constants.map{|e| GEO.const_get(e)}
end
@dashId = dcid
@dashOptions = DashConfig.all(:active => true)
# Default dashboard is its own case
if(dcid == 0)
defaultPanels = [{:title => "My Active Reviews", :color => "primary", :type => DASHPANEL_TYPE::MYACTIVE, :maxwks => 0, :zerotext => "No Records"},
{:title => "My New Reviews", :color => "primary", :type => DASHPANEL_TYPE::MY_WNO_TESTS, :maxwks => 0, :zerotext => "No Records"},
{:title => "My Pending Approvals", :color => "primary", :type => DASHPANEL_TYPE::MY_APPROVALS, :maxwks => 0, :zerotext => "No Records"}]
dc = DashConfig.new(:name => "Default Dashboard", :showStats => false, :customCode => false, :panels => defaultPanels)
@dashName = "Default Dashboard"
else
dc = DashConfig.get(dcid)
@dashName = dc.name
end
if(dc.customCode)
dashSubclass = VRDashConfig.getByKey(dc.customKey)
if(dashSubclass.nil?)
Rollbar.error("Custom DC subclass missing", {:DCID => dc.id, :Key => dc.customKey})
@errstr = "Unable to generate dashboard - subclass missing"
return erb :error
end
begin
dashResult = dashSubclass.generate(dc.getSettingsForDash, @user.id, geos, cache)
rescue Exception => e
dashResult = {:success => false, :faultstring => "Exception occurred while generating dashboard"}
Rollbar.error(e, "Exception while generating custom dashboard", {:uid => @user.id, :dc_key => dc.customKey})
end
if(dashResult[:success])
@panels, @statblocks = dashResult[:generatedDash]
if(!@statblocks.nil?)
@showStats = true
end
else
Rollbar.error("DC generate failure", {:DCID => dc.id, :Key => dc.customKey, :fault => dashResult[:faultstring]})
@dashError = true
@errstr = "Unable to generate dashboard - #{dashResult[:faultstring]}"
return erb :dash
end
else
#First build the panels, *respecting permissions over all else*
dc.panels.each do |panel|
records = Array.new
addedThisPanel = Array.new
#Check RT access first
if(!getAllowedRTsForUser(@user.id).include?(panel[:rt].to_i) && !NON_RT_DASHPANELS.include?(panel[:type]))
records = []
elsif(panel[:type] == DASHPANEL_TYPE::MYACTIVE)
Test.all(:reviewer => session[:uid], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MYACTIVE_RT)
Test.all(Test.application.record_type => panel[:rt], :reviewer => session[:uid], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_WNO_TESTS)
Application.all(:owner => session[:uid], :tests => nil, :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_WNO_TESTS_RT)
Application.all(:record_type => panel[:rt], :owner => session[:uid], :tests => nil, :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::STATUS_NEW_AND_INPROG)
Test.all(Test.application.record_type => panel[:rt], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::STATUS_PASSED)
Test.all(Test.application.record_type => panel[:rt], :complete => true, :pass => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::STATUS_FAILED)
Test.all(Test.application.record_type => panel[:rt], :complete => true, :pass => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::STATUS_CLOSED)
Test.all(Test.application.record_type => panel[:rt], :complete => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::ALL_APPS)
Application.all(:record_type => panel[:rt], :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::APPS_WNO_TESTS)
Application.all(:record_type => panel[:rt], :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::UNASSIGNED_NEW_RT)
Application.all(:record_type => panel[:rt], :geo => geos, :owner => nil).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::UNASSIGNED_NEW_ALL)
Application.all(:geo => geos, :owner => nil).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
records << {:app => a, :test => nil}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_PASSED)
Test.all(:reviewer => session[:uid], :complete => true, :pass => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_FAILED)
Test.all(:reviewer => session[:uid], :complete => true, :pass => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_ALL)
Test.all(:reviewer => session[:uid], :complete => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_APPROVALS)
Test.all(:is_pending => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
if(canApproveProvPass?)
Test.all(:complete => false, :provPassReq => true, :provPass => false).each do |t|
next if(addedThisPanel.include?(t.application_id))
if(getAllowedRTsForUser(@session[:uid]).include?(t.application.record_type))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
end
end
elsif(panel[:type] == DASHPANEL_TYPE::MY_APPROVALS)
Test.all(:is_pending => true, Test.application.record_type => panel[:rt], Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
if(canApproveProvPass?)
Test.all(:complete => false, :provPassReq => true, :provPass => false, Test.application.record_type => panel[:rt]).each do |t|
next if(addedThisPanel.include?(t.application_id))
if(getAllowedRTsForUser(@session[:uid]).include?(t.application.record_type))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
records << {:app => a, :test => t}
addedThisPanel << a.id
end
end
end
end
@panels << {:title => panel[:title], :color => panel[:color], :records => records, :maxwks => ((panel[:maxwks] <= 0) ? nil : panel[:maxwks]),
:fetch_time => nil, :zerotext => panel[:zerotext], :panelType => panel[:type], :panelRT => panel[:rt]}
end
#Build stats
if(dc.showStats)
@showStats = true
dc.stats.each do |stat|
count = 0
addedThisPanel = Array.new
#First, see if there is an identical panel to just get its size instead of recreating queries
isPanelDup = false
@panels.each do |panel|
if(stat[:type] == panel[:panelType] && stat[:rt] == panel[:panelRT])
isPanelDup = true
count = panel[:records].size
end
end
#Not a panel dup, so do some calculating. Same as above, but we will only care about the count.
# We have to actually do full db queries to check perms, last status, and duplication
if(!isPanelDup)
if(!getAllowedRTsForUser(@user.id).include?(stat[:rt].to_i) && stat[:type] != DASHPANEL_TYPE::MYACTIVE)
count = 0
elsif(stat[:type] == DASHPANEL_TYPE::MYACTIVE)
Test.all(:reviewer => session[:uid], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MYACTIVE_RT)
Test.all(Test.application.record_type => stat[:rt], :reviewer => session[:uid], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_WNO_TESTS)
Application.all(:reviewer => session[:uid], :tests => nil, :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_WNO_TESTS_RT)
Application.all(:record_type => stat[:rt], :reviewer => session[:uid], :tests => nil, :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::STATUS_NEW_AND_INPROG)
Test.all(Test.application.record_type => stat[:rt], :complete => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::STATUS_PASSED)
Test.all(Test.application.record_type => stat[:rt], :complete => true, :pass => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::STATUS_FAILED)
Test.all(Test.application.record_type => stat[:rt], :complete => true, :pass => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::STATUS_CLOSED)
Test.all(Test.application.record_type => stat[:rt], :complete => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::ALL_APPS)
Application.all(:record_type => stat[:rt], :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::APPS_WNO_TESTS)
Application.all(:record_type => stat[:rt], :geo => geos).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::UNASSIGNED_NEW_RT)
Application.all(:record_type => stat[:rt], :geo => geos, :owner => nil).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::UNASSIGNED_NEW_ALL)
Application.all(:geo => geos, :owner => nil).each do |a|
next if(addedThisPanel.include?(a.id))
next if (a.tests.size > 0)
next if (a.isPrivate && !canViewReview?(a.id))
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_PASSED)
Test.all(:reviewer => session[:uid], :complete => true, :pass => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_FAILED)
Test.all(:reviewer => session[:uid], :complete => true, :pass => false, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_ALL)
Test.all(:reviewer => session[:uid], :complete => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
#Only care about most recent test as it is app's status
next if(t.id != a.tests.last.id)
count += 1
addedThisPanel << a.id
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_APPROVALS)
Test.all(:is_pending => true, Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
count += 1
addedThisPanel << a.id
end
if(canApproveProvPass?)
Test.all(:complete => false, :provPassReq => true, :provPass => false).each do |t|
next if(addedThisPanel.include?(t.application_id))
if(getAllowedRTsForUser(@session[:uid]).include?(t.application.record_type))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
count += 1
addedThisPanel << a.id
end
end
end
elsif(stat[:type] == DASHPANEL_TYPE::MY_APPROVALS_RT)
Test.all(:is_pending => true, Test.application.record_type => stat[:rt], Test.application.geo => geos).each do |t|
next if(addedThisPanel.include?(t.application_id))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
count += 1
addedThisPanel << a.id
end
if(canApproveProvPass?)
Test.all(:complete => false, :provPassReq => true, :provPass => false, Test.application.record_type => stat[:rt]).each do |t|
next if(addedThisPanel.include?(t.application_id))
if(getAllowedRTsForUser(@session[:uid]).include?(t.application.record_type))
a = Application.get(t.application_id)
next if (a.isPrivate && !canViewReview?(a.id))
next if !canFinalizeTest?(t.id)
count += 1
addedThisPanel << a.id
end
end
end
end
end
color = stat[:color]
if(color.start_with?("auto"))
lim = color[4..5].to_i
if(count < lim)
color = "success"
elsif(count < lim*2)
color = "warning"
else
color = "danger"
end
end
@statblocks << {:icon => stat[:icon], :text => stat[:text], :value => count, :color => color}
end
else
@showStats = false
end
end
erb :dash
end
##
# Main index page - dashboard.
# @route /
# @viewfile views/dash.erb
get '/' do
@user = User.get(session[:uid])
#What dash config are we using?
if(@user.dashOverride >= 0)
dcid = @user.dashOverride
else
if(!@user.verified?)
dcid = 0
else
org = Organization.get(@user.org)
if(org.nil?)
dcid = 0
else
dcid = org.dashconfig
end
end
end
if(dcid > 0)
dc = DashConfig.get(dcid)
dcid = 0 if(dc.nil?)
end
instance_exec dcid, &show_dash
end
get '/showdash/:dcid/?' do
dcid = params[:dcid].to_i
dc = DashConfig.get(dcid)
dcid == 0 if(dc.nil?)
instance_exec dcid, &show_dash
end
get "/geo/:geo/?" do
session[:geo] = params[:geo].to_i
redirect "/"
end
post "/postcomment/:what/:whatid/?" do
what = nil
whatId = nil
if(params[:what] == "a")
app = Application.get(params[:whatid])
if(app.nil?)
@errstr = "App not found"
return erb :error
end
halt 401, (erb :unauth) if(!canViewReview?(app.id))
what = LINK_TYPE::APPLICATION
whatId = app.id
elsif(params[:what] == "t")
test = Test.get(params[:whatid])
if(test.nil?)
@errstr = "Test not found"
return erb :error
end
app = test.application
halt 401, (erb :unauth) if(!canViewReview?(app.id))
what = LINK_TYPE::TEST
whatId = test.id
elsif(params[:what] == "v")
vuln = Vulnerability.get(params[:whatid])
if(vuln.nil?)
@errstr = "Vuln not found"
return erb :error
end
test = vuln.test
app = test.application
halt 401, (erb :unauth) if(!canViewReview?(app.id))
what = LINK_TYPE::VULN
whatId = vuln.id
end
if(what.nil? || whatId.nil?)
@errstr = "No What/WhatID to post comment"
return erb :error
end
body = params[:body].strip
vis_myOrg = false
if (!params[:vis_myOrg].nil?)
vis_myOrg = true
end
vis_tester = false
if (!params[:vis_tester].nil?)
vis_tester = true
end
vis_testOrg = false
if (!params[:vis_testOrg].nil?)
vis_testOrg = true
end
if(body.nil? || body.empty?)
redirect "/tests/#{test.id}/#{vuln.id}"
else
c = Comment.create(:what => what, :whatId => whatId, :body => body, :author => session[:uid], :views => [session[:uid]], :vis_authOrg => vis_myOrg, :vis_tester => vis_tester, :vis_testOrg => vis_testOrg)
end
if(what == LINK_TYPE::APPLICATION)
redirect "/reviews/#{whatId}"
elsif(what == LINK_TYPE::TEST)
redirect "/tests/#{whatId}"
elsif(what == LINK_TYPE::VULN)
redirect "/tests/#{test.id}/#{whatId}"
end
end
post "/markCommentsRead/:what/:whatid/?" do
what = nil
whatId = nil
if(params[:what] == "a")
app = Application.get(params[:whatid])
if(app.nil?)
@errstr = "App not found"
return erb :error
end
halt 401, (erb :unauth) if(!canViewReview?(app.id))
comments = Comment.commentsForApp(params[:whatid], session[:uid], session[:org])
elsif(params[:what] == "t")
test = Test.get(params[:whatid])
if(test.nil?)
@errstr = "Test not found"
return erb :error
end
app = test.application
halt 401, (erb :unauth) if(!canViewReview?(app.id))
comments = Comment.commentsForTest(params[:whatid], session[:uid], session[:org])
elsif(params[:what] == "v")
vuln = Vulnerability.get(params[:whatid])
if(vuln.nil?)
@errstr = "Vuln not found"
return erb :error
end
test = vuln.test
app = test.application
halt 401, (erb :unauth) if(!canViewReview?(app.id))
comments = Comment.commentsForVuln(params[:whatid], session[:uid], session[:org])
end
comments.each do |c|
if(c.isUnseen?(session[:uid]))
c.markSeen(session[:uid])
end
end
return 200
end
post "/markNotifsSeen/?" do
Notification.markAllUserRead(session[:uid])
return 200
end
get "/viewAllNotifs/?" do
@notifs = Notification.allForUser(session[:uid])
Notification.markAllUserRead(session[:uid])
erb :view_all_notifs
end
post "/delComment/:cid/?" do
c = Comment.get(params[:cid].to_i)
if(c.author != session[:uid] && !admin?)
return 401
else
if(c.destroy)
return 200
else
return 500
end
end
end
get '/usersettings' do
@user = User.get(session[:uid])
userAlloc = MonthlyAllocation.allocationForUser(@user.id)
if(userAlloc.nil?)
@alloc = 0
else
@alloc = userAlloc.allocation
end
@dashConfigs = DashConfig.all(:active => true)
erb :usersettings
end
post '/usersettings' do
if(!params[:save].nil?)
@user = User.get(session[:uid])
newName = params[:userName].strip
@user.name = newName unless newName.nil?
newInits = params[:userInitials].strip
@user.initials = newInits unless newInits.nil?
newEmail = params[:email].strip
@user.email = newEmail unless (newEmail.nil? || !isValidEmail?(newEmail))