Skip to content

Commit 8591cbe

Browse files
committed
fix: it should handle already encoded parts in the URL when redirecting
1 parent a93d801 commit 8591cbe

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

__tests__/redirector.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,22 @@ describe('GET /r/:postId', () => {
105105
.expect(302)
106106
.expect('Location', 'http://localhost:5002/posts/p1-p1');
107107
});
108+
109+
it('should not escape already encoded URL', async () => {
110+
await con
111+
.getRepository(ArticlePost)
112+
.update(
113+
{ id: 'p1' },
114+
{ url: 'http://p1.com/hello%world/%f0%9f%9a%80-to-the-🌔' },
115+
);
116+
return request(app.server)
117+
.get('/r/p1')
118+
.expect(302)
119+
.expect(
120+
'Location',
121+
'http://p1.com/hello%world/%f0%9f%9a%80-to-the-%F0%9F%8C%94?ref=dailydev',
122+
);
123+
});
108124
});
109125

110126
describe('GET /:id/profile-image', () => {

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
"deepmerge": "^4.3.1",
9797
"dotenv": "17.2.2",
9898
"emoji-regex": "^10.5.0",
99+
"encodeurl": "^2.0.0",
99100
"eventsource": "^2.0.2",
100101
"fast-json-stringify": "^6.0.1",
101102
"fastify": "^5.6.0",
@@ -148,6 +149,7 @@
148149
"@faker-js/faker": "^9.8.0",
149150
"@fastify/static": "^8.2.0",
150151
"@swc/core": "^1.13.5",
152+
"@types/encodeurl": "^1.0.3",
151153
"@types/express": "^5.0.3",
152154
"@types/humanize-duration": "^3.27.4",
153155
"@types/jest": "^29.5.14",

pnpm-lock.yaml

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/routes/redirector.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { URL } from 'url';
22
import { FastifyInstance } from 'fastify';
3+
import encodeurl from 'encodeurl';
34
import { ArticlePost, Post } from '../entity';
45
import { getDiscussionLink, notifyView } from '../common';
56
import createOrGetConnection from '../db';
@@ -35,7 +36,7 @@ export default async function (fastify: FastifyInstance): Promise<void> {
3536
}
3637
const url = new URL(post.url);
3738
url.searchParams.append('ref', 'dailydev');
38-
const encodedUri = encodeURI(url.href);
39+
const encodedUri = encodeurl(url.href);
3940
if (req.isBot) {
4041
return res.status(302).redirect(encodedUri);
4142
}

0 commit comments

Comments
 (0)