apollo-client: Apollo Client throwing a `TypeError: options.headers.hasOwnProperty is not a function`

Issue Description

This is the stack trace caught by Sentry. We are observing this for a few users while using the Safari 17.2.1.

TypeError: options.headers.hasOwnProperty is not a function. (In 'options.headers.hasOwnProperty(key)', 'options.headers.hasOwnProperty' is undefined)
  at ? (webkit-masked-url://hidden/:418:63)
  at <anonymous>(../../../node_modules/.aspect_rules_js/@apollo+client@3.8.8_1356184083/node_modules/@apollo/client/link/http/createHttpLink.js:127:13)
  at Subscription(../../../node_modules/.aspect_rules_js/zen-observable-ts@1.2.5/node_modules/zen-observable-ts/module.js:190:34)
  at _proto3.subscribe(../../../node_modules/.aspect_rules_js/zen-observable-ts@1.2.5/node_modules/zen-observable-ts/module.js:264:16)
  at <anonymous>(../../../node_modules/.aspect_rules_js/@apollo+client@3.8.8_1356184083/node_modules/@apollo/client/link/error/index.js:11:42)

Link to Reproduction

I’ve included the stack trace above. We are unable to reproduce this ourselves, but we have Sentry capturing the stack trace with sourcemaps.

Reproduction Steps

No response

@apollo/client version

3.8.8

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 29 (16 by maintainers)

Most upvoted comments

I ran into this issue myself where the Forcepoint Safari Extension / Websense Endpoint Helper extension was forced on to my computer by corporate leaving me with no options but to dive in and see what is going on.

Inside of the SafariExtension, they hijack the window.fetch and XMLHttpRequest and add what they need on top of them to monitor all requests. In this they have a block of code that checks options.headers and does some sort of reassignment for their monitoring use cases. In this, they expect the headers object to be a full object prototype with hasOwnProperty function but for some reason, only in Safari, only when doing a spread of the headers, this function doesn’t exist.

The right fix is for the plugin to do a, Object.prototype.hasOwnProperty.call(object, key) because that uses the Object.prototype instead of hoping the thing being passed in has the prototype functions it wants. Eslint has been encouraging this behavior for ages now. https://eslint.org/docs/latest/rules/no-prototype-builtins . Sadly we can’t change the vendor’s code so I needed to find a workaround.

What I ended up doing to get things to work, and mind you I’m not using this apollo client directly, I’m using @nuxtjs/apollo to handle a lot of the heavy lifting, but the parameters existed for me to get to what I needed.

Apollo Client exposes custom fetching https://www.apollographql.com/docs/react/networking/advanced-http-networking/#custom-fetching , see also https://github.com/apollographql/apollo-client/blob/36d2caece4bcca7cf855ff8e82a9769df0ef405e/src/link/http/createHttpLink.ts#L185 . In the plugin I’m using it’s part of the httpLinkOptions, I was able to pass it the following fetch to make things happy.

httpLinkOptions: {
    fetch: async (uri, options) => {
      Object.assign(options, {
        headers: {
            "x-my-headers": "yes"
        }
      });
      
      return await fetch(uri, options);
    },
}

For whatever reason, the object assign of the headers object got me around the error.

I hope this helps anyone else who stumbles upon this error. This is absolutely feels like a gross work around for someone else’s bug. If anyone has a way to submit a bug to forcepoint, please do.

@phryneas I was able to give this a look in my project in safari and I’m no longer seeing the issue. I did have a few hiccups getting there because of the layers of abstraction provided by frameworks, but was able to verify.

Thanks for getting around to this so quickly! y’all are awesome.

Thanks for that @spnc-omz and @phryneas.

@spnc-omz thank you for the feedback - I’m getting this released then 😃

@spnc-omz that puts enough puzzle pieces in place that we might also be able to work around it on our side.

Could you please give npm i @apollo/client@0.0.0-pr-12054-20240904112316 a try and report back if it also solves the issue?

You are correct. There is a security extension installed. image I opened an incognito, and the error is no longer there.

Interestingly I don’t see anything changed having to do with headers in between 3.7.2 and 3.7.3. Here’s a different view of it that shows the diff between the compiled versions. Looking for changes to headers doesn’t yield any changes to anything with hasOwnProperty, so I’m a bit at a loss.

Unfortunately I’ve got a few other priorities I’ve got to look at today, so I’ll need to revisit later. If you have a revelation or spot it before I do, let me know and we’d be happy to address! PRs welcome as well 🙂

Awesome. Appreciate that context! Let me go and see what might have changed in that version to try and get a clue. Gotta love those cross browser quirks sometimes (if thats what it is 😆)