/
MarkVerbHandlersWithValidateAntiforgeryTokenTests.cs
923 lines (801 loc) · 27 KB
/
MarkVerbHandlersWithValidateAntiforgeryTokenTests.cs
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
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Linq;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Testing;
using Xunit;
using VerifyCS = Microsoft.CodeAnalysis.CSharp.Testing.XUnit.CodeFixVerifier<
Microsoft.NetFramework.Analyzers.MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
using VerifyVB = Microsoft.CodeAnalysis.VisualBasic.Testing.XUnit.CodeFixVerifier<
Microsoft.NetFramework.Analyzers.MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer,
Microsoft.CodeAnalysis.Testing.EmptyCodeFixProvider>;
namespace Microsoft.NetFramework.Analyzers.UnitTests
{
public class MarkVerbHandlersWithValidateAntiforgeryTokenTests
{
#region Boilerplate
private static DiagnosticResult GetCA3147CSharpNoVerbs(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.NoVerbsRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147CSharpNoVerbsNoToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.NoVerbsNoTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147CSharpGetAndToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.GetAndTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147CSharpGetAndOtherToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.GetAndOtherAndTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147CSharpVerbsAndNoToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.VerbsAndNoTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147BasicNoVerbs(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.NoVerbsRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147BasicNoVerbsNoToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.NoVerbsNoTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147BasicGetAndToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.GetAndTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147BasicGetAndOtherToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.GetAndOtherAndTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
private static DiagnosticResult GetCA3147BasicVerbsAndNoToken(int line, int column, string controllerAction)
{
return new DiagnosticResult(MarkVerbHandlersWithValidateAntiforgeryTokenAnalyzer.VerbsAndNoTokenRule).WithLocation(line, column).WithArguments(controllerAction);
}
#endregion
/// <summary>
/// Types in the System.Web.Mvc namespace that the analyzer looks for, so we don't have to reference ASP.NET MVC bits.
/// </summary>
private const string SystemWebMvcNamespaceCSharp = @"
namespace System.Web.Mvc
{
using System;
public class Controller { }
public class ControllerBase { }
public class ActionResult { }
public class ContentResult : ActionResult { }
public class ValidateAntiForgeryTokenAttribute : Attribute { }
public class HttpGetAttribute : Attribute { }
public class HttpPostAttribute : Attribute { }
public class HttpPutAttribute : Attribute { }
public class HttpDeleteAttribute : Attribute { }
public class HttpPatchAttribute : Attribute { }
public class AcceptVerbsAttribute : Attribute
{
public AcceptVerbsAttribute(params string[] verbs)
{
}
public AcceptVerbsAttribute(HttpVerbs verbs)
{
}
}
public class NonActionAttribute : Attribute { }
public class ChildActionOnlyAttribute : Attribute { }
/// <summary>Enumerates the HTTP verbs.</summary>
[Flags]
public enum HttpVerbs
{
/// <summary>Retrieves the information or entity that is identified by the URI of the request.</summary>
Get = 1,
/// <summary>Posts a new entity as an addition to a URI.</summary>
Post = 2,
/// <summary>Replaces an entity that is identified by a URI.</summary>
Put = 4,
/// <summary>Requests that a specified URI be deleted.</summary>
Delete = 8,
/// <summary>Retrieves the message headers for the information or entity that is identified by the URI of the request.</summary>
Head = 0x10,
/// <summary>Requests that a set of changes described in the request entity be applied to the resource identified by the Request-URI.</summary>
Patch = 0x20,
/// <summary>Represents a request for information about the communication options available on the request/response chain identified by the Request-URI.</summary>
Options = 0x40
}
}
";
/// <summary>
/// If the tested source code starts with SystemWebMvcNamespaceCSharp, we can add offsets when specifying location line numbers,
/// and not break everything when adding to SystemWebMvcNamespaceCSharp.
/// </summary>
private static readonly int SystemWebMvcNamespaceCSharpLineCount = SystemWebMvcNamespaceCSharp.Where(ch => ch == '\n').Count();
/// <summary>
/// Types in the System.Web.Mvc namespace that the analyzer looks for, so we don't have to reference ASP.NET MVC bits.
/// </summary>
private const string SystemWebMvcNamespaceBasic = @"
Imports System
Namespace System.Web.Mvc
Public Class Controller
End Class
Public Class ControllerBase
End Class
Public Class ActionResult
End Class
Public Class ContentResult
Inherits ActionResult
End Class
Public Class ValidateAntiForgeryTokenAttribute
Inherits Attribute
End Class
Public Class HttpGetAttribute
Inherits Attribute
End Class
Public Class HttpPostAttribute
Inherits Attribute
End Class
Public Class HttpPutAttribute
Inherits Attribute
End Class
Public Class HttpDeleteAttribute
Inherits Attribute
End Class
Public Class HttpPatchAttribute
Inherits Attribute
End Class
Public Class AcceptVerbsAttribute
Inherits Attribute
Public Sub New(ByVal ParamArray verbs() As String)
End Sub
Public Sub New(ByVal verbs As HttpVerbs)
End Sub
End Class
Public Class NonActionAttribute
Inherits Attribute
End Class
Public Class ChildActionOnlyAttribute
Inherits Attribute
End Class
<Flags>
Public Enum HttpVerbs
[Get] = 1
Post = 2
Put = 4
Delete = 8
Head = 16
Patch = 32
Options = 64
End Enum
End Namespace
";
/// <summary>
/// If the tested source code starts with SystemWebMvcNamespaceBasic, we can add offsets when specifying location line numbers,
/// and not break everything when adding to SystemWebMvcNamespaceBasic.
/// </summary>
private static readonly int SystemWebMvcNamespaceBasicLineCount = SystemWebMvcNamespaceBasic.Where(ch => ch == '\n').Count();
[Fact]
public async Task HaveAcceptStringPutAndToken_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
[AcceptVerbs(""Put"")]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task HaveAcceptStringPutAndToken_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.AcceptVerbs(""Put"")>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByRef input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task ReturnsNonActionResult_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
public string DoSomething(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task HttpGet_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
[HttpGet]
public ActionResult DoSomething(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task ReturnsNonActionResult_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
Public Function DoSomething(ByVal input As String) As String
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task HaveAcceptEnumPutAndToken_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
[AcceptVerbs(HttpVerbs.Post)]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task HaveAcceptEnumPutAndToken_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.AcceptVerbs(System.Web.Mvc.HttpVerbs.Post)>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByRef input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task NotAController_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class NotAController
{
public ContentResult DoSomethingElse()
{
return null;
}
}
}"
);
}
[Fact]
public async Task NotAController_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class NotAController
Public Function DoSomething() As System.Web.Mvc.ContentResult
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task NotAnAction_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class AController : Controller
{
[NonAction]
public ContentResult DoSomethingElse()
{
return null;
}
}
}"
);
}
[Fact]
public async Task NotAnAction_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.NonAction>
Public Function DoSomething() As System.Web.Mvc.ContentResult
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task HaveHttpPostAndToken_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult DoSomething(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task HaveHttpPostAndToken_Basic_NoDiagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.HttpPost>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace"
);
}
[Fact]
public async Task MissingVerbsAndToken_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
public ActionResult DoSomething(string input)
{
return null;
}
}
}
",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 8, 29, "DoSomething"));
}
[Fact]
public async Task MissingVerbsAndToken_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicNoVerbsNoToken(SystemWebMvcNamespaceBasicLineCount + 6, 25, "DoSomething"));
}
[Fact]
public async Task HttpGetAndToken_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : ControllerBase
{
[HttpGet]
[ValidateAntiForgeryToken]
public ActionResult DoSomething()
{
return new ContentResult();
}
}
}
",
GetCA3147CSharpGetAndToken(SystemWebMvcNamespaceCSharpLineCount + 10, 29, "DoSomething"));
}
[Fact]
public async Task HttpGetAndToken_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.HttpGet>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicGetAndToken(SystemWebMvcNamespaceBasicLineCount + 8, 25, "DoSomething"));
}
[Fact]
public async Task AcceptGetPostAndToken_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : Controller
{
[AcceptVerbs(HttpVerbs.Post | HttpVerbs.Get)]
[ValidateAntiForgeryToken]
public ContentResult DoSomething()
{
return new ContentResult();
}
}
}",
GetCA3147CSharpGetAndToken(SystemWebMvcNamespaceCSharpLineCount + 10, 30, "DoSomething"),
GetCA3147CSharpGetAndOtherToken(SystemWebMvcNamespaceCSharpLineCount + 10, 30, "DoSomething"));
}
[Fact]
public async Task AcceptGetPostAndToken_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.AcceptVerbs(System.Web.Mvc.HttpVerbs.Post Or System.Web.Mvc.HttpVerbs.Get)>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicGetAndToken(SystemWebMvcNamespaceBasicLineCount + 8, 25, "DoSomething"),
GetCA3147BasicGetAndOtherToken(SystemWebMvcNamespaceBasicLineCount + 8, 25, "DoSomething"));
}
[Fact]
public async Task HttpGetHttpPutAndToken_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class ApiController : ControllerBase
{
[HttpGet]
[HttpPut]
[ValidateAntiForgeryToken]
public ActionResult DoSomething()
{
return new ContentResult();
}
}
}
",
GetCA3147CSharpGetAndToken(SystemWebMvcNamespaceCSharpLineCount + 11, 29, "DoSomething"),
GetCA3147CSharpGetAndOtherToken(SystemWebMvcNamespaceCSharpLineCount + 11, 29, "DoSomething"));
}
[Fact]
public async Task HttpGetHttpPutAndToken_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.HttpGet>
<System.Web.Mvc.HttpPost>
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicGetAndToken(SystemWebMvcNamespaceBasicLineCount + 9, 25, "DoSomething"),
GetCA3147BasicGetAndOtherToken(SystemWebMvcNamespaceBasicLineCount + 9, 25, "DoSomething"));
}
[Fact]
public async Task TokenWithoutVerbs_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class TokenWithVerbsController : Controller
{
[ValidateAntiForgeryToken]
public ActionResult DoSomething()
{
return null;
}
}
}
",
GetCA3147CSharpNoVerbs(SystemWebMvcNamespaceCSharpLineCount + 9, 29, "DoSomething"));
}
[Fact]
public async Task TokenWithoutVerbs_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
<System.Web.Mvc.ValidateAntiForgeryToken>
Public Function DoSomething(ByVal input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicNoVerbs(SystemWebMvcNamespaceBasicLineCount + 7, 25, "DoSomething"));
}
[Fact]
public async Task AcceptVerbsNoToken_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Web.Mvc;
public class AcceptVerbsNoTokenController : Controller
{
private const HttpVerbs AllowedVerbs = HttpVerbs.Post | HttpVerbs.Put;
[AcceptVerbs(AllowedVerbs)]
public ActionResult DoSomething()
{
return null;
}
}
}
",
GetCA3147CSharpVerbsAndNoToken(SystemWebMvcNamespaceCSharpLineCount + 11, 29, "DoSomething"));
}
[Fact]
public async Task AcceptVerbsNoToken_Basic_Diagnostic()
{
await VerifyVB.VerifyAnalyzerAsync(SystemWebMvcNamespaceBasic + @"
Namespace Blah
Public Class ApiController
Inherits System.Web.Mvc.Controller
Private Const AllowedVerbs As System.Web.Mvc.HttpVerbs = System.Web.Mvc.HttpVerbs.Post Or System.Web.Mvc.HttpVerbs.Put
<System.Web.Mvc.AcceptVerbs(AllowedVerbs)>
Public Function DoSomething(ByRef input As String) As System.Web.Mvc.ActionResult
Return Nothing
End Function
End Class
End Namespace",
GetCA3147BasicVerbsAndNoToken(SystemWebMvcNamespaceBasicLineCount + 9, 25, "DoSomething"));
}
[Fact]
public async Task DocumentationViolationPostExample_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace TestNamespace
{
using System.Web.Mvc;
public class TestController : Controller
{
public ActionResult TransferMoney(string toAccount, string amount)
{
// You don't want an attacker specify to who and how much money to transfer.
return null;
}
}
}",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 8, 29, "TransferMoney"));
}
[Fact]
public async Task DocumentationFixPostExample_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace TestNamespace
{
using System.Web.Mvc;
public class TestController : Controller
{
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult TransferMoney(string toAccount, string amount)
{
return null;
}
}
}"
);
}
[Fact]
public async Task DocumentationViolationGetExample_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace TestNamespace
{
using System.Web.Mvc;
public class TestController : Controller
{
public ActionResult Help(int topicId)
{
// This Help method is an example of a read-only operation with no harmful side effects.
return null;
}
}
}",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 8, 29, "Help"));
}
[Fact]
public async Task DocumentationFixGetExample_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace TestNamespace
{
using System.Web.Mvc;
public class TestController : Controller
{
[HttpGet]
public ActionResult Help(int topicId)
{
return null;
}
}
}"
);
}
[Fact]
public async Task MissingVerbsAndTokenAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class ApiController : Controller
{
public async Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}
",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 9, 41, "DoSomethingAsync"));
}
[Fact]
public async Task AcceptVerbsNoTokenAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class AcceptVerbsNoTokenController : Controller
{
private const HttpVerbs AllowedVerbs = HttpVerbs.Post | HttpVerbs.Put;
[AcceptVerbs(AllowedVerbs)]
public async Task<ActionResult> DoSomethingAsync()
{
return null;
}
}
}
",
GetCA3147CSharpVerbsAndNoToken(SystemWebMvcNamespaceCSharpLineCount + 12, 41, "DoSomethingAsync"));
}
[Fact]
public async Task HaveAcceptStringPutAndTokenAsync_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class ApiController : Controller
{
[AcceptVerbs(""Put"")]
[ValidateAntiForgeryToken]
public async Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}"
);
}
[Fact]
public async Task MissingVerbsAndTokenTaskButNotAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class ApiController : Controller
{
public Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}
",
GetCA3147CSharpNoVerbsNoToken(SystemWebMvcNamespaceCSharpLineCount + 9, 35, "DoSomethingAsync"));
}
[Fact]
public async Task AcceptVerbsNoTokenTaskButNotAsync_CSharp_Diagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class AcceptVerbsNoTokenController : Controller
{
private const HttpVerbs AllowedVerbs = HttpVerbs.Post | HttpVerbs.Put;
[AcceptVerbs(AllowedVerbs)]
public Task<ActionResult> DoSomethingAsync()
{
return null;
}
}
}
",
GetCA3147CSharpVerbsAndNoToken(SystemWebMvcNamespaceCSharpLineCount + 12, 35, "DoSomethingAsync"));
}
[Fact]
public async Task HaveAcceptStringPutAndTokenTaskButNotAsync_CSharp_NoDiagnostic()
{
await VerifyCS.VerifyAnalyzerAsync(SystemWebMvcNamespaceCSharp + @"
namespace Blah
{
using System.Threading.Tasks;
using System.Web.Mvc;
public class ApiController : Controller
{
[AcceptVerbs(""Put"")]
[ValidateAntiForgeryToken]
public Task<ActionResult> DoSomethingAsync(string input)
{
return null;
}
}
}"
);
}
}
}