getSignedURL() in Google Cloud Function produces link that works for several days, then returns "SignatureDoesNotMatch" error
Environment details
- OS: Heroku dyno (Ubuntu 16.04)
- Node.js version: 8.6
- npm version: 6.0.0
@google-cloud/storageversion: 1.6.0
Steps to reproduce
Repo of relevant code: https://github.com/colinjstief/getSignedUrl-example
Lots of this code is copied/pasted straight from official example (link here). Code in my repo runs as a Google Cloud Function and produces a link that works for a few days (maybe 7?), and then seems to expire with "SignatureDoesNotMatch" 403 response. Full message reads:
"The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method."
URL seems valid, including this query parameter: '...&Expires=16730323200&Signature=...'
Both the initial request and the later request (after apparent expiration) are performed the same ways: either through an axios.get() or simply placing in browser bar. Both work at first, then both fail later.
Same issue seems to be reported here GoogleCloudPlatform/google-cloud-node#1976, here googleapis/nodejs-storage#144, and here https://github.com/firebase/functions-samples/issues/360
Thank you for reporting. Are you able to share the entire link that works, then eventually fails? It should probably be looked at by someone on the Storage team (cc @frankyn).
IIUC, the issue here is that it expires when it shouldn't given the config at defined: index.js#L28
That looks right and matches the expires timestamp that we use in the constructed URL: 16730323200. Is there anything else that would help debug from the Storage API-side? Unless you think there's something we could be doing improperly from the code, outside of the Expires parameter?
I added a shorter Node script to generate blobToSign: Given the version used for the example I modified client library v1.6.0 to console log the blobToSign on line#1703:
GET
16730352000
/bucket/image.jpg
Shortened script:
// Imports the Google Cloud client library
const Storage = require('@google-cloud/storage');
// Your Google Cloud Platform project ID
const projectId = 'project-id';
// Creates a client
const storage = new Storage({
projectId: projectId,
});
// The name for the new bucket
const bucketName = 'bucket';
bucket = storage.bucket(bucketName);
file = bucket.file("image.jpg");
CONFIG = {
action: 'read',
expires: '03-01-2500',
};
file.getSignedUrl(CONFIG, function(err, url) {
console.log(url);
});
I do notice no metadata (contentType) is being used when creating the blobToSign so this should provide the similar output due to different service accounts, bucket, and object names. @stephenplusplus does this look right?
@colinjstief could you share the signed URL or provide the value inside <StringToSign> in the error message?
Sure thing, here it is:
https://storage.googleapis.com/landlinc-stage.appspot.com/9AnnEvelbPM6Y8dkhK6V%2Freports%2F67Np6lvR63N3xiwVej83%2FOvsTySFqPd%2FjzwtK%2Fthumb_20171130_115850.jpg?GoogleAccessId=landlinc-stage@appspot.gserviceaccount.com&Expires=16730323200&Signature=VesGWuEhz%2F12TU6j%2FI5HZrqh%2BdNotJ%2B6WSrkHSjfVSPEna6nEHMFwtT3e1laBOf6GNGd8Vbona2F5Y8i4jfKi3R%2BtKsK3xOBnmKrGv04kv8QoJcMpCyNBrdDLaaHeVoSHz1h6ghiwJtNCt3UOB5ErNsc1fSh90LAJBO0wsHvLWCWHGJV5EazzyBRNHxjBiB%2BKYqdqJjpogtbPxN9yDfMG0JavybqIJvp3SZlMvZ5j2E6BsJMqo9S0nkFTorjjRJr8IEbhjA7IFPwnzjLSW4O4O%2Bc0XXhpdLrWarWSJEwHWrQOo%2FVjR63r5XYjloQ%2FwFEw9PXBgQ8Wmqf3AN5jOdnVQ%3D%3D
@colinjstief one more request. Could you recreate this signedURL using my script above and the service account [email protected] ? I want to compare.
Away from computer right now, will post this evening.
Seems like I need a little more in that snippet related to credentials? Just running it via node on my local machine and it's throwing a "Cannot sign data without client_email" error.
@stephenplusplus is the only way to define client_email by setting the GOOGLE_APPLICATION_CREDENTIALS? The error message a bit confusing at the moment as it's not actionable.
@colinjstief Instead of running the shorter script, could you re-upload the same object you did before with the same name? The goal would be to regenerate the same signed URL you provided earlier and compare the versions.
@frankyn sorry for the confusion, but if I understood correctly, I generated a signed URL using the exact same cloud function (and environment) as before. Here it is:
https://storage.googleapis.com/landlinc-stage.appspot.com/9AnnEvelbPM6Y8dkhK6V%2Freports%2FCHbwtVuIPFD4ig0rbs8m%2FOvsTySFqPd%2FC4y59%2Fthumb_20171130_115850.jpg?GoogleAccessId=landlinc-stage%40appspot.gserviceaccount.com&Expires=16730323200&Signature=BqgwaD1mIFyH0wQZYnNn9IDNNFpVFHa5WpsYl%2F3De0ck7R8AiqgZN3w2N%2FyLBwT6fbPYkUWp%2FoY2W5y6lu0Q9S5HkL4LCK6TvLcjHePddWlE1w6wfSM7Nmb6fixRU1PyzO081HF4l28zCLBZS335F2mH0TBa3pQFwTyh53O2mDMIBiTCwfLZh3xwfFkhhEN4cU8gqEHD8klA3dRSKg%2FY2L9z1Nb6pocWcNJPWuGaBHCB61eAVX5urMlqJWlRvLJblpKUXsURbefdQPGKMNAdjCn8GIzKkKttrBqOSWMtaheI%2BaXC022Erb1nPl7tVcxAjbCB5bWlszoQmdvKsWQTuw%3D%3D
One thing to note, the url string prior to the query parameters is a little different because the logic of the process creates unique identifiers for that particular upload.
Could you update the logic to use the same unique identifier as the prior signed URL?
Technically, if the signedURL method provided all the same parameters it will generate the same signedURL. It doesn't use current time and also doesn't maintain state for the signed URL in the GCS backend.
Here is the URL, created from a file with the exact directory as the original file. It appears to have a different signature:
https://storage.googleapis.com/landlinc-stage.appspot.com/9AnnEvelbPM6Y8dkhK6V%2Freports%2F67Np6lvR63N3xiwVej83%2FOvsTySFqPd%2FjzwtK%2Fthumb_20171130_115850.jpg?GoogleAccessId=landlinc-stage%40appspot.gserviceaccount.com&Expires=16730323200&Signature=Of%2FsX%2BTrwGhipWTzsSOIbiX9AxLFyWn1ScUatJ1xdMdblH4AZi7f2jxDAe1ckbJWT5O8Y6FAqgEWoN1fgsWQ5Xvd17einxAIm5%2FCcv9iaap19wejV5sT8kOkpkCu0dNwsu6psovWSx4Y1i4RmzEfP9CuSfocyfuNdZXx5c5zUCZ30dxlQCiKE3zeZwh%2FM47XUbKEikXPnmaLrpO9czfrVLkdmJeT8kCYUdGIlC5ldtEcwaN1fDMs%2BWQAgvCd9CpODVh1ogHybO%2F2hCA3Sje3UpWAzDrB%2F8uM50ceDFN66ntUCZRM4cxHflTIu7kXlxoWKg9qonaIuyGZN5wIQEEwcg%3D%3D
Another thing to note is that I do some cropping and compressing of the image, so technically it's not the same file each time. Would that change things?
One difference between this version from the prior besides the different Signature is @ is %40 (percent encoded). Did you update the version of the NodeJs storage client library?
Given the different signature, I wonder if the private key for the service account was refreshed, maybe? Will try to reproduce the error and report back.
That is strange. No, I just checked the change history and I’m 99% sure the library wasn’t updated.
Hello, I have the exact same problem (signed URLs expire after some time, even though 'expires' property is set to a date far in the future). This is the relevant code (initialization is slightly different than the example @colinjstief provided):
admin.initializeApp(functions.config().firebase);
exports.onFileUpload = functions.storage.object().onFinalize(data => {
const bucket = data.bucket;
const filePath = data.name;
const destBucket = admin.storage().bucket(bucket);
const file = destBucket.file(filePath);
return file.getSignedUrl({
action: 'read',
expires: '03-09-2491'
})
.then(url => {
return url[0];
})
.then(
// save url to db and do other stuff ...
)
};
@google-cloud/storage version is the same: 1.6.0
These are two generated URLs for the exact same file (just re-uploaded and overwritten couple days later). Currently both links work (even though the signatures are different), but the first one will probably stop working a week from now approximately, the second one after two weeks. That estimation is based on previous observations.
https://storage.googleapis.com/vennfx-web-ng.appspot.com/1524742000212_ForexAdvantages.jpg?GoogleAccessId=vennfx-web-ng%40appspot.gserviceaccount.com&Expires=16447017600&Signature=FDo8YPo0TkkNqhe60Igdf5YNSY%2FDDtijImLE3Ah0W2AP8JOxhABBFQAnUCFyvsvupZ4hOJhKb48QZzvKakVplTLAolZswrrmNaWe9OOSH9vnDm5cvkd0J7QCOd%2F7Bo7XrQmCtKrdDQdtc9tnVp6Hp5Bs0vQgmOucfaMD0UU0utn4uy0m5KRzsZiagHPzzBqkx9jiF%2BwdqQBAFttZEV4dQUQNB6g4OnuuwN7%2FALutR%2FZSzMlPmftMKxccntCpu2FO0goeXOMfTFyn6SaFRokVrssddzzcSbHcH%2B%2BvA74yWzWeiFWcy3k0kvmn4YPotWlDpr1SJ3PC8J4qYI05MMVsCg%3D%3D
https://storage.googleapis.com/vennfx-web-ng.appspot.com/1524742000212_ForexAdvantages.jpg?GoogleAccessId=vennfx-web-ng%40appspot.gserviceaccount.com&Expires=16447017600&Signature=t2OiQH4ulovSDNBy88EccKuczuHf3pcO4wvDnYmYIux0tnJoP92w4wKYCL6RTuLf8B93bwSZMgERVg5edCUns8kdDt7Ira1OjhjDWw%2BiBlyzdUXCibbeMiv8V44BBLQH5q%2Br32DA7rgX%2BcSWrolgl03pKAZaJoYfSy95TmhyT8eQAYvk8LZRlLwYRt8Q247eYGcWGlLX9gSpgSzrhLvDOQZTkqcqKuOmpvLdSoU73O3OuJf7y2txSOsLU7d%2FG7O%2B7u4eQxg2XZdJbUlJcZRNCZ8AxWKGkDzEZMnAYcaGZ1%2F%2BYlVXF6q60WjYEFB7UAbsi0hEKgl2V2aWyY3ji6pTIw%3D%3D
@frankyn I see that the first URL that @BorisBeast generated is now expired. Mine both seem to be working, but I expect that to change in the next few days. Will keep checking.
Have the exact same problem.
In a Firebase function, using GCS node js API ver 1.7.0, uploading an image to GCS and immediately asking for a signed URL to expire in the future (~7 years):
let gcsBucket = gcs.bucket(bucketname);
let options = {
destination: gcsFilename,
metadata: {
metadata: metadata,
contentType: 'image/jpeg'
}
};
gcsBucket.upload(mainImageLocalUrl, options).then((data) => {
let file = data[0];
let config = {
action: 'read',
expires: '01-01-2025'
};
return file.getSignedUrl(config);
Everything is working fine (url is accessible by clients) but at some point (after few days, ~10 days) - it "expires" and returns:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google secret key and signing method.
</Message>
<StringToSign>
GET 1735689600 /XXXXXXXXX.appspot.com/some_directory%2FFAD0E0EA-61AE-4689-AB22-189E02FD2710.jpg
</StringToSign>
</Error>
URLs created more recently are available and working as expected. Random examples (as of 8am GMT on July 8th): URLs created on June 22nd are returning the above error URLs created on June 29th are still working and allowing access to files
@frankyn Both of the URLs created for this thread are now broken and throwing the same error.
Hi all,
Apologies for the delay:
I created a new service account and then generated a signed URL for a GET request.
After deleting the service account key from the Google Cloud Console, I received the same error message you all have been seeing:
<Error>
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your Google
secret key and signing method.
</Message>
<StringToSign>
GET 1531165081 /coderfrank/51f7251dc8561126be000027_736.jpg
</StringToSign>
</Error>
I'll follow-up internally with the Cloud Functions team. My current guess is the service account key used to provision your Cloud Function environment is deleted after a certain amount of time (5-7 days). The service account is used to generate a signedURL and when deleted renders the URL useless (SignatureDoesNotMatch).
I appreciate everyone's patience on this issue.