Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded key fixes #6

Merged
merged 7 commits into from
Oct 19, 2023
Merged

Embedded key fixes #6

merged 7 commits into from
Oct 19, 2023

Conversation

oliviathet
Copy link
Contributor

@oliviathet oliviathet commented Oct 19, 2023

TL;DR: 1 fix + 2 changes for the embedded key that is generated and stored in localStorage:

  • [fix] fix keygen to have the right keyops
  • [change] expiration for local storage
  • [change] RESET event feature

We want to avoid the nightmare situation where we have buggy keys in the user's localStorage. Rather than directing the user from manually debugging/clearing their localStorage, we can add 2 additional controls: 1) The parent page can post a RESET_EMBEDDED_KEY event that we listen to clear the key from localStorage. 2) The key is set in localStorage with an expiration of 48 hours. If it's expired when we're fetching it, we remove it from localStorage and return null. If it should have an expiration but is not saved with an expiration, we also remove it and return null.

TODO: Maybe in a separate PR, we need to figure out a fix for

index.html:890 Uncaught (in promise) Error: decryption failed: M
at HpkeDecrypt (index.html:890:15)
at async onInjectBundle (index.html:809:39)

@oliviathet oliviathet requested a review from r-n-o October 19, 2023 03:50
@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Oct 19, 2023

Deploying with  Cloudflare Pages  Cloudflare Pages

Latest commit: 75f52ef
Status: ✅  Deploy successful!
Preview URL: https://b8f84bba.recovery-br3.pages.dev
Branch Preview URL: https://olivia-expire-and-fix.recovery-br3.pages.dev

View logs

Copy link
Collaborator

@r-n-o r-n-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One piece that's missing: initEmbeddedKey should check for expir

index.html Outdated Show resolved Hide resolved
index.html Show resolved Hide resolved
index.test.js Show resolved Hide resolved
index.html Outdated
Comment on lines 830 to 833
* DEBUG functionality only to reset the page's localStorage
*/
document.getElementById("clear").addEventListener("click", TKHQ.clearEmbeddedKey);
document.getElementById("reset").addEventListener("click", TKHQ.resetEmbeddedKey);
}, false);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want an actual event to drive the reset let's have the button click trigger an event on the page like we do for other events:
https://github.com/tkhq/recovery/blob/ce33591723435a64be97830a2a492ef6f7e519a7/index.html#L776-L782

And then you can add another if statement in here to handle the reset case:
https://github.com/tkhq/recovery/blob/ce33591723435a64be97830a2a492ef6f7e519a7/index.html#L753-L762

That way the parent will be able to post a RESET message to an iframe, or we can trigger this even in the standalone page, and the exact same onReset handler will be called.

index.html Show resolved Hide resolved
@oliviathet oliviathet changed the title WIP: Embedded key fixes Embedded key fixes Oct 19, 2023
@oliviathet
Copy link
Contributor Author

btw initEmbeddedKey checks for expiry by calling getEmbeddedKey (calls getItemWithExpiry) and setEmbeddedKey (setItemWithExpiry)

index.html Outdated
document.getElementById("reset").addEventListener("click", TKHQ.resetEmbeddedKey);
document.getElementById("reset").addEventListener("click", async function(e) {
e.preventDefault();
window.postMessage({ "type": "RESET_EMBEDDED_KEYSET" })
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • I think you're missing the event handler for this new event! We should now have a onReset or onClear function to do this (you can rename resetEmbeddedKey
  • The even name should be RESET_EMBEDDED_KEY (you have an extra SET in there!)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AH im just slipping today >_< thanks for catching this

Copy link
Collaborator

@r-n-o r-n-o left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! Looks ready to merge! One final tiny nitpicky thing

index.html Outdated
getEmbeddedKey,
setEmbeddedKey,
clearEmbeddedKey,
onResetEmbeddedKey,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I don't think this needs to be exported anymore!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i originally kept it for the test, but i think we don't really need to test this unless we want to test all events. and we are already testing the underlying functionality for localStorage getter/setter. so i just removed this and its usage in the test!

@oliviathet oliviathet merged commit 6b6d7b9 into main Oct 19, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants