@@ -138,36 +138,18 @@ private void ValidateSplitChallenge(string[] splitChallenge)
138
138
}
139
139
140
140
_requestContext . Logger . Verbose ( ( ) => $ "[Managed Identity] Challenge is valid. FilePath: { splitChallenge [ 1 ] } ") ;
141
+ string path = Path . GetFullPath ( new Uri ( splitChallenge [ 1 ] ) . LocalPath )
142
+ . TrimEnd ( Path . DirectorySeparatorChar , Path . AltDirectorySeparatorChar ) ;
141
143
142
- if ( DesktopOsHelper . IsWindows ( ) )
143
- {
144
- if ( ! IsValidWindowsPath ( splitChallenge [ 1 ] ) )
145
- {
146
- throw CreateManagedIdentityException (
147
- MsalError . ManagedIdentityRequestFailed ,
148
- MsalErrorMessage . ManagedIdentityInvalidFile ) ;
149
- }
150
-
151
- _requestContext . Logger . Verbose ( ( ) => "[Managed Identity] Windows path is valid." ) ;
152
- }
153
- else if ( DesktopOsHelper . IsLinux ( ) )
154
- {
155
- if ( ! IsValidLinuxPath ( splitChallenge [ 1 ] ) )
156
- {
157
- throw CreateManagedIdentityException (
158
- MsalError . ManagedIdentityRequestFailed ,
159
- MsalErrorMessage . ManagedIdentityInvalidFile ) ;
160
- }
161
-
162
- _requestContext . Logger . Verbose ( ( ) => "[Managed Identity] Linux path is valid." ) ;
163
- }
164
- else
144
+ if ( ! IsValidPath ( splitChallenge [ 1 ] ) )
165
145
{
166
146
throw CreateManagedIdentityException (
167
147
MsalError . ManagedIdentityRequestFailed ,
168
- MsalErrorMessage . ManagedIdentityPlatformNotSupported ) ;
148
+ MsalErrorMessage . ManagedIdentityInvalidFile ) ;
169
149
}
170
150
151
+ _requestContext . Logger . Verbose ( ( ) => $ "[Managed Identity] File path is valid. Path: { path } ") ;
152
+
171
153
var length = new FileInfo ( splitChallenge [ 1 ] ) . Length ;
172
154
173
155
if ( ( ! File . Exists ( splitChallenge [ 1 ] ) || ( length ) > 4096 ) )
@@ -191,19 +173,28 @@ private MsalException CreateManagedIdentityException(string errorCode, string er
191
173
null ) ;
192
174
}
193
175
194
- private bool IsValidLinuxPath ( string path )
176
+ private bool IsValidPath ( string path )
195
177
{
196
- string linuxPath = "/var/opt/azcmagent/tokens/" ;
178
+ string expectedFilePath ;
197
179
198
- return path . StartsWith ( linuxPath , StringComparison . OrdinalIgnoreCase ) &&
199
- path . EndsWith ( ".key" , StringComparison . OrdinalIgnoreCase ) ;
200
- }
180
+ if ( DesktopOsHelper . IsWindows ( ) )
181
+ {
182
+ string expandedExpectedPath = Environment . ExpandEnvironmentVariables ( "%ProgramData% \\ AzureConnectedMachineAgent \\ Tokens \\ " ) ;
201
183
202
- private bool IsValidWindowsPath ( string path )
203
- {
204
- string expandedExpectedPath = Environment . ExpandEnvironmentVariables ( "%ProgramData%\\ AzureConnectedMachineAgent\\ Tokens\\ " ) ;
184
+ expectedFilePath = expandedExpectedPath + Path . GetFileName ( path ) ;
185
+ }
186
+ else if ( DesktopOsHelper . IsLinux ( ) )
187
+ {
188
+ expectedFilePath = "/var/opt/azcmagent/tokens/" + Path . GetFileName ( path ) ;
189
+ }
190
+ else
191
+ {
192
+ throw CreateManagedIdentityException (
193
+ MsalError . ManagedIdentityRequestFailed ,
194
+ MsalErrorMessage . ManagedIdentityPlatformNotSupported ) ;
195
+ }
205
196
206
- return path . StartsWith ( expandedExpectedPath , StringComparison . OrdinalIgnoreCase ) &&
197
+ return path . Equals ( expectedFilePath , StringComparison . OrdinalIgnoreCase ) &&
207
198
path . EndsWith ( ".key" , StringComparison . OrdinalIgnoreCase ) ;
208
199
}
209
200
}
0 commit comments