Skip to content

Strip any number of trailing slashes from realm URLs #3819

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 3 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/boot/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,15 @@ const migrations: { [string]: (GlobalState) => GlobalState } = {
},
};
},

// Fixes #3567 for users with cached realm urls with multiple trailing slashes.
'11': state => ({
...state,
accounts: state.accounts.map(a => ({
...a,
realm: a.realm.replace(/\/+$/, ''),
})),
}),
};

const reduxPersistConfig: Config = {
Expand Down
4 changes: 4 additions & 0 deletions src/utils/__tests__/url-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,10 @@ describe('fixRealmUrl', () => {
expect(fixRealmUrl('https://example.com/')).toEqual('https://example.com');
});

test('when a realm url has two trailing "/" remove them', () => {
expect(fixRealmUrl('https://example.com//')).toEqual('https://example.com');
});

test('when input url is correct, do not change it', () => {
expect(fixRealmUrl('https://example.com')).toEqual('https://example.com');
});
Expand Down
4 changes: 1 addition & 3 deletions src/utils/url.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ export type Protocol = 'https://' | 'http://';

const protocolRegex = /^\s*((?:http|https):\/\/)(.*)$/;

/** DEPRECATED */
const hasProtocol = (url: string = '') => url.search(protocolRegex) !== -1;

// Split a (possible) URL into protocol and non-protocol parts.
Expand All @@ -79,14 +78,13 @@ export const parseProtocol = (value: string): [Protocol | null, string] => {
return [null, value];
};

/** DEPRECATED */
export const fixRealmUrl = (url: string = '') => {
if (url === '') {
return '';
}
const trimmedUrl = url
.replace(/\s/g, '') // strip any spaces, internal or otherwise
.replace(/\/$/g, ''); // eliminate trailing slash
.replace(/\/+$/, ''); // eliminate trailing slash(es)

return hasProtocol(trimmedUrl) ? trimmedUrl : `https://${trimmedUrl}`;
};
Expand Down