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

docs: expand key rotation example for future #474

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

rarkins
Copy link
Contributor

@rarkins rarkins commented Mar 11, 2022

I'm guessing/hoping that this is how it works, but I figured it was as much effort to prepare a docs PR in case it is than to ask the question. Does this look right??

@vercel
Copy link

vercel bot commented Mar 11, 2022

Someone is attempting to deploy a commit to a Personal Account owned by @vvo on Vercel.

@vvo first needs to authorize it.

README.md Outdated Show resolved Hide resolved
Co-authored-by: Vincent Voyer <vincent@codeagain.com>
@vvo
Copy link
Owner

vvo commented Mar 11, 2022

It does look right yes on the explanation. I am not even using rotation on my side (BOOO!).

The rotation strategy can also be simplified to:

  • if your sessions are 14 days long
  • if you change keys every 7 days
  • then you only need at most two keys in the array
  • and you can always stick to ids 1., 2.

day 0:

1: abc

day 7:

1. def
2. abc

day 14:

1. ghi
2. def

day 21:

1. jkl
2. ghi

What do you think?

@rarkins
Copy link
Contributor Author

rarkins commented Mar 11, 2022

Does a 2 week session mean that you get logged out and see a login screen every 2 weeks, or that you need to access the site at least every two weeks to remain logged in and never see a login screen?

@vvo
Copy link
Owner

vvo commented Mar 11, 2022

or that you need to access the site at least every two weeks to remain logged in and never see a login screen?

You need to access the site and do an action that itself will resolve to a session.save(). There's no automatic session refresh for reads only.

@rarkins
Copy link
Contributor Author

rarkins commented Mar 11, 2022

While I agree that your approach sounds feasible, it also looks like something you could easily mess up by accident if you were doing it regularly and had a lot of passwords over time..

@wup-one
Copy link

wup-one commented May 29, 2022

It does look right yes on the explanation. I am not even using rotation on my side (BOOO!).

The rotation strategy can also be simplified to:

  • if your sessions are 14 days long
  • if you change keys every 7 days
  • then you only need at most two keys in the array
  • and you can always stick to ids 1., 2.

day 0:

1: abc

day 7:

1. def
2. abc

day 14:

1. ghi
2. def

day 21:

1. jkl
2. ghi

What do you think?

Hey @vvo,
Is this example supposed to work, or is it simply a proposal?

Because I've been testing the password rotation functionality, and I completely agree with your example, but it doesn't seem to work that way.

What I've found is that the password number can't change or the unseal fails.

e.g., this unit test passes

test("Password rotation", async () => {
  const firstPassword = { 1: "BcTv8NKLVfGcTt18HqGf2DhEnmJrLbNU" };
  const secondPassword = {
    1: "BcTv8NKLVfGcTt18HqGf2DhEnmJrLbNU",
    2: "scKVNPWFippYjA3tRjJPuPnK7ocj4Vnn",
  };

  const seal = await sealData(
    { user: { id: 30 } },
    { password: firstPassword },
  );

  const session = await getIronSession(
    {
      ...defaultReq,
      headers: { cookie: `test=${seal}` },
    } as IncomingMessage,
    defaultRes,
    { password: secondPassword, cookieName },
  );

  expect(session).toMatchInlineSnapshot(`
    Object {
      "user": Object {
        "id": 30,
      },
    }
  `);
});

but this one fails

test("Password rotation", async () => {
  const firstPassword = { 1: "BcTv8NKLVfGcTt18HqGf2DhEnmJrLbNU" };
  const secondPassword = {
    1: "scKVNPWFippYjA3tRjJPuPnK7ocj4Vnn",
    2: "BcTv8NKLVfGcTt18HqGf2DhEnmJrLbNU",
  };

  const seal = await sealData(
    { user: { id: 30 } },
    { password: firstPassword },
  );

  const session = await getIronSession(
    {
      ...defaultReq,
      headers: { cookie: `test=${seal}` },
    } as IncomingMessage,
    defaultRes,
    { password: secondPassword, cookieName },
  );

  expect(session).toMatchInlineSnapshot(`
    Object {
      "user": Object {
        "id": 30,
      },
    }
  `);
});

Note the subtle difference being the definition of "secondPassword" where the IDs are in the reverse order. When the password used to encrypt is moved to "2", the unseal fails.

@rarkins
Copy link
Contributor Author

rarkins commented May 29, 2022

FWIW I would like to try the approach proposed by @vvo, so I'm also interested in seeing if it works as hoped

@ffakira
Copy link

ffakira commented Oct 18, 2023

Just wanted to see what is the current status progress for this PR? Sorry for sounding naive, but by any chance we have password rotation at iron-session?

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.

None yet

4 participants