Skip to content
Navigation Menu
{{ message }}
forked from security-code-scan/security-code-scan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfiguration.cs
More file actions
809 lines (670 loc) · 33.9 KB
/
Copy pathConfiguration.cs
File metadata and controls
809 lines (670 loc) · 33.9 KB
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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Text.RegularExpressions;
using SecurityCodeScan.Analyzers.Taint;
using SecurityCodeScan.Analyzers.Utils;
namespace SecurityCodeScan.Config
{
/// <summary>
/// Internal configuration optimized for queries
/// </summary>
internal class Configuration
{
public Configuration()
{
_PasswordValidatorRequiredProperties = new HashSet<string>();
PasswordValidatorRequiredProperties = new ReadOnlyHashSet<string>(_PasswordValidatorRequiredProperties);
ConfigurationBehavior = new Dictionary<string, KeyValuePair<string, MethodBehavior>>();
Behavior = new Dictionary<string, MethodBehavior>();
_TaintEntryPoints = new HashSet<string>();
TaintEntryPoints = new ReadOnlyHashSet<string>(_TaintEntryPoints);
_CsrfGroupsList = new LinkedList<CsrfNamedGroup>();
_CsrfGroups = new Dictionary<string, LinkedListNode<CsrfNamedGroup>>();
_PasswordFields = new HashSet<string>();
PasswordFields = new ReadOnlyHashSet<string>(_PasswordFields);
_ConstantFields = new HashSet<string>();
ConstantFields = new ReadOnlyHashSet<string>(_ConstantFields);
_TaintTypeNameToBit = new Dictionary<string, ulong>(62);
}
public Configuration(Configuration config)
{
ReportAnalysisCompletion = config.ReportAnalysisCompletion;
AuditMode = config.AuditMode;
PasswordValidatorRequiredLength = config.PasswordValidatorRequiredLength;
MinimumPasswordValidatorProperties = config.MinimumPasswordValidatorProperties;
_PasswordValidatorRequiredProperties = new HashSet<string>(config.PasswordValidatorRequiredProperties);
PasswordValidatorRequiredProperties = new ReadOnlyHashSet<string>(_PasswordValidatorRequiredProperties);
ConfigurationBehavior = new Dictionary<string, KeyValuePair<string, MethodBehavior>>(config.ConfigurationBehavior);
Behavior = config.Behavior.ToDictionary();
_TaintEntryPoints = new HashSet<string>(config.TaintEntryPoints);
TaintEntryPoints = new ReadOnlyHashSet<string>(_TaintEntryPoints);
_CsrfGroupsList = new LinkedList<CsrfNamedGroup>(config.CsrfGoups);
_CsrfGroups = new Dictionary<string, LinkedListNode<CsrfNamedGroup>>(config.CsrfGoups.Count);
var node = _CsrfGroupsList.First;
while (node != null)
{
_CsrfGroups.Add(node.Value.Name, node);
node = node.Next;
}
_PasswordFields = new HashSet<string>(config.PasswordFields);
PasswordFields = new ReadOnlyHashSet<string>(_PasswordFields);
WebConfigFilesRegex = config.WebConfigFilesRegex;
_ConstantFields = new HashSet<string>(config.ConstantFields);
ConstantFields = new ReadOnlyHashSet<string>(_ConstantFields);
_TaintTypeNameToBit = new Dictionary<string, ulong>(config._TaintTypeNameToBit);
}
public Configuration(ConfigData configData) : this()
{
if (configData.TaintTypes != null)
RegisterTaintTypes(configData.TaintTypes);
ReportAnalysisCompletion = configData.ReportAnalysisCompletion ?? false;
AuditMode = configData.AuditMode ?? false;
MinimumPasswordValidatorProperties = configData.MinimumPasswordValidatorProperties ?? 0;
PasswordValidatorRequiredLength = configData.PasswordValidatorRequiredLength ?? 0;
if (configData.PasswordValidatorRequiredProperties != null)
{
foreach (var data in configData.PasswordValidatorRequiredProperties)
{
_PasswordValidatorRequiredProperties.Add(data);
}
}
foreach (var data in configData.Behavior)
{
ConfigurationBehavior[data.Key] = CreateBehavior(data.Value);
}
foreach (var source in configData.TaintEntryPoints)
{
if (source.Value?.Method?.ArgTypes != null)
throw new Exception("Taint entry point ArgTypes are not supported.");
_TaintEntryPoints.Add(MethodBehaviorHelper.GetMethodBehaviorKey(source.Value.Namespace,
source.Value.ClassName,
source.Value.Name,
source.Value?.Method?.ArgTypes));
}
foreach (var data in configData.CsrfProtection)
{
AddCsrfProtectionToConfiguration(data);
}
if (configData.PasswordFields != null)
{
foreach (var data in configData.PasswordFields)
{
_PasswordFields.Add(data.ToUpperInvariant());
}
}
if (configData.WebConfigFiles != null)
{
WebConfigFilesRegex = new Regex(configData.WebConfigFiles, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
foreach (var data in configData.ConstantFields)
{
_ConstantFields.Add(data);
}
}
public bool ReportAnalysisCompletion { get; private set; }
public bool AuditMode { get; private set; }
public int PasswordValidatorRequiredLength { get; private set; }
public int MinimumPasswordValidatorProperties { get; private set; }
private readonly HashSet<string> _PasswordValidatorRequiredProperties;
public ReadOnlyHashSet<string> PasswordValidatorRequiredProperties { get; }
private readonly HashSet<string> _TaintEntryPoints;
public ReadOnlyHashSet<string> TaintEntryPoints { get; }
private readonly HashSet<string> _PasswordFields;
public ReadOnlyHashSet<string> PasswordFields { get; }
public Regex WebConfigFilesRegex { get; private set; }
private readonly HashSet<string> _ConstantFields;
public ReadOnlyHashSet<string> ConstantFields { get; }
private Dictionary<string, ulong> _TaintTypeNameToBit;
public IReadOnlyDictionary<string, ulong> TaintTypeNameToBit => _TaintTypeNameToBit;
// is needed to have allow merging by configuration Id
private readonly Dictionary<string, KeyValuePair<string, MethodBehavior>> ConfigurationBehavior;
// once merged the configuration Id is not used: the key is method signature parts
public IReadOnlyDictionary<string, MethodBehavior> Behavior { get; private set; }
private readonly LinkedList<CsrfNamedGroup> _CsrfGroupsList; // ensure groups are exposed in the same order they were added
private readonly Dictionary<string, LinkedListNode<CsrfNamedGroup>> _CsrfGroups;
public IReadOnlyCollection<CsrfNamedGroup> CsrfGoups => (IReadOnlyCollection<CsrfNamedGroup>)_CsrfGroupsList;
public void MergeWith(ConfigData config)
{
if (config.TaintTypes != null)
RegisterTaintTypes(config.TaintTypes);
if (config.ReportAnalysisCompletion.HasValue)
ReportAnalysisCompletion = config.ReportAnalysisCompletion.Value;
if (config.AuditMode.HasValue)
AuditMode = config.AuditMode.Value;
if (config.MinimumPasswordValidatorProperties.HasValue)
MinimumPasswordValidatorProperties = config.MinimumPasswordValidatorProperties.Value;
if (config.PasswordValidatorRequiredLength.HasValue)
PasswordValidatorRequiredLength = config.PasswordValidatorRequiredLength.Value;
if (config.PasswordValidatorRequiredProperties != null)
{
foreach (var property in config.PasswordValidatorRequiredProperties)
{
_PasswordValidatorRequiredProperties.Add(property);
}
}
if (config.Behavior != null)
{
foreach (var behavior in config.Behavior)
{
if (behavior.Value == default(MethodBehaviorData))
ConfigurationBehavior.Remove(behavior.Key);
else
ConfigurationBehavior[behavior.Key] = CreateBehavior(behavior.Value);
}
}
if (config.TaintEntryPoints != null)
{
foreach (var source in config.TaintEntryPoints)
{
if (source.Value == default(TaintEntryPointData))
{
_TaintEntryPoints.Remove(source.Key);
}
else
{
if (source.Value?.Method?.ArgTypes != null)
throw new Exception("Taint entry point ArgTypes are not supported.");
_TaintEntryPoints.Add(MethodBehaviorHelper.GetMethodBehaviorKey(source.Value.Namespace,
source.Value.ClassName,
source.Value.Name,
source.Value?.Method?.ArgTypes));
}
}
}
if (config.CsrfProtection != null)
{
foreach (var data in config.CsrfProtection)
{
if (data.Class == null && data.AntiCsrfAttributes == null && data.Method == null && data.Parameter == null)
{
if (_CsrfGroups.TryGetValue(data.Name, out var node))
{
_CsrfGroupsList.Remove(node);
_CsrfGroups.Remove(data.Name);
}
}
else
AddCsrfProtectionToConfiguration(data);
}
}
if (config.PasswordFields != null)
{
foreach (var field in config.PasswordFields)
{
_PasswordFields.Add(field.ToUpperInvariant());
}
}
if (config.WebConfigFiles != null)
{
WebConfigFilesRegex = new Regex(config.WebConfigFiles, RegexOptions.IgnoreCase | RegexOptions.Compiled);
}
if (config.ConstantFields != null)
{
foreach (var field in config.ConstantFields)
{
_ConstantFields.Add(field);
}
}
}
public void PrepareForQueries()
{
// Build the Behavior optimized for queries after the merge.
Behavior = ConfigurationBehavior.Values.ToDictionary(pair => pair.Key, pair => pair.Value);
}
private void RegisterTaintTypes(List<string> typeNames)
{
var availableBit = 1ul << 3;
foreach (var registeredBit in TaintTypeNameToBit.Values)
{
if (availableBit <= registeredBit)
availableBit = registeredBit;
}
foreach (var typeName in typeNames)
{
if (typeName == "Tainted" || typeName == "Safe" || typeName == "Constant")
throw new Exception("'Tainted', 'Safe', and 'Constant' are reserved taint types and cannot be used for custom taint");
if (typeName.StartsWith("~"))
throw new Exception("Custom taint type cannot start from '~'");
if (TaintTypeNameToBit.ContainsKey(typeName))
throw new Exception("Duplicate taint type");
if (availableBit == 0ul)
throw new Exception("Max number of taint types reached");
_TaintTypeNameToBit[typeName] = availableBit;
availableBit = availableBit << 1;
}
}
private IReadOnlyList<Condition> GetPreConditions(Dictionary<object, object> ifSection,
IReadOnlyDictionary<int, PostCondition> mainPostConditions)
{
if (ifSection == null)
return null;
if (!ifSection.TryGetValue("Condition", out object value))
return null;
var configConditions = (Dictionary<object, object>)value;
if (!ifSection.TryGetValue("Then", out value))
return null;
var then = (Dictionary<object, object>)value;
var conditions = new Dictionary<int, object>(configConditions.Count);
foreach (var argument in configConditions)
{
if (!(argument.Value is Dictionary<object, object> d))
throw new Exception("Invalid precondition format");
var idx = int.Parse((string)argument.Key);
if (d.Count != 1)
throw new Exception("Only one precondition per argument is supported");
var condition = d.First();
if ((string)condition.Key != "Value")
throw new Exception("Only 'Value' preconditions are supported");
var conditionValue = (string)condition.Value;
if (int.TryParse(conditionValue, out var intVal))
conditions.Add(idx, intVal);
else
conditions.Add(idx, conditionValue);
}
return new List<Condition> { new Condition(conditions, GetPostConditions(then, mainPostConditions)) };
}
private ulong GetTaintBits(IEnumerable<object> taintTypes)
{
ulong bits = 0ul;
foreach (var type in taintTypes)
{
var taintType = (string)type;
bits |= GetTaintBits(taintType, out var negate);
if (negate)
throw new Exception("Negation in arrays is not supported");
}
if (bits == 0ul)
throw new Exception("Unknown taint type");
return bits;
}
private ulong GetTaintBits(string taintType, out bool negate)
{
negate = false;
if (taintType.StartsWith("~"))
{
negate = true;
taintType = taintType.Substring(1);
}
ulong taintBits;
switch (taintType)
{
case "Tainted":
taintBits = (ulong)VariableTaint.Tainted;
break;
case "Safe":
taintBits = (ulong)VariableTaint.Safe;
break;
case "Constant":
taintBits = (ulong)VariableTaint.Constant;
break;
default:
taintBits = TaintTypeNameToBit[taintType];
break;
}
return taintBits;
}
private IReadOnlyDictionary<int, ulong> GetArguments(IReadOnlyList<object> arguments)
{
if (arguments == null || !arguments.Any())
return null;
var outArguments = new Dictionary<int, ulong>(arguments.Count);
foreach (var argument in arguments)
{
switch (argument)
{
case string s:
{
int i;
switch (s)
{
case "This":
i = (int)ArgumentIndex.This;
break;
default:
i = int.Parse(s);
if (i < 0)
throw new Exception("Invalid argument index or name");
break;
}
outArguments.Add(i, (ulong)VariableTaint.Safe);
break;
}
case Dictionary<object, object> d when d.Count == 1:
{
var indexToTaintType = d.First();
switch (indexToTaintType.Value)
{
case string taintType:
{
var i = int.Parse((string)indexToTaintType.Key);
if (i < 0)
throw new Exception("Argument index cannot be negative");
var taintBit = TaintTypeNameToBit[taintType]; // "Tainted" is not supported
outArguments.Add(i, taintBit);
break;
}
case List<object> taintTypes:
{
ulong bits = GetTaintBits(taintTypes);
outArguments.Add(int.Parse((string)indexToTaintType.Key), bits);
break;
}
default:
throw new Exception("Unknown taint type");
}
break;
}
default:
throw new Exception("Unknown behavior argument");
}
}
return outArguments;
}
private IReadOnlyDictionary<int, InjectableArgument> GetInjectableArguments(IReadOnlyList<object> arguments)
{
if (arguments == null || !arguments.Any())
return null;
var outArguments = new Dictionary<int, InjectableArgument>(arguments.Count);
foreach (var argument in arguments)
{
switch (argument)
{
case Dictionary<object, object> d when d.Count == 1:
{
var rule = d.First();
var ruleId = (string)rule.Key;
switch (rule.Value)
{
case string idxText:
{
int idx = int.Parse(idxText);
if (idx < 0)
throw new Exception("Argument index cannot be negative");
outArguments.Add(idx, new InjectableArgument((ulong)VariableTaint.Safe, ruleId));
break;
}
case List<object> indices:
foreach (var index in indices)
{
switch (index)
{
case string idxText:
{
int idx = int.Parse(idxText);
if (idx < 0)
throw new Exception("Argument index cannot be negative");
outArguments.Add(idx, new InjectableArgument((ulong)VariableTaint.Safe, ruleId));
break;
}
case Dictionary<object, object> idxToTaints when idxToTaints.Count == 1:
{
var idxToTaint = idxToTaints.First();
var idxText = (string)idxToTaint.Key;
int idx = int.Parse(idxText);
if (idx < 0)
throw new Exception("Argument index cannot be negative");
var taintBit = GetTaintBits((string)idxToTaint.Value, out var negate);
outArguments.Add(idx, new InjectableArgument(taintBit, ruleId, negate));
break;
}
default:
throw new Exception("Unknown behavior argument");
}
}
break;
default:
throw new Exception("Unknown behavior argument");
}
break;
}
default:
throw new Exception("Unknown behavior argument");
}
}
return outArguments;
}
private IReadOnlyDictionary<int, PostCondition> GetPostConditions(IReadOnlyDictionary<object, object> arguments,
IReadOnlyDictionary<int, PostCondition> defaultPostConditions = null)
{
if (arguments == null || !arguments.Any())
return null;
var conditions = new Dictionary<int, PostCondition>(arguments.Count);
foreach (var argument in arguments)
{
var argKey = (string)argument.Key;
if (argKey == "ArgTypes" || argKey == "If" || argKey == "InjectableArguments" || argKey == "Condition")
continue;
if (!(argument.Value is Dictionary<object, object> d))
throw new Exception("Invalid postcondition format");
int idx;
switch (argKey)
{
case "Returns":
idx = (int)ArgumentIndex.Returns;
break;
case "This":
idx = (int)ArgumentIndex.This;
break;
default:
idx = int.Parse(argKey);
if (idx < 0)
throw new Exception("Invalid argument index or name");
break;
}
ulong taintBit = 0ul;
ImmutableHashSet<int> taintFromArguments = null;
foreach (var condition in d)
{
var conditionKey = (string)condition.Key;
switch (conditionKey)
{
case "Taint":
switch (condition.Value)
{
case string taintType:
taintBit = GetTaintBits(taintType, out var negate);
if (negate)
throw new Exception("Negation in postconditions is not supported");
break;
case List<object> taintTypes:
taintBit = GetTaintBits(taintTypes);
break;
}
break;
case "TaintFromArguments":
var taintFrom = (List<object>)condition.Value;
if (taintFrom != null && taintFrom.Count == 0)
{
throw new Exception("Do not specify 'TaintFromArguments' or provide at least one value");
}
var args = GetArguments(taintFrom);
if (args.Values.Any(x => x != (ulong)VariableTaint.Safe))
throw new Exception("'TaintFromArguments' supports only array of indices");
taintFromArguments = args.Keys.ToImmutableHashSet();
break;
default:
throw new Exception("Only 'Taint' and 'TaintFromArguments' are supported in postconditions");
}
}
if (defaultPostConditions != null)
{
if (taintBit == 0ul)
taintBit = defaultPostConditions[idx].Taint;
if (taintFromArguments == null)
taintFromArguments = defaultPostConditions[idx].TaintFromArguments;
}
conditions.Add(idx, new PostCondition(taintBit, taintFromArguments));
}
return conditions;
}
private InjectableArgument GetField(object value)
{
if (value == null)
return null;
switch (value)
{
case string s:
return new InjectableArgument((ulong)VariableTaint.Safe, s);
case List<object> taintTypes when taintTypes.Count == 1:
{
var types = (Dictionary<object, object>)taintTypes.First();
if (types.Count != 1)
throw new Exception("Unknown injectable argument");
var t = types.First();
var taintBits = GetTaintBits((string)t.Value, out var negate);
return new InjectableArgument(taintBits, (string)t.Key, negate);
}
default:
throw new Exception("Unknown injectable argument");
}
}
private readonly char[] Parenthesis = { '(', ')' };
private void ValidateArgTypes(string argTypes, string nameSpace, string className, string name)
{
if (argTypes == null)
return;
if (argTypes.Length == 0)
throw new Exception($"Do not specify 'ArgTypes:' in {nameSpace}.{className}.{name} to match any overload");
if (argTypes.Trim() != argTypes)
throw new Exception($"Leading or trailing white space in {nameSpace}.{className}.{name}");
if (argTypes[0] != '(' || argTypes[argTypes.Length - 1] != ')')
throw new Exception($"Invalid parenthesis in {nameSpace}.{className}.{name}");
argTypes = argTypes.Substring(1, argTypes.Length - 2);
if (argTypes.IndexOfAny(Parenthesis) != -1)
throw new Exception($"Parenthesis detected inside of 'ArgTypes:' in {nameSpace}.{className}.{name}");
if (argTypes != string.Empty)
{
foreach (var argType in argTypes.Split(new[] { ", " }, StringSplitOptions.None))
{
if (argType.Trim() != argType)
throw new Exception(
$"Leading or trailing white space in argument of {nameSpace}.{className}.{name}");
if (!argType.Contains(".") && !argType.Equals("dynamic"))
throw new Exception($"Argument type lacks namespace in {nameSpace}.{className}.{name}");
if (argType.Contains("this "))
throw new Exception($"'this' keyword should be omitted in {nameSpace}.{className}.{name}");
var arg = argType;
if (argType.Contains("params "))
arg = argType.Replace("params ", "");
if (argType.Contains("out "))
arg = argType.Replace("out ", "");
if (arg.Contains(" "))
throw new Exception($"Argument name should be omitted in {nameSpace}.{className}.{name}");
}
}
}
private void ValidateCondition(IDictionary<object, object> condition)
{
foreach (var key in condition.Keys.ToList())
{
int conditionIndex;
var val = condition[key];
if (key is int)
{
conditionIndex = (int)key;
}
else
{
if (!(key is string keyString) || !int.TryParse(keyString, out conditionIndex))
throw new Exception("Condition key must be an argument index");
// force condition to have a integer typed keys
condition.Remove(key);
condition[conditionIndex] = val;
}
if (conditionIndex < 0)
throw new Exception("Condition key must be an argument index >= 0");
if (!(val is IReadOnlyDictionary<object, object> valDict))
throw new Exception("Condition value must be a dictionary");
if (valDict.Count != 1)
throw new Exception("Condition dictionary must have a single value");
if (!valDict.TryGetValue("Value", out var conditionValue))
throw new Exception("Condition dictionary must contain 'Value'");
if (!(conditionValue is int) && !(conditionValue is bool))
{
var updatedValueDict = new Dictionary<object, object>(1);
var asStr = conditionValue as string;
if (int.TryParse(asStr, out var asInt))
{
updatedValueDict["Value"] = asInt;
}
else if (bool.TryParse(asStr, out var asBool))
{
updatedValueDict["Value"] = asBool;
}
else
{
throw new Exception("Condition value must be a integer, or boolean");
}
condition[conditionIndex] = updatedValueDict;
}
}
}
private KeyValuePair<string, MethodBehavior> CreateBehavior(object behavior)
{
var behaviorDict = (Dictionary<object, object>)behavior;
string nameSpace = null;
if (behaviorDict.TryGetValue("Namespace", out object value))
nameSpace = (string)value;
string className = null;
if (behaviorDict.TryGetValue("ClassName", out value))
className = (string)value;
string name = null;
if (behaviorDict.TryGetValue("Name", out value))
name = (string)value;
string argTypes = null;
Dictionary<object, object> ifCondition = null;
IReadOnlyList<object> injectableArguments = null;
Dictionary<object, object> condition = null;
Dictionary<object, object> method = null;
if (behaviorDict.TryGetValue("Method", out value))
{
method = (Dictionary<object, object>)value;
if (method.TryGetValue("ArgTypes", out value))
{
argTypes = (string)value;
ValidateArgTypes(argTypes, nameSpace, className, name);
}
if (method.TryGetValue("If", out value))
ifCondition = (Dictionary<object, object>)value;
if (method.TryGetValue("InjectableArguments", out value))
injectableArguments = (IReadOnlyList<object>)value;
if (method.TryGetValue("Condition", out value))
{
condition = (Dictionary<object, object>)value;
ValidateCondition(condition);
}
}
object injectable = null;
if (behaviorDict.TryGetValue("Field", out value))
{
var field = (Dictionary<object, object>)value;
if (field.TryGetValue("Injectable", out value))
injectable = value;
}
var key = MethodBehaviorHelper.GetMethodBehaviorKey(nameSpace, className, name, argTypes);
var mainPostConditions = GetPostConditions(method);
return new KeyValuePair<string, MethodBehavior>(key, new MethodBehavior(condition,
GetPreConditions(ifCondition, mainPostConditions),
mainPostConditions,
GetInjectableArguments(injectableArguments),
GetField(injectable)));
}
public void AddCsrfProtectionToConfiguration(CsrfProtectionData csrfData)
{
if (string.IsNullOrWhiteSpace(csrfData.Name))
throw new Exception($"{nameof(CsrfProtectionData.Name)} is required in CsrfProtection");
if (!_CsrfGroups.TryGetValue(csrfData.Name, out var curGroupNode))
{
var curGroup = new CsrfNamedGroup(csrfData);
var node = _CsrfGroupsList.AddLast(curGroup);
_CsrfGroups.Add(csrfData.Name, node);
}
else
{
curGroupNode.Value.AddFrom(csrfData);
}
}
}
}
You can’t perform that action at this time.
