@@ -150,4 +150,157 @@ describe("Getting AzLogin PS script", () => {
150150 } ) ;
151151 } ) ;
152152
153+ const INJECT_RAW = "abc' ; Start-Process calc ; $x='" ;
154+ const INJECT_ESCAPED = "abc'' ; Start-Process calc ; $x=''" ;
155+
156+ test ( 'SECURITY: tenant-id single quote is escaped (SP+secret path)' , ( ) => {
157+ setEnv ( 'environment' , 'azurecloud' ) ;
158+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
159+ setEnv ( 'allow-no-subscriptions' , 'true' ) ;
160+ setEnv ( 'auth-type' , 'SERVICE_PRINCIPAL' ) ;
161+ let creds = {
162+ 'clientId' : 'client-id' ,
163+ 'clientSecret' : 'client-secret' ,
164+ 'tenantId' : INJECT_RAW ,
165+ 'subscriptionId' : 'subscription-id'
166+ }
167+ setEnv ( 'creds' , JSON . stringify ( creds ) ) ;
168+
169+ let loginConfig = new LoginConfig ( ) ;
170+ loginConfig . initialize ( ) ;
171+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
172+ expect ( loginScript ) . toContain ( `-Tenant '${ INJECT_ESCAPED } '` ) ;
173+ expect ( loginScript ) . not . toContain ( `-Tenant '${ INJECT_RAW } '` ) ;
174+ } ) ;
175+ } ) ;
176+
177+ test ( 'SECURITY: subscription-id single quote is escaped (SP+secret path)' , ( ) => {
178+ setEnv ( 'environment' , 'azurecloud' ) ;
179+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
180+ setEnv ( 'allow-no-subscriptions' , 'true' ) ;
181+ setEnv ( 'auth-type' , 'SERVICE_PRINCIPAL' ) ;
182+ let creds = {
183+ 'clientId' : 'client-id' ,
184+ 'clientSecret' : 'client-secret' ,
185+ 'tenantId' : 'tenant-id' ,
186+ 'subscriptionId' : INJECT_RAW
187+ }
188+ setEnv ( 'creds' , JSON . stringify ( creds ) ) ;
189+
190+ let loginConfig = new LoginConfig ( ) ;
191+ loginConfig . initialize ( ) ;
192+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
193+ expect ( loginScript ) . toContain ( `-Subscription '${ INJECT_ESCAPED } '` ) ;
194+ expect ( loginScript ) . not . toContain ( `-Subscription '${ INJECT_RAW } '` ) ;
195+ } ) ;
196+ } ) ;
197+
198+ test ( 'SECURITY: client-id single quote is escaped (SP+secret path, PSCredential)' , ( ) => {
199+ setEnv ( 'environment' , 'azurecloud' ) ;
200+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
201+ setEnv ( 'allow-no-subscriptions' , 'true' ) ;
202+ setEnv ( 'auth-type' , 'SERVICE_PRINCIPAL' ) ;
203+ let creds = {
204+ 'clientId' : INJECT_RAW ,
205+ 'clientSecret' : 'client-secret' ,
206+ 'tenantId' : 'tenant-id' ,
207+ 'subscriptionId' : 'subscription-id'
208+ }
209+ setEnv ( 'creds' , JSON . stringify ( creds ) ) ;
210+
211+ let loginConfig = new LoginConfig ( ) ;
212+ loginConfig . initialize ( ) ;
213+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
214+ expect ( loginScript ) . toContain ( `New-Object System.Management.Automation.PSCredential('${ INJECT_ESCAPED } ',` ) ;
215+ expect ( loginScript ) . not . toContain ( `New-Object System.Management.Automation.PSCredential('${ INJECT_RAW } ',` ) ;
216+ } ) ;
217+ } ) ;
218+
219+ test ( 'SECURITY: client-id single quote is escaped (OIDC path)' , ( ) => {
220+ setEnv ( 'environment' , 'azurecloud' ) ;
221+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
222+ setEnv ( 'allow-no-subscriptions' , 'false' ) ;
223+ setEnv ( 'tenant-id' , 'tenant-id' ) ;
224+ setEnv ( 'subscription-id' , 'subscription-id' ) ;
225+ setEnv ( 'client-id' , INJECT_RAW ) ;
226+ setEnv ( 'auth-type' , 'SERVICE_PRINCIPAL' ) ;
227+
228+ let loginConfig = new LoginConfig ( ) ;
229+ loginConfig . initialize ( ) ;
230+ jest . spyOn ( loginConfig , 'getFederatedToken' ) . mockImplementation ( async ( ) => { loginConfig . federatedToken = "fake-token" ; } ) ;
231+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
232+ expect ( loginScript ) . toContain ( `-ApplicationId '${ INJECT_ESCAPED } '` ) ;
233+ expect ( loginScript ) . not . toContain ( `-ApplicationId '${ INJECT_RAW } '` ) ;
234+ } ) ;
235+ } ) ;
236+
237+ test ( 'SECURITY: client-id single quote is escaped (user-assigned MI path)' , ( ) => {
238+ setEnv ( 'environment' , 'azurecloud' ) ;
239+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
240+ setEnv ( 'allow-no-subscriptions' , 'true' ) ;
241+ setEnv ( 'auth-type' , 'IDENTITY' ) ;
242+ setEnv ( 'client-id' , INJECT_RAW ) ;
243+
244+ let loginConfig = new LoginConfig ( ) ;
245+ loginConfig . initialize ( ) ;
246+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
247+ expect ( loginScript ) . toContain ( `-AccountId '${ INJECT_ESCAPED } '` ) ;
248+ expect ( loginScript ) . not . toContain ( `-AccountId '${ INJECT_RAW } '` ) ;
249+ } ) ;
250+ } ) ;
251+
252+ test ( 'SECURITY: tenant-id and subscription-id single quotes are escaped (system-assigned MI path)' , ( ) => {
253+ setEnv ( 'environment' , 'azurecloud' ) ;
254+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
255+ setEnv ( 'allow-no-subscriptions' , 'false' ) ;
256+ setEnv ( 'tenant-id' , INJECT_RAW ) ;
257+ setEnv ( 'subscription-id' , INJECT_RAW ) ;
258+ setEnv ( 'auth-type' , 'IDENTITY' ) ;
259+
260+ let loginConfig = new LoginConfig ( ) ;
261+ loginConfig . initialize ( ) ;
262+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
263+ expect ( loginScript ) . toContain ( `-Tenant '${ INJECT_ESCAPED } '` ) ;
264+ expect ( loginScript ) . toContain ( `-Subscription '${ INJECT_ESCAPED } '` ) ;
265+ expect ( loginScript ) . not . toContain ( `-Tenant '${ INJECT_RAW } '` ) ;
266+ expect ( loginScript ) . not . toContain ( `-Subscription '${ INJECT_RAW } '` ) ;
267+ } ) ;
268+ } ) ;
269+
270+ test ( 'SECURITY: resourceManagerEndpointUrl single quote is escaped (AzureStack path)' , ( ) => {
271+ setEnv ( 'environment' , 'azurestack' ) ;
272+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
273+ setEnv ( 'allow-no-subscriptions' , 'true' ) ;
274+ setEnv ( 'auth-type' , 'SERVICE_PRINCIPAL' ) ;
275+ let creds = {
276+ 'clientId' : 'client-id' ,
277+ 'clientSecret' : 'client-secret' ,
278+ 'tenantId' : 'tenant-id' ,
279+ 'subscriptionId' : 'subscription-id' ,
280+ 'resourceManagerEndpointUrl' : INJECT_RAW
281+ }
282+ setEnv ( 'creds' , JSON . stringify ( creds ) ) ;
283+
284+ let loginConfig = new LoginConfig ( ) ;
285+ loginConfig . initialize ( ) ;
286+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ _ , loginScript ] ) => {
287+ expect ( loginScript ) . toContain ( `-ARMEndpoint '${ INJECT_ESCAPED } '` ) ;
288+ expect ( loginScript ) . not . toContain ( `-ARMEndpoint '${ INJECT_RAW } '` ) ;
289+ } ) ;
290+ } ) ;
291+
292+ test ( 'SECURITY: escapePSSingleQuoted handles null/undefined without throwing' , ( ) => {
293+ setEnv ( 'environment' , 'azurecloud' ) ;
294+ setEnv ( 'enable-AzPSSession' , 'true' ) ;
295+ setEnv ( 'allow-no-subscriptions' , 'false' ) ;
296+ setEnv ( 'auth-type' , 'IDENTITY' ) ;
297+
298+ let loginConfig = new LoginConfig ( ) ;
299+ loginConfig . initialize ( ) ;
300+ return AzPSSCriptBuilder . getAzPSLoginScript ( loginConfig ) . then ( ( [ loginMethod , loginScript ] ) => {
301+ expect ( loginScript ) . toContain ( "Connect-AzAccount -Identity -Environment 'azurecloud'" ) ;
302+ expect ( loginMethod ) . toBe ( 'system-assigned managed identity' ) ;
303+ } ) ;
304+ } ) ;
305+
153306} ) ;
0 commit comments