Azure Blob Storage functions suddenly giving 403 errors

Viewed 463

I wrote an api endpoint to get a saskey for an Azure blob using javascript a few months ago. It's worked fine since then. Just yesterday, it started giving me 403 errors. I hadn't touched the file in months, nor had I accessed my Azure account to make changes. Looked at some posts here and checked my firewall (correctly configured), date on PC is correct, even tried resetting and using my access key2. Still getting the same error. Azure is responding with a saskey, it's just not valid. Any suggestions on how to troubleshoot this would be much appreciated. Code is as follows:

api.js (mediaLoc is the file name):

app.get("/getSASKey", checkAuth, async function (req, res) {
  var mediaLoc = req.query.mediaLoc;
  console.log('mediaLoc is: ', mediaLoc)
  blobResult = await bloboperations.sasKey(mediaLoc).then((result) => {
  console.log('api about to send sasKey back as: ', result)
      res.send(result);
  });
});

bloboperations.sasKey:

async function sasKey(mediaLoc) {
    try {
    console.log('starting sasKey in bloboperations mediaLoc:', mediaLoc)
    var storage = require("@azure/storage-blob")
    const accountname = process.env.BLOB_ACCT_NAME;
    const key = process.env.BLOB_KEY;
    const cerds = new storage.StorageSharedKeyCredential(accountname,key);
    const blobServiceClient = new storage.BlobServiceClient(`https://${accountname}.blob.core.windows.net`,cerds);
    const containerName="dev";
    const client =blobServiceClient.getContainerClient(containerName)
    const blobName=mediaLoc;
    const blobClient = client.getBlobClient(blobName);
    const blobSAS = storage.generateBlobSASQueryParameters({
      containerName, 
      blobName, 
      permissions: storage.BlobSASPermissions.parse("racwd"), 
      startsOn: new Date(),
      expiresOn: new Date(new Date().valueOf() + 86400)
    },
    cerds 
  ).toString();
    const sasUrl= blobClient.url+"?"+blobSAS;
    return sasUrl
    }
    catch (error) {
        console.log(error);
    }
}

EDIT/ADDITIONAL INFORMATION: As soon as I posted this edit, I thought I saw space in the URL. After looking more closely and copying it to notepad, I see there isn't a space after all. Too bad, I was hoping it could be that easy!

The saskey I'm getting back from Azure is:

sv=2020-06-12&st=2021-07-25T20%3A58%3A53Z&se=2021-07-25T21%3A00%3A20Z&sr=b&sp=racwd&sig=q6ylvdvxFKuR1zHmK2sa%2FwjlDjRoqtA6Jt47vZ0pQt4%3D

I'm appending that to: https://MYACCTHERE.blob.core.windows.net/dev/0f0djx0dshmp316324a060040a9b1624826368094.jpg? (0f0djx0dshmp316324a060040a9b1624826368094.jpg is the blob name on Azure.)

to get the url:

https://s4udev.blob.core.windows.net/dev/0f0djx0dshmp316324a060040a9b1624826368094.jpg?sv=2020-06-12&st=2021-07-25T20%3A58%3A53Z&se=2021-07-25T21%3A00%3A20Z&sr=b&sp=racwd&sig=q6ylvdvxFKuR1zHmK2sa%2FwjlDjRoqtA6Jt47vZ0pQt4%3D

(Obviously, this is from yesterday so it is expired.) At the time I generated it, I tried copying and pasting this directly into the browser address line with the same 403 error.

I'm using Vue2 on the front end. The url is passed to Vue as variable sasKey and placed on the page using this code:

  <v-img 
  class="white--text align-end imageTag"
  max-width="10rem"
  height="100%"
  :src="sasKey"
  >
  </v-img>
1 Answers

The answer for me was found in the comments by Gaurav Mantri. I used url encode and it fixed the problem.

Related