Skip to content

Commit

Permalink
Fix withItemData treating sessions that don't match an item as valid (#…
Browse files Browse the repository at this point in the history
…5168)

* Fix withItemData treating sessions that don't match an item as valid

* Try fixing the tests

* Fix the test
  • Loading branch information
emmatown authored Mar 22, 2021
1 parent 7fb3d36 commit 343b742
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-flowers-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystone-next/keystone': major
---

Fixed `withItemData` returning sessions that don't match an item rather than treating them as invalid
8 changes: 2 additions & 6 deletions packages-next/keystone/src/session/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export function withItemData<T extends { listKey: string; itemId: string }>(
!session.itemId ||
!sudoContext.lists[session.listKey]
) {
return session;
return;
}

// NOTE: This is wrapped in a try-catch block because a "not found" result will currently
Expand All @@ -104,15 +104,11 @@ export function withItemData<T extends { listKey: string; itemId: string }>(
where: { id: session.itemId },
resolveFields: fieldSelections[session.listKey] || 'id',
});
// If there is no matching item found, return the session without a `data value
if (!item) {
return session;
}
return { ...session, data: item };
} catch (e) {
// TODO: This swallows all errors, we need a way to differentiate between "not found" and
// actual exceptions that should be thrown
return session;
return;
}
},
};
Expand Down
8 changes: 3 additions & 5 deletions tests/api-tests/auth-header.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Iron from '@hapi/iron';
import express from 'express';
import { text, timestamp, password } from '@keystone-next/fields';
import { createSchema, list } from '@keystone-next/keystone/schema';
Expand Down Expand Up @@ -35,7 +34,7 @@ const initialData = {
};

const COOKIE_SECRET = 'qwertyuiopasdfghjlkzxcvbmnm1234567890';
const defaultAccess = ({ context }: { context: KeystoneContext }) => !!context.session?.item;
const defaultAccess = ({ context }: { context: KeystoneContext }) => !!context.session?.data;

const auth = createAuth({ listKey: 'User', identityField: 'email', secretField: 'password' });

Expand Down Expand Up @@ -150,19 +149,18 @@ multiAdapterRunners().map(({ runner, adapterName }) =>
for (const [listKey, items] of Object.entries(initialData)) {
await createItems({ context, listKey, items });
}
const { sessionToken, item } = await login(
const { sessionToken } = await login(
app,
initialData.User[0].data.email,
initialData.User[0].data.password
);

expect(sessionToken).toBeTruthy();

const sealedData = await Iron.seal({ item }, COOKIE_SECRET, Iron.defaults);
const { data, errors } = await networkedGraphqlRequest({
app,
headers: {
Cookie: `keystonejs-session=${sealedData}`,
Cookie: `keystonejs-session=${sessionToken}`,
},
query: '{ allUsers { id } }',
});
Expand Down

1 comment on commit 343b742

@vercel
Copy link

@vercel vercel bot commented on 343b742 Mar 22, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.