Skip to content

Commit

Permalink
check for valid host when reseting password
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrodesouza committed Apr 3, 2024
1 parent df70860 commit cd37c3e
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion apps/web/pages/api/forgot-password/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,37 @@ import { prisma } from '@linen/database';
import { getHostFromHeaders } from '@linen/utilities/domain';
import { cors, preflight } from 'utilities/cors';

async function checkDomain(host: string) {
if (process.env.NODE_ENV === 'development') {
return true;
}
const domain = host.substring(host.lastIndexOf('/') + 1);
if (domain === 'linen.dev' || domain === 'www.linen.dev') {
return true;
}
const exist = await prisma.accounts.findFirst({
where: { redirectDomain: domain },
});
if (exist) {
return true;
}
return false;
}

async function create(request: NextApiRequest, response: NextApiResponse) {
const { email, origin } = JSON.parse(request.body);

if (!email) {
return response.status(400).json({ error: 'Email is required' });
}

const host = origin || getHostFromHeaders(request.headers);
const isValidDomain = await checkDomain(host);

if (!isValidDomain) {
return response.status(400).json({ error: 'Invalid domain' });
}

try {
const token = generateToken();

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

await ResetPasswordMailer.send({
to: email,
host: origin || getHostFromHeaders(request.headers),
host,
token,
});
} catch (exception) {
Expand Down

0 comments on commit cd37c3e

Please sign in to comment.