sp-dev-docs icon indicating copy to clipboard operation
sp-dev-docs copied to clipboard

Form digest expiration in SPFx service

Open martinlingstuyl opened this issue 3 years ago • 1 comments

Target SharePoint environment

SharePoint Online

What SharePoint development model, framework, SDK or API is this about?

💥 SharePoint Framework

Developer environment

Windows

What browser(s) / client(s) have you tested

  • [ ] 💥 Internet Explorer
  • [X] 💥 Microsoft Edge
  • [ ] 💥 Google Chrome
  • [ ] 💥 FireFox
  • [ ] 💥 Safari
  • [ ] mobile (iOS/iPadOS)
  • [ ] mobile (Android)
  • [ ] not applicable
  • [ ] other (enter in the "Additional environment details" area below)

Additional environment details

  • browser version 104.0.1293.70

  • SPFx version 1.15.2 (also noted in 1.15.0)

  • Node.js version v16.15.1

I'm using a docker dev container to build and serve the SPFx solution.

Describe the bug / error

The problem occurs when using an SPFx service in an SPFx command set extension. The SPFx service consumes the page context and retrieves the Form Digest to be able to call SharePoint endpoints using POST requests. When leaving the page open for more than an hour, and refreshing the page, the form digest will not be retrieved appropriately. You'll get an expired Form Digest. This will lead to 403 errors on post requests.

I've documented the behavior with a sample solution, and gif file here:

https://github.com/martinlingstuyl/spfx-requestdigest-issue

Originally, I filed it with the crew of PnP.js. They are also saying it's an SPFx bug, not a pnp.js bug. See the conversation here: https://github.com/pnp/pnpjs/issues/2379

Steps to reproduce

  1. Create an SPFx command set extension
  2. Create an SPFx service that consumes the PageContext to retrieve the Form Digest value.
  3. Use the SPfx service in the Command set extension to show or use the form digest.
  4. Wait an hour with the browser open
  5. Refresh the page
  6. The form digest that's being retrieved will be an old digest.

Expected behavior

I'd expect to always get a newed-up form digest after refreshing the page.

martinlingstuyl avatar Sep 06 '22 07:09 martinlingstuyl

Thank you for reporting this issue. We will be triaging your incoming issue as soon as possible.

ghost avatar Sep 06 '22 07:09 ghost

@martinlingstuyl - sorry for the delay. I can confirm the issue and we will be looking into it.

AJIXuMuK avatar Oct 31 '22 18:10 AJIXuMuK

Awesome! Great to hear it @AJIXuMuK!

martinlingstuyl avatar Oct 31 '22 20:10 martinlingstuyl

@martinlingstuyl - so, so update here. The issue will be fixed, but it will take time. The problem is in Service Worker used by Lists and Libraries. It will be updated but in the first half of the next year. So for now, I believe the only workaround here is to check the digest value, check the expiration and request it if needed.

But we WILL fix it. It won't just happen right away unfortunately.

AJIXuMuK avatar Nov 11 '22 14:11 AJIXuMuK

That's ok, I've got my workaround in place...thanks for looking into it and getting back to me on it.

martinlingstuyl avatar Nov 11 '22 14:11 martinlingstuyl