OLD | NEW |
| (Empty) |
1 /// Copyright (c) Microsoft Corporation. All rights reserved. | |
2 | |
3 using System; | |
4 using System.Globalization; | |
5 using System.IO; | |
6 using Microsoft.VisualStudio.Build.ComInteropWrapper; | |
7 | |
8 namespace Microsoft.VisualStudio.Project | |
9 { | |
10 /// <summary> | |
11 /// Does security validation of a user project before loading the projec
t. | |
12 /// </summary> | |
13 public class UserProjectSecurityChecker : ProjectSecurityChecker | |
14 { | |
15 #region fields | |
16 /// <summary> | |
17 /// The project shim for the main project file. | |
18 /// We need this otherwise the msbuild API cannot check the user
file. | |
19 /// </summary> | |
20 private ProjectShim mainProjectShim; | |
21 | |
22 #endregion | |
23 | |
24 #region ctors | |
25 /// <summary> | |
26 /// Overloaded Constructor | |
27 /// </summary> | |
28 /// <param name="projectFilePath">path to the project file</para
m> | |
29 /// <param name="serviceProvider">A service provider.</param> | |
30 public UserProjectSecurityChecker(IServiceProvider serviceProvid
er, string projectFilePath) : | |
31 base(serviceProvider, projectFilePath) | |
32 { | |
33 } | |
34 #endregion | |
35 | |
36 #region properties | |
37 /// <summary> | |
38 /// The main projects' shim. | |
39 /// </summary> | |
40 internal protected ProjectShim MainProjectShim | |
41 { | |
42 get | |
43 { | |
44 return this.mainProjectShim; | |
45 } | |
46 internal set | |
47 { | |
48 this.mainProjectShim = value; | |
49 } | |
50 } | |
51 #endregion | |
52 | |
53 #region overridden method | |
54 /// <summary> | |
55 /// Checks if the user file is safe with imports. If it has then
the user file is considered unsafe. | |
56 /// </summary> | |
57 /// <param name="securityErrorMessage">At return describes the r
eason why the projects is not considered safe.</param> | |
58 /// <returns>true if the user project is safe regarding imports.
</returns> | |
59 protected override bool IsProjectSafeWithImports(out string secu
rityErrorMessage) | |
60 { | |
61 securityErrorMessage = String.Empty; | |
62 | |
63 string[] directImports = this.SecurityCheckHelper.GetDir
ectlyImportedProjects(this.ProjectShim); | |
64 | |
65 if(directImports != null && directImports.Length > 0) | |
66 { | |
67 securityErrorMessage = String.Format(CultureInfo
.CurrentCulture, SR.GetString(SR.DetailsUserImport, CultureInfo.CurrentUICulture
), Path.GetFileName(this.ProjectShim.FullFileName), directImports[0]); | |
68 return false; | |
69 } | |
70 | |
71 return true; | |
72 } | |
73 | |
74 /// <summary> | |
75 /// Checks if the project is safe regarding properties. | |
76 /// </summary> | |
77 /// <param name="securityErrorMessage">At return describes the r
eason why the projects is not considered safe.</param> | |
78 /// <returns>true if the project has only safe properties.</retu
rns> | |
79 protected override bool IsProjectSafeWithProperties(out string s
ecurityErrorMessage) | |
80 { | |
81 securityErrorMessage = String.Empty; | |
82 | |
83 // Now ask the security check heper for the safe propert
ies. | |
84 string reasonForFailure; | |
85 bool isUserFile; | |
86 bool isProjectSafe = this.SecurityCheckHelper.IsProjectS
afe(ProjectSecurityChecker.DangerousPropertyProperty, | |
87
ProjectSecurityC
hecker.DefaultDangerousProperties, | |
88
this.mainProject
Shim, | |
89
this.ProjectShim
, | |
90
SecurityCheckPas
s.Properties, | |
91
out reasonForFai
lure, | |
92
out isUserFile); | |
93 | |
94 // Main project gets precedence over the user project. | |
95 // Do not report that since this is only for the user fi
le. | |
96 if(!isUserFile) | |
97 { | |
98 return true; | |
99 } | |
100 | |
101 if(!isProjectSafe) | |
102 { | |
103 securityErrorMessage = this.GetMessageString(rea
sonForFailure, SR.DetailsProperty); | |
104 } | |
105 | |
106 return isProjectSafe; | |
107 } | |
108 | |
109 /// <summary> | |
110 /// Checks if the project is safe regarding targets. | |
111 /// </summary> | |
112 /// <param name="securityErrorMessage">At return describes the r
eason why the projects is not considered safe.</param> | |
113 /// <returns>true if the project has only safe targets.</returns
> | |
114 protected override bool IsProjectSafeWithTargets(out string secu
rityErrorMessage) | |
115 { | |
116 securityErrorMessage = String.Empty; | |
117 | |
118 // Now ask the security check heper for the safe targets
. | |
119 string reasonForFailure; | |
120 bool isUserFile; | |
121 bool isProjectSafe = this.SecurityCheckHelper.IsProjectS
afe(ProjectSecurityChecker.DangerousTargetProperty, | |
122
ProjectSecurityC
hecker.DefaultDangerousTargets, | |
123
this.mainProject
Shim, | |
124
this.ProjectShim
, | |
125
SecurityCheckPas
s.Targets, | |
126
out reasonForFai
lure, | |
127
out isUserFile); | |
128 | |
129 // Main project gets precedence over the user project. | |
130 // Do not report that since this is only for the user fi
le. | |
131 if(!isUserFile) | |
132 { | |
133 return true; | |
134 } | |
135 | |
136 if(!isProjectSafe) | |
137 { | |
138 securityErrorMessage = this.GetMessageString(rea
sonForFailure, SR.DetailsTarget); | |
139 } | |
140 | |
141 return isProjectSafe; | |
142 } | |
143 | |
144 /// <summary> | |
145 /// Checks if the project is safe regarding items. | |
146 /// </summary> | |
147 /// <param name="securityErrorMessage">At return describes the r
eason why the projects is not considered safe.</param> | |
148 /// <returns>true if the project has only safe items.</returns> | |
149 protected override bool IsProjectSafeWithItems(out string securi
tyErrorMessage) | |
150 { | |
151 securityErrorMessage = String.Empty; | |
152 | |
153 // Now ask the security check heper for the safe items. | |
154 string reasonForFailure; | |
155 bool isUserFile; | |
156 | |
157 bool isProjectSafe = this.SecurityCheckHelper.IsProjectS
afe(ProjectSecurityChecker.DangerousItemsProperty, | |
158
ProjectSecurityC
hecker.DefaultDangerousItems, | |
159
this.mainProject
Shim, | |
160
this.ProjectShim
, | |
161
SecurityCheckPas
s.Items, | |
162
out reasonForFai
lure, | |
163
out isUserFile); | |
164 | |
165 // Main project gets precedence over the user project. | |
166 // Do not report that since this is only for the user fi
le. | |
167 if(!isUserFile) | |
168 { | |
169 return true; | |
170 } | |
171 | |
172 if(!isProjectSafe) | |
173 { | |
174 securityErrorMessage = this.GetMessageString(rea
sonForFailure, SR.DetailsItem); | |
175 } | |
176 | |
177 return isProjectSafe; | |
178 } | |
179 #endregion | |
180 } | |
181 } | |
OLD | NEW |