Skip to content

Commit e3c5a96

Browse files
CodingWithHardikCopilot
andcommitted
fix(security): security issue fixed
Co-authored-by: Copilot <copilot@github.com>
1 parent edb3f04 commit e3c5a96

4 files changed

Lines changed: 61 additions & 3 deletions

File tree

src/(auth)/_auth.login.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export default {
5858
passwordchangeAt: fetchedUser.passwordchangeAt,
5959
role: fetchedUser.platformRole,
6060
reftoken: exptoken,
61-
exp: Date.now() + 3 * 24 * 60 * 60 * 1000
61+
tokenExpiry: Date.now() + 3 * 24 * 60 * 60 * 1000
6262
}
6363
let encrypted = clipher.update(JSON.stringify(data), 'utf8', 'hex')
6464
encrypted += clipher.final('hex');

src/(auth)/_auth.regenerate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export default {
5757
passwordchangeAt: device.user.passwordchangeAt,
5858
role: device.user.platformRole,
5959
reftoken: device.token,
60-
exp: Date.now() + 3 * 24 * 60 * 60 * 1000
60+
tokenExpiry: Date.now() + 3 * 24 * 60 * 60 * 1000
6161
}
6262
const clipher = crypto.createCipheriv("aes-256-cbc", key, ivcode)
6363
let encrypted = clipher.update(JSON.stringify(data), 'utf8', 'hex')

src/(auth)/_auth.register.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ export default {
8080
passwordchangeAt: createdUser.passwordchangeAt,
8181
role: createdUser.platformRole,
8282
reftoken: exptoken,
83-
exp: Date.now() + 3 * 24 * 60 * 60 * 1000
83+
tokenExpiry: Date.now() + 3 * 24 * 60 * 60 * 1000
8484
}
8585
let encrypted = clipher.update(JSON.stringify(data), 'utf8', 'hex')
8686
encrypted += clipher.final('hex');

src/(auth)/_auth.resend.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Static, t } from "elysia";
2+
import { prisma } from "../../lib/prisma";
3+
import jwt from "jsonwebtoken";
4+
import crypto from "crypto";
5+
6+
const AuthSchema = t.Object({
7+
authToken: t.String(),
8+
})
9+
10+
export default {
11+
post: {
12+
handler: async ({ body, request }: { body: Static<typeof AuthSchema>, request: Request }) => {
13+
if (process.env.UNAUTHORIZED_BYPASS_ENABLED !== "true" && request.headers.get("origin") !== process.env.MAIN_WEBSITE_URL) return new Response("Unauthorized", { status: 401 })
14+
let tokenpayload;
15+
try {
16+
tokenpayload = JSON.parse(Buffer.from(body.authToken.split(".")[1], "base64").toString("utf-8"))
17+
} catch {
18+
return new Response("Unauthorized", { status: 401 });
19+
}
20+
if (!tokenpayload?.token) return new Response("Unauthorized", { status: 401 });
21+
const [encryptedtoken, icodev] = String(tokenpayload.token).split(":")
22+
if (!encryptedtoken || !icodev) return new Response("Unauthorized", { status: 401 });
23+
const device = await prisma.devices.findUnique({
24+
where: {
25+
icodev: icodev,
26+
},
27+
include: {
28+
user: true
29+
}
30+
})
31+
if (!device || !device?.user || !device?.token) return new Response("Unauthorized", { status: 401 });
32+
let jsondecoded;
33+
try {
34+
jsondecoded = jwt.verify(body.authToken, device.user.randompwd)
35+
} catch (err: any) {
36+
return new Response("Unauthorized", { status: 401 });
37+
}
38+
if (typeof jsondecoded !== "object" || jsondecoded === null || !(jsondecoded as any).token) return new Response("Unauthorized", { status: 401 });
39+
const tokensep = String((jsondecoded as any).token).split(":")
40+
if (tokensep.length !== 2) return new Response("Unauthorized", { status: 401 });
41+
const key = new TextEncoder().encode(device.user.hashedpwd.split('').reverse().join('')).slice(0, 32)
42+
const ivcode = Buffer.from(device.ivcode, "base64");
43+
const decipher = crypto.createDecipheriv("aes-256-cbc", key, ivcode)
44+
let decrypted = decipher.update(tokensep[0], "hex", "utf8")
45+
decrypted += decipher.final("utf8")
46+
const decrypteddata = JSON.parse(decrypted);
47+
if (!decrypteddata || typeof decrypteddata !== "object") return new Response("Unauthorized", { status: 401 });
48+
if (decrypteddata.tokenExpiry < Date.now()) return new Response("Unauthorized", { status: 401 });
49+
if (decrypteddata.reftoken !== device.token) return new Response("Unauthorized", { status: 401 });
50+
if (decrypteddata.email !== device.user.email || decrypteddata.id !== device.user.id || decrypteddata.verified !== device.user.emailVerified || decrypteddata.createdAt !== device.user.createdAt.toISOString() || decrypteddata.passwordchangeAt !== device.user.passwordchangeAt?.toISOString() || decrypteddata.role !== device.user.platformRole) return new Response("Unauthorized", { status: 401 });
51+
if (device.user.emailVerified) return new Response("Email already verified", { status: 400 });
52+
53+
},
54+
schema: {
55+
body: AuthSchema
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)