Skip to content

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

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

Merged
merged 7 commits into from
Jul 19, 2024
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
Loading