Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions __tests__/redirector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,22 @@ describe('GET /r/:postId', () => {
.expect(302)
.expect('Location', 'http://localhost:5002/posts/p1-p1');
});

it('should not escape already encoded URL', async () => {
await con
.getRepository(ArticlePost)
.update(
{ id: 'p1' },
{ url: 'http://p1.com/hello%20world/%f0%9f%9a%80-to-the-🌔' },
);
return request(app.server)
.get('/r/p1')
.expect(302)
.expect(
'Location',
'http://p1.com/hello%20world/%f0%9f%9a%80-to-the-%F0%9F%8C%94?ref=dailydev',
);
});
});

describe('GET /:id/profile-image', () => {
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
"deepmerge": "^4.3.1",
"dotenv": "17.2.2",
"emoji-regex": "^10.5.0",
"encodeurl": "^2.0.0",
"eventsource": "^2.0.2",
"fast-json-stringify": "^6.0.1",
"fastify": "^5.6.0",
Expand Down Expand Up @@ -148,6 +149,7 @@
"@faker-js/faker": "^9.8.0",
"@fastify/static": "^8.2.0",
"@swc/core": "^1.13.5",
"@types/encodeurl": "^1.0.3",
"@types/express": "^5.0.3",
"@types/humanize-duration": "^3.27.4",
"@types/jest": "^29.5.14",
Expand Down
17 changes: 17 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion src/routes/redirector.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { URL } from 'url';
import { FastifyInstance } from 'fastify';
import encodeurl from 'encodeurl';
import { ArticlePost, Post } from '../entity';
import { getDiscussionLink, notifyView } from '../common';
import createOrGetConnection from '../db';
Expand Down Expand Up @@ -35,7 +36,7 @@ export default async function (fastify: FastifyInstance): Promise<void> {
}
const url = new URL(post.url);
url.searchParams.append('ref', 'dailydev');
const encodedUri = encodeURI(url.href);
const encodedUri = encodeurl(url.href);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why we encode in the first place or just during redirect, feels like it should be a once off in yggdrasil or when saving post, but that is something in general

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fully sure, it was first added in 9dab364

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess there could be edgecases somewhere somehow?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah no point in dwelling that part for now

if (req.isBot) {
return res.status(302).redirect(encodedUri);
}
Expand Down
Loading