gatsby: Gatsby-source-wordpress not able to fetch image behind basic authenticate WordPress source

Preliminary Checks

Description

Upgrading to latest gatsby-source-wordpress 6.10.0 has led to a build error of unable to fetch image error, with WordPress source that is behind basic authentication password protection.

Build is fine once it’s downgraded back to gatsby-source-wordpress 6.9.1

Error message: UNHANDLED REJECTION Unable to fetch: [Image URL]

Reason: Response code 401 (Unauthorized)

Reproduction Link

Any basic auth protected WordPress

Steps to Reproduce

  1. With latest gatsby-source-wordpress 6.10.0 installed, run a build from a basic authenticated protected wordpress source

Expected Result

gatsby-source-wordpress MediaItem to fetch images and media without error

Actual Result

UNHANDLED REJECTION Unable to fetch: [Image URL]

Reason: Response code 401 (Unauthorized)

Fetch details: { “attempt”: 3, “method”: “GET”, “errorCode”: “ERR_NON_2XX_3XX_RESPONSE”, “responseStatusCode”: 401, “responseStatusMessage”: “Unauthorized”, “requestHeaders”: { “user-agent”: “got (https://github.com/sindresorhus/got)”, “accept-encoding”: “gzip, deflate, br” }, “responseHeaders”: { “server”: “nginx”, “date”: “Wed, 16 Mar 2022 21:55:14 GMT”, “content-type”: “text/html”, “content-length”: “172”, “connection”: “close”, “www-authenticate”: “Basic realm="XXXXXXXX"” } }

Environment

gatsby-source-wordpress 6.10.0
basic auth protected WordPress source

Config Flags

No response

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 29 (20 by maintainers)

Most upvoted comments

@rburgst Update: we are planning on implementing a fix to make this work when Image CDN is disabled within the next two weeks.

@rburgst I apologize you’ve had a poor experience before writing PRs. That’s not our goal. I’ll take responsible for getting the remark one in tomorrow. I hope you’ll give contributing a go again in the future and have a better experience.

@rburgst @KyleAMathews the integrations team at Gatsby has this bug scheduled as part of our current 2 week sprint. I’ll keep yall updated as we make progress.

@joernroeder looks related indeed. The original issue was separate but was resolved, the new issue is the same as this one.

There is indeed! That PR is close to being merged. If you need a fix today you can try these canary versions:

yarn add gatsby@4.15.0-alpha-wp-image-cdn-auth.46 gatsby-source-wordpress@6.15.0-alpha-wp-image-cdn-auth.46

Make sure the versions are exact in your package.json (without a ^ before the version).

Thanks for asking!

EDIT: Looks like those canaries aren’t working properly. I’m looking into it now, possibly I published them wrong EDIT2: Fixed! I updated the versions above with working versions

@veryspry will this also fix #35626 by any chance?

@rburgst What @TylerBarnes said!

While we could just pass the auth as args to gatsbyImageResolver, this resolver function gets used elsewhere “automagically” and not always explicitly like it is in the HTML replace function you pointed out. We need to make sure this fix covers the following cases:

  1. when calling the function explicitly in code - like this example
  2. when the resolver is called when querying the gatsbyImage field on nodes where this field is automatically attached. For instance, when running this query against the gatsby-source-graphql plugin: image
  3. when other CMS’ / source plugins need to use basic auth for image downloads AND making sure that multiple source plugins needing to pass the auth headers can work side by side

That said, if we just pass the auth header data as args to gatsbyImageData, then this will only fix certain cases for gatsby-source-wordpress and will still not work correctly in other cases (e.g. the query from the screenshot above)

I have the same issue after upgrading gatsby-source-wordpress to the latest version 6.10.0.