Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

File.publicUrl() returns an incorrect URL if the file key contains URL query parameters #1824

Closed

Description

@jbrowning

Environment details

  • OS: Google Cloud Function (1st gen)
  • Node.js version: 14.19.1
  • npm version:
  • @google-cloud/storage version: 5.8.5

Steps to reproduce

  1. Upload a file to Cloud Storage containing query parameters in the key -- example: somefile.jpg?state=thumbnail
  2. Make the file public via File.makePublic()
  3. Retrieve the file's public URL with File.publicUrl()

The returned URL looks like https://storage.googleapis.com/bucket/somefile?state=thumbnail but any attempt to download the file at that URL results in the following error:

<Error>
  <Code>AccessDenied</Code>
  <Message>Access denied.</Message>
  <Details>Anonymous caller does not have storage.objects.get access to the Google Cloud Storage object.</Details>
</Error>

HOWEVER Viewing the file in the Google Cloud Platform Cloud Storage web console does give the correctly-encoded URL which looks like https://storage.googleapis.com/bucket/somefile.jpg%3Fstate%3Dthumbnail

I've reviewed the Changelog for subsequent releases but there is no mention of a fix for this.

Activity

  1. added
    priority: p2Moderately-important priority. Fix may not be included in next release.
    type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.
    on Mar 24, 2022
  2. ddelgrosso1 commented on Mar 28, 2022

    @ddelgrosso1
    Contributor

    Thank you for bringing this to our attention @jbrowning. I have identified what I believe to be the source of the problem you are encountering and will be working on a fix. I will update this issue soon with any further findings / when a fix is available.

  3. self-assigned this
    on Mar 28, 2022
  4. ddelgrosso1 commented on Mar 28, 2022

    @ddelgrosso1
    Contributor

    @jbrowning what I believe to be the fix for this has been released in version 5.18.3.

  5. jbrowning commented on Mar 28, 2022

    @jbrowning
    Author

    @ddelgrosso1 Perfect. Thanks for the quick fix.

  6. sseppola commented on Apr 7, 2022

    @sseppola

    @ddelgrosso1 was it intentional that filenames with paths now turn from upload/video/test.mp4 to upload%2Fvideo%2Ftest.mp4?
    It still works, and the GCS UI shows the directories as expected, but the public urls I store in my DB are less readable.

    Edit:
    Would you consider something like:

    publicUrl(): string {
    	const url = new URL(`${this.storage.apiEndpoint}/${this.bucket.name}/${this.name}`);
    	return url.origin + url.pathname + encodeURIComponent(url.search)
    }

    This should be Nodejs 10 compatible according to: https://developer.mozilla.org/en-US/docs/Web/API/URL

  7. ddelgrosso1 commented on Apr 7, 2022

    @ddelgrosso1
    Contributor

    Hi @sseppola it was intentional from the standpoint that other spots in the code are currently encoding the entire name portion as I applied in the fix. Your suggestion makes sense to me, let me investigate if switching to something like this will have any other possible consequences.

  8. ddelgrosso1 commented on Apr 7, 2022

    @ddelgrosso1
    Contributor

    @sseppola I should have realized this before but the reason it was done this way is due to the object naming guidelines in GCS. In the above snippet this.name represents the object name, which can support characters such as spaces, ampersands, etc. In order to return a properly working public url the entire name will need to be encoded to account for these cases.

  9. sseppola commented on Apr 7, 2022

    @sseppola

    Makes sense, thanks for resolving it so quickly 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: storageIssues related to the googleapis/nodejs-storage API.priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions