1
+ // Copyright (c) .NET Foundation. All rights reserved.
2
+ // Licensed under the MIT License. See License.txt in the project root for license information.
3
+
4
+ using System ;
5
+ using System . Collections . Generic ;
6
+ using Newtonsoft . Json . Linq ;
7
+ using Xunit ;
8
+
9
+ namespace Microsoft . Azure . WebJobs . Script . Tests
10
+ {
11
+ public sealed class MetadataJsonHelperTests
12
+ {
13
+ [ Fact ]
14
+ public void CreateJObjectWithSanitizedPropertyValue_NullJsonObject_ThrowsArgumentNullException ( )
15
+ {
16
+ JObject jsonObject = null ;
17
+ var propertyNames = new [ ] { "sensitiveProperty" } ;
18
+
19
+ Assert . Throws < ArgumentNullException > ( ( ) => MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( jsonObject , propertyNames ) ) ;
20
+ }
21
+
22
+ [ Fact ]
23
+ public void CreateJObjectWithSanitizedPropertyValue_NullPropertyNames_ThrowsArgumentNullException ( )
24
+ {
25
+ var jsonObject = new JObject ( ) ;
26
+ IEnumerable < string > propertyNames = null ;
27
+
28
+ Assert . Throws < ArgumentNullException > ( ( ) => MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( jsonObject , propertyNames ) ) ;
29
+ }
30
+
31
+ [ Fact ]
32
+ public void CreateJObjectWithSanitizedPropertyValue_ValidInput_SanitizesMatchingProperties ( )
33
+ {
34
+ var jsonObject = new JObject
35
+ {
36
+ { "sensitiveProperty1" , "AccountKey=foo" } ,
37
+ { "sensitiveProperty2" , "MyConnection" } ,
38
+ { "SENSITIVEPROPERTY3" , "AccountKey=bar" } ,
39
+ { "otherProperty" , "value2" }
40
+ } ;
41
+ var sensitiveBindingPropertyNames = new [ ] { "sensitiveProperty1" , "sensitiveproperty2" , "sensitiveproperty3" } ;
42
+
43
+ var result = MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( jsonObject , sensitiveBindingPropertyNames ) ;
44
+
45
+ Assert . Equal ( "[Hidden Credential]" , result [ "sensitiveProperty1" ] . ToString ( ) ) ;
46
+ Assert . Equal ( "MyConnection" , result [ "sensitiveProperty2" ] . ToString ( ) ) ;
47
+ Assert . Equal ( "[Hidden Credential]" , result [ "SENSITIVEPROPERTY3" ] . ToString ( ) ) ;
48
+ Assert . Equal ( "value2" , result [ "otherProperty" ] . ToString ( ) ) ;
49
+ }
50
+
51
+ [ Fact ]
52
+ public void CreateJObjectWithSanitizedPropertyValue_NoMatchingProperties_DoesNotSanitize ( )
53
+ {
54
+ var jsonObject = new JObject
55
+ {
56
+ { "otherProperty1" , "value1" } ,
57
+ { "otherProperty2" , "value2" } ,
58
+ { "otherProperty3" , "AccountKey=foo" }
59
+ } ;
60
+ var sensitiveBindingPropertyNames = new [ ] { "sensitiveProperty" } ;
61
+
62
+ var result = MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( jsonObject , sensitiveBindingPropertyNames ) ;
63
+
64
+ Assert . Equal ( "value1" , result [ "otherProperty1" ] . ToString ( ) ) ;
65
+ Assert . Equal ( "value2" , result [ "otherProperty2" ] . ToString ( ) ) ;
66
+ Assert . Equal ( "AccountKey=foo" , result [ "otherProperty3" ] . ToString ( ) ) ;
67
+ }
68
+
69
+ [ Fact ]
70
+ public void CreateJObjectWithSanitizedPropertyValue_StringInput_NullOrEmptyJson_ThrowsArgumentException ( )
71
+ {
72
+ string json = null ;
73
+ var propertyNames = new [ ] { "sensitiveProperty" } ;
74
+
75
+ Assert . Throws < ArgumentException > ( ( ) => MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( json , propertyNames ) ) ;
76
+ }
77
+
78
+ [ Fact ]
79
+ public void CreateJObjectWithSanitizedPropertyValue_StringInput_InvalidJson_ThrowsJsonReaderException ( )
80
+ {
81
+ var json = "invalid json" ;
82
+ var propertyNames = new [ ] { "sensitiveProperty" } ;
83
+
84
+ Assert . Throws < Newtonsoft . Json . JsonReaderException > ( ( ) => MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( json , propertyNames ) ) ;
85
+ }
86
+
87
+ [ Fact ]
88
+ public void CreateJObjectWithSanitizedPropertyValue_StringInput_ValidJson_SanitizesMatchingProperties ( )
89
+ {
90
+ var json = "{ \" SensitiveProperty\" : \" pwd=12345\" , \" otherProperty\" : \" value2\" }" ;
91
+ var propertyNames = new [ ] { "sensitiveproperty" } ;
92
+
93
+ var result = MetadataJsonHelper . CreateJObjectWithSanitizedPropertyValue ( json , propertyNames ) ;
94
+
95
+ Assert . Equal ( "[Hidden Credential]" , result [ "SensitiveProperty" ] . ToString ( ) ) ;
96
+ Assert . Equal ( "value2" , result [ "otherProperty" ] . ToString ( ) ) ;
97
+ }
98
+ }
99
+ }
0 commit comments