@@ -33,7 +33,8 @@ public sealed class SessionsPythonPluginTests : IDisposable
3333 endpoint : new Uri ( "http://localhost:8888" ) )
3434 {
3535 CodeExecutionType = SessionsPythonSettings . CodeExecutionTypeSetting . Synchronous ,
36- CodeInputType = SessionsPythonSettings . CodeInputTypeSetting . Inline
36+ CodeInputType = SessionsPythonSettings . CodeInputTypeSetting . Inline ,
37+ AllowedDomains = [ "localhost" ]
3738 } ;
3839
3940 private readonly SessionsPythonSettings _settingsWithFileOperationsEnabled ;
@@ -57,6 +58,7 @@ public SessionsPythonPluginTests()
5758 {
5859 CodeExecutionType = SessionsPythonSettings . CodeExecutionTypeSetting . Synchronous ,
5960 CodeInputType = SessionsPythonSettings . CodeInputTypeSetting . Inline ,
61+ AllowedDomains = [ "localhost" ] ,
6062 EnableDangerousFileUploads = true ,
6163 AllowedUploadDirectories = new [ ] { Path . GetDirectoryName ( Path . GetFullPath ( FileTestDataFilePath ) ) ! } ,
6264 AllowedDownloadDirectories = new [ ] { Path . GetDirectoryName ( Path . GetFullPath ( FileTestDataFilePath ) ) ! }
@@ -335,6 +337,7 @@ public async Task ItShouldDownloadFileSavingInDiskAsync()
335337 [ InlineData ( "prod.fake-test-host.io" , "https://prod.fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , true ) ]
336338 [ InlineData ( "www.fake-test-host.io" , "https://www.fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , true ) ]
337339 [ InlineData ( "www.prod.fake-test-host.io" , "https://www.prod.fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , true ) ]
340+ [ InlineData ( "FAKE-TEST-HOST.IO" , "https://fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , true ) ]
338341 [ InlineData ( "fake-test-host.io" , "https://fake-test-host-1.io/subscriptions/123/rg/456/sps/test-pool" , false ) ]
339342 [ InlineData ( "fake-test-host.io" , "https://www.fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , false ) ]
340343 [ InlineData ( "www.fake-test-host.io" , "https://fake-test-host.io/subscriptions/123/rg/456/sps/test-pool" , false ) ]
@@ -351,17 +354,63 @@ public async Task ItShouldRespectAllowedDomainsAsync(string allowedDomain, strin
351354
352355 var sut = new SessionsPythonPlugin ( this . _defaultSettings , this . _httpClientFactory ) ;
353356
354- // Act
355- #pragma warning disable CA1031 // Do not catch general exception types
356- try
357+ // Act and assert
358+ if ( isAllowed )
357359 {
358360 await sut . ListFilesAsync ( ) ;
359361 }
360- catch when ( ! isAllowed )
362+ else
361363 {
362- // Ignore exception if the endpoint is not allowed since we expect it
364+ await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => sut . ListFilesAsync ( ) ) ;
363365 }
364- #pragma warning restore CA1031 // Do not catch general exception types
366+ }
367+
368+ [ Fact ]
369+ public async Task ItShouldDenyRequestsWhenAllowedDomainsIsNullAsync ( )
370+ {
371+ // Arrange
372+ this . _defaultSettings . AllowedDomains = null ;
373+ this . _defaultSettings . Endpoint = new Uri ( "http://169.254.169.254/metadata/instance" ) ;
374+ var sut = new SessionsPythonPlugin ( this . _defaultSettings , this . _httpClientFactory ) ;
375+
376+ // Act and assert
377+ await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => sut . ListFilesAsync ( ) ) ;
378+ }
379+
380+ [ Fact ]
381+ public async Task ItShouldDenyRequestsWhenAllowedDomainsIsEmptyAsync ( )
382+ {
383+ // Arrange
384+ this . _defaultSettings . AllowedDomains = [ ] ;
385+ this . _defaultSettings . Endpoint = new Uri ( "http://169.254.169.254/metadata/instance" ) ;
386+ var sut = new SessionsPythonPlugin ( this . _defaultSettings , this . _httpClientFactory ) ;
387+
388+ // Act and assert
389+ await Assert . ThrowsAsync < InvalidOperationException > ( ( ) => sut . ListFilesAsync ( ) ) ;
390+ }
391+
392+ [ Fact ]
393+ public async Task ItShouldRejectRedirectResponseFromNonRedirectingClientAsync ( )
394+ {
395+ // Arrange
396+ await using var server = new RedirectLoopbackServer ( "metadata/instance" , "application/json" , [ ] ) ;
397+ using var httpClientHandler = new HttpClientHandler { AllowAutoRedirect = false } ;
398+ using var nonRedirectingClient = new HttpClient ( httpClientHandler ) ;
399+ var httpClientFactoryMock = new Mock < IHttpClientFactory > ( ) ;
400+ httpClientFactoryMock . Setup ( f => f . CreateClient ( It . IsAny < string > ( ) ) ) . Returns ( nonRedirectingClient ) ;
401+ var settings = new SessionsPythonSettings (
402+ sessionId : Guid . NewGuid ( ) . ToString ( ) ,
403+ endpoint : new Uri ( server . BaseUri , "start" ) )
404+ {
405+ AllowedDomains = [ server . BaseUri . Host ] ,
406+ CodeExecutionType = SessionsPythonSettings . CodeExecutionTypeSetting . Synchronous ,
407+ CodeInputType = SessionsPythonSettings . CodeInputTypeSetting . Inline
408+ } ;
409+ var sut = new SessionsPythonPlugin ( settings , httpClientFactoryMock . Object ) ;
410+
411+ // Act and assert
412+ await Assert . ThrowsAsync < HttpOperationException > ( ( ) => sut . ListFilesAsync ( ) ) ;
413+ Assert . False ( server . RedirectTargetContacted , "The redirect target should not have been contacted." ) ;
365414 }
366415
367416 [ Fact ]
@@ -539,6 +588,7 @@ public async Task ItShouldAllowDownloadDirectlyInsideAllowedDirectoryAsync()
539588 sessionId : Guid . NewGuid ( ) . ToString ( ) ,
540589 endpoint : new Uri ( "http://localhost:8888" ) )
541590 {
591+ AllowedDomains = [ "localhost" ] ,
542592 AllowedDownloadDirectories = new [ ] { tempDir }
543593 } ;
544594
0 commit comments