Skip to content

Commit

Permalink
fix: incorrect url parsing (isssue: #10924) (#11528)
Browse files Browse the repository at this point in the history
**Motivation**

fix: #10924


**Test plan**

```tsx
import extractPathFromURL from '@react-navigation/native/src/extractPathFromURL';

test('extractPathFromURL', () => {
  expect(extractPathFromURL(['https://mysite.com'], 'https://mysite.com/readPolicy?url=https://test.com')).toBe(
    '/readPolicy?url=https://test.com',
  );
});
// test fail ^^^ because `extractPathFromURL` removed all double slashes
// Expected: "/readPolicy?url=https://test.com"
// Received: "/readPolicy?url=https:/test.com"
```

Co-authored-by: Michał Osadnik <micosa97@gmail.com>
  • Loading branch information
retyui and osdnk committed Sep 3, 2023
1 parent ba1feba commit fab2cc6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions packages/native/src/__tests__/extractPathFromURL.test.tsx
Expand Up @@ -324,3 +324,12 @@ it('returns undefined for non-matching host with wildcard', () => {
)
).toBeUndefined();
});

it('returns a valid search query when it has a url as param', () => {
expect(
extractPathFromURL(
['https://mysite.com'],
'https://mysite.com/readPolicy?url=https://test.com'
)
).toBe('/readPolicy?url=https://test.com');
});
5 changes: 4 additions & 1 deletion packages/native/src/extractPathFromURL.tsx
Expand Up @@ -15,7 +15,10 @@ export function extractPathFromURL(prefixes: string[], url: string) {
.join('\\.')}`
);

const normalizedURL = url.replace(/\/+/g, '/');
const [originAndPath, searchParams] = url.split('?');
const normalizedURL = originAndPath
.replace(/\/+/g, '/')
.concat(searchParams ? `?${searchParams}` : '');

if (prefixRegex.test(normalizedURL)) {
return normalizedURL.replace(prefixRegex, '');
Expand Down

0 comments on commit fab2cc6

Please sign in to comment.