2
2
// Copyright (c) James Jackson-South, Jeavon Leopold, and contributors. All rights reserved.
3
3
// Licensed under the Apache License, Version 2.0.
4
4
// </copyright>
5
+
6
+ // <summary>
7
+ // A singleton class for communicating with Azure Blob Storage.
8
+ // </summary>
5
9
namespace Our . Umbraco . FileSystemProviders . Azure
6
10
{
7
11
using System ;
@@ -10,6 +14,7 @@ namespace Our.Umbraco.FileSystemProviders.Azure
10
14
using System . Globalization ;
11
15
using System . IO ;
12
16
using System . Linq ;
17
+ using System . Text . RegularExpressions ;
13
18
using global ::Umbraco . Core . IO ;
14
19
using Microsoft . WindowsAzure . Storage ;
15
20
using Microsoft . WindowsAzure . Storage . Blob ;
@@ -34,6 +39,11 @@ internal class AzureFileSystem : IFileSystem
34
39
/// </summary>
35
40
private const string Delimiter = "/" ;
36
41
42
+ /// <summary>
43
+ /// The regex for parsing container names.
44
+ /// </summary>
45
+ private static readonly Regex ContainerRegex = new Regex ( "^[a-z0-9](?:[a-z0-9]|(\\ -(?!\\ -))){1,61}[a-z0-9]$|^\\ $root$" , RegexOptions . Compiled ) ;
46
+
37
47
/// <summary>
38
48
/// Our object to lock against during initialization.
39
49
/// </summary>
@@ -525,7 +535,7 @@ public Stream OpenFile(string path)
525
535
526
536
if ( ! blockBlob . Exists ( ) )
527
537
{
528
- this . LogHelper . Info < AzureBlobFileSystem > ( string . Format ( "No file exists at {0 }." , path ) ) ;
538
+ this . LogHelper . Info < AzureBlobFileSystem > ( $ "No file exists at { path } .") ;
529
539
return null ;
530
540
}
531
541
@@ -549,7 +559,16 @@ public Stream OpenFile(string path)
549
559
/// <returns>The <see cref="CloudBlobContainer"/></returns>
550
560
private static CloudBlobContainer CreateContainer ( CloudBlobClient cloudBlobClient , string containerName , BlobContainerPublicAccessType accessType )
551
561
{
552
- CloudBlobContainer container = cloudBlobClient . GetContainerReference ( containerName ) ;
562
+ containerName = containerName . ToLowerInvariant ( ) ;
563
+
564
+ // Validate container name - from: http://stackoverflow.com/a/23364534/5018
565
+ bool isContainerNameValid = ContainerRegex . IsMatch ( containerName ) ;
566
+ if ( isContainerNameValid == false )
567
+ {
568
+ throw new ArgumentException ( $ "The container name { containerName } is not valid, see https://msdn.microsoft.com/en-us/library/azure/dd135715.aspx for the restrtictions for container names.") ;
569
+ }
570
+
571
+ CloudBlobContainer container = cloudBlobClient . GetContainerReference ( containerName . ToLowerInvariant ( ) ) ;
553
572
container . CreateIfNotExists ( ) ;
554
573
container . SetPermissions ( new BlobContainerPermissions { PublicAccess = accessType } ) ;
555
574
return container ;
0 commit comments