Skip to content

Commit 5ceec55

Browse files
committed
fix for sas token parsing
1 parent 707a102 commit 5ceec55

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

src/UmbracoFileSystemProviders.Azure/AzureFileSystem.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -615,26 +615,60 @@ private static CloudBlobContainer CreateContainer(CloudBlobClient cloudBlobClien
615615
}
616616

617617
CloudBlobContainer container = cloudBlobClient.GetContainerReference(containerName.ToLowerInvariant());
618-
if (cloudBlobClient.Credentials.IsSAS)
618+
if (!container.Exists())
619619
{
620-
// Shared access signatures (SAS) have some limitations compared to shared access keys
621-
// read more on: https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1
622-
string[] sasTokenProperties = cloudBlobClient.Credentials.SASToken.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
623-
bool isAccountSas = sasTokenProperties.Where(k => k.ToLowerInvariant().StartsWith("si=")).FirstOrDefault() == null;
624-
if (isAccountSas)
620+
if (cloudBlobClient.Credentials.IsSAS)
625621
{
626-
container.CreateIfNotExists();
622+
// Shared access signatures (SAS) have some limitations compared to shared access keys
623+
// read more on: https://docs.microsoft.com/en-us/azure/storage/common/storage-dotnet-shared-access-signature-part-1
624+
string[] sasTokenProperties = cloudBlobClient.Credentials.SASToken.Split("&".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
625+
bool isAccountSas = sasTokenProperties.Where(k => k.ToLowerInvariant().StartsWith("ss=")).FirstOrDefault() != null;
627626

628-
// permissions can't be set!
629-
}
627+
string allowedServices = sasTokenProperties.Where(k => k.ToLowerInvariant().StartsWith("ss=")).FirstOrDefault();
628+
if (allowedServices != null)
629+
{
630+
allowedServices = allowedServices.Split('=')[1].ToLower();
631+
}
632+
else
633+
{
634+
allowedServices = string.Empty;
635+
}
630636

631-
return container;
632-
}
637+
string resourceTypes = sasTokenProperties.Where(k => k.ToLowerInvariant().StartsWith("srt=")).FirstOrDefault();
638+
if (resourceTypes != null)
639+
{
640+
resourceTypes = resourceTypes.Split('=')[1].ToLower();
641+
}
642+
else
643+
{
644+
resourceTypes = string.Empty;
645+
}
633646

634-
if (!container.Exists())
635-
{
636-
container.CreateIfNotExists();
637-
container.SetPermissions(new BlobContainerPermissions { PublicAccess = accessType });
647+
string permissions = sasTokenProperties.Where(k => k.ToLowerInvariant().StartsWith("sp=")).FirstOrDefault();
648+
if (permissions != null)
649+
{
650+
permissions = permissions.Split('=')[1].ToLower();
651+
}
652+
else
653+
{
654+
permissions = string.Empty;
655+
}
656+
657+
bool canCreateContainer = allowedServices.Contains('b') && resourceTypes.Contains('c') && permissions.Contains('c');
658+
if (canCreateContainer)
659+
{
660+
container.CreateIfNotExists();
661+
662+
// cannot set permissions with sas access
663+
}
664+
}
665+
else
666+
{
667+
container.CreateIfNotExists();
668+
BlobContainerPermissions newPermissions = container.GetPermissions();
669+
newPermissions.PublicAccess = accessType;
670+
container.SetPermissions(newPermissions);
671+
}
638672
}
639673

640674
return container;

0 commit comments

Comments
 (0)