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

fix: check for valid content-length before calling request.formData() for HEAD requests #662

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/empty-rockets-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@shopify/shopify-app-remix": patch
---

Check for non-empty body before calling `request.formData()` in login handler
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ describe('login helper', () => {
it('returns an empty errors object if GET and no shop param', async () => {
// GIVEN
const shopify = shopifyApp(testConfig());
const headers = new Headers();
headers.set('Content-Length', '0');
const requestMock = {
url: `${APP_URL}/auth/login`,
method: 'GET',
headers,
};

// WHEN
Expand All @@ -27,10 +30,13 @@ describe('login helper', () => {
// GIVEN
const formDataMock = jest.fn();
const shopify = shopifyApp(testConfig());
const headers = new Headers();
headers.set('Content-Length', '0');
const requestMock = {
url: `${APP_URL}/auth/login?shop=${TEST_SHOP}`,
method: 'GET',
formData: formDataMock,
headers,
};

// WHEN
Expand All @@ -40,13 +46,37 @@ describe('login helper', () => {
expect(formDataMock).not.toHaveBeenCalled();
});

it('does not access formData if method is GET with an empty body and no shop parameter (HEAD)', async () => {
// GIVEN
const formDataMock = jest.fn();
const shopify = shopifyApp(testConfig());
const headers = new Headers();
headers.set('Content-Length', '0');
const requestMock = {
url: `${APP_URL}/auth/login`,
// HEAD requests will be passed as GET requests to the loader function which calls the login function
method: 'GET',
formData: formDataMock,
headers,
};

// WHEN
const errors = await shopify.login(requestMock as any as Request);

// THEN
expect(errors).toEqual({});
});

it('returns an error if the shop parameter is missing', async () => {
// GIVEN
const shopify = shopifyApp(testConfig());
const headers = new Headers();
headers.set('Content-Length', '0');
const requestMock = {
url: `${APP_URL}/auth/login`,
formData: async () => ({get: () => null}),
method: 'POST',
headers,
};

// WHEN
Expand All @@ -64,12 +94,15 @@ describe('login helper', () => {
async ({urlShop, formShop, method}) => {
// GIVEN
const shopify = shopifyApp(testConfig());
const headers = new Headers();
headers.set('Content-Length', '0');
const requestMock = {
url: urlShop
? `${APP_URL}/auth/login?shop=${urlShop}`
: `${APP_URL}/auth/login`,
formData: async () => ({get: () => formShop}),
method,
headers,
};

// WHEN
Expand Down Expand Up @@ -102,12 +135,16 @@ describe('login helper', () => {
isEmbeddedApp: testCaseConfig.isEmbeddedApp,
});
const shopify = shopifyApp(config);
const headers = new Headers();
headers.set('Content-Length', method === 'POST' ? '123' : '0');
const requestMock = {
url: urlShop
? `${APP_URL}/auth/login?shop=${urlShop}`
: `${APP_URL}/auth/login`,
formData: async () => ({get: () => formShop}),
formData:
method === 'POST' ? async () => ({get: () => formShop}) : undefined,
method,
headers,
};

// WHEN
Expand All @@ -133,10 +170,13 @@ describe('login helper', () => {
isEmbeddedApp: testCaseConfig.isEmbeddedApp,
});
const shopify = shopifyApp(config);
const headers = new Headers();
headers.set('Content-Length', '123');
const requestMock = {
url: `${APP_URL}/auth/login`,
formData: async () => ({get: () => `https://${TEST_SHOP}/`}),
method: 'POST',
headers,
};

// WHEN
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ export function loginFactory(params: BasicParams) {
return async function login(request: Request): Promise<LoginError | never> {
const url = new URL(request.url);
const shopParam = url.searchParams.get('shop');

if (request.method === 'GET' && !shopParam) {
const contentLength = parseInt(
request.headers.get('Content-Length') ?? '0',
10,
);

// A HEAD request will be passed as a GET request, to avoid calling `request.formData()` on an empty body in a HEAD request
// we check if the `Content-Length` header is truthy so it's safe to call `request.formData()`
if (request.method === 'GET' && !shopParam && !contentLength) {
return {};
}

Expand Down