Skip to content

Commit cd37c3e

Browse files
committed
check for valid host when reseting password
1 parent df70860 commit cd37c3e

File tree

1 file changed

+26
-1
lines changed
  • apps/web/pages/api/forgot-password

1 file changed

+26
-1
lines changed

apps/web/pages/api/forgot-password/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,37 @@ import { prisma } from '@linen/database';
55
import { getHostFromHeaders } from '@linen/utilities/domain';
66
import { cors, preflight } from 'utilities/cors';
77

8+
async function checkDomain(host: string) {
9+
if (process.env.NODE_ENV === 'development') {
10+
return true;
11+
}
12+
const domain = host.substring(host.lastIndexOf('/') + 1);
13+
if (domain === 'linen.dev' || domain === 'www.linen.dev') {
14+
return true;
15+
}
16+
const exist = await prisma.accounts.findFirst({
17+
where: { redirectDomain: domain },
18+
});
19+
if (exist) {
20+
return true;
21+
}
22+
return false;
23+
}
24+
825
async function create(request: NextApiRequest, response: NextApiResponse) {
926
const { email, origin } = JSON.parse(request.body);
1027

1128
if (!email) {
1229
return response.status(400).json({ error: 'Email is required' });
1330
}
31+
32+
const host = origin || getHostFromHeaders(request.headers);
33+
const isValidDomain = await checkDomain(host);
34+
35+
if (!isValidDomain) {
36+
return response.status(400).json({ error: 'Invalid domain' });
37+
}
38+
1439
try {
1540
const token = generateToken();
1641

@@ -29,7 +54,7 @@ async function create(request: NextApiRequest, response: NextApiResponse) {
2954

3055
await ResetPasswordMailer.send({
3156
to: email,
32-
host: origin || getHostFromHeaders(request.headers),
57+
host,
3358
token,
3459
});
3560
} catch (exception) {

0 commit comments

Comments
 (0)