Skip to content

Commit

Permalink
fix(aws-amplify): createKeyValueStorageFromCookieStorageAdapter misse…
Browse files Browse the repository at this point in the history
…s default path and secure values (aws-amplify#13508)

* fix(aws-amplify): createKeyValueStorageFromCookieStorageAdapter misses default path and secure values

* Ensure Path is being serialized

* Delete cookie without path attr before setting it
  • Loading branch information
HuiSF authored Jul 19, 2024
1 parent aa7ae18 commit 60a559f
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
sameSite: 'strict' as any,
httpOnly: true,
secure: true,
path: '/a-path',
};

const result = createCookieStorageAdapterFromNextServerContext(mockContext);
Expand Down Expand Up @@ -176,7 +177,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
mockSerializeOptions.domain
};Expires=${mockSerializeOptions.expires.toUTCString()};HttpOnly;SameSite=${
mockSerializeOptions.sameSite
};Secure`,
};Secure;Path=${mockSerializeOptions.path}`,
);
});

Expand All @@ -188,7 +189,7 @@ describe('createCookieStorageAdapterFromNextServerContext', () => {
mockSerializeOptions.domain
};Expires=${mockSerializeOptions.expires.toUTCString()};HttpOnly;SameSite=${
mockSerializeOptions.sameSite
};Secure`,
};Secure;Path=${mockSerializeOptions.path}`,
);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ const createMutableCookieStoreFromHeaders = (
const serializeSetCookieOptions = (
options: CookieStorage.SetCookieOptions,
): string => {
const { expires, domain, httpOnly, sameSite, secure } = options;
const { expires, domain, httpOnly, sameSite, secure, path } = options;
const serializedOptions: string[] = [];
if (domain) {
serializedOptions.push(`Domain=${domain}`);
Expand All @@ -235,6 +235,9 @@ const serializeSetCookieOptions = (
if (secure) {
serializedOptions.push(`Secure`);
}
if (path) {
serializedOptions.push(`Path=${path}`);
}

return serializedOptions.join(';');
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,21 @@ describe('keyValueStorage', () => {
);
});

it('should remove item before setting item', async () => {
const testKey = 'testKey';
const testValue = 'testValue';
keyValueStorage.setItem(testKey, testValue);
expect(mockCookiesStorageAdapter.delete).toHaveBeenCalledWith(testKey);
expect(mockCookiesStorageAdapter.set).toHaveBeenCalledWith(
testKey,
testValue,
{
...defaultSetCookieOptions,
expires: expect.any(Date),
},
);
});

it('should set item with options', async () => {
const testKey = 'testKey';
const testValue = 'testValue';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
export const defaultSetCookieOptions: CookieStorage.SetCookieOptions = {
// TODO: allow configure with a public interface
sameSite: 'lax',
secure: true,
path: '/',
};
const ONE_YEAR_IN_MS = 365 * 24 * 60 * 60 * 1000;

Expand All @@ -25,6 +27,11 @@ export const createKeyValueStorageFromCookieStorageAdapter = (
): KeyValueStorageInterface => {
return {
setItem(key, value) {
// Delete the cookie item first then set it. This results:
// SetCookie: key=;expires=1970-01-01;(path='current-path') <- remove path'ed cookies
// SetCookie: key=value;expires=Date.now() + 365 days;path=/;secure=true
cookieStorageAdapter.delete(key);

// TODO(HuiSF): follow up the default CookieSerializeOptions values
cookieStorageAdapter.set(key, value, {
...defaultSetCookieOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ export declare namespace CookieStorage {
export type SetCookieOptions = Partial<
Pick<
CookieSerializeOptions,
'domain' | 'expires' | 'httpOnly' | 'maxAge' | 'sameSite' | 'secure'
| 'domain'
| 'expires'
| 'httpOnly'
| 'maxAge'
| 'sameSite'
| 'secure'
| 'path'
>
>;

Expand Down

0 comments on commit 60a559f

Please sign in to comment.