Add getUserTokenMinusSQLInjection to prevent SQL Injection in via the
Some checks are pending
Build and Push Docker Image / build-and-push (push) Waiting to run

cookies (that may be not possible, but it is a safety guard I want to
add. (Chat: https://t3.chat/chat/c1883e6a-6c38-4af3-9818-0e927449c61c)
This commit is contained in:
yuanhau 2025-06-10 09:39:11 +08:00
parent bd3a81dfbc
commit 2895263e52
10 changed files with 90 additions and 54 deletions

View file

@ -1,17 +1,9 @@
import sql from "~/server/components/postgres";
import getUserTokenMinusSQLInjection from "~/server/components/getUserToken";
export default defineEventHandler(async (event) => {
// Check user data.
const userToken = getCookie(event, "token");
if (!userToken) {
return {
error: "ERR_NOT_ALLOWED",
};
}
const checkUserToken = await sql`
select * from usertokens
where token=${userToken}
`;
if (checkUserToken.length === 0) {
const token = await getUserTokenMinusSQLInjection(event);
if (token.error.length !== 0) {
return {
error: "ERR_NOT_ALLOWED",
};
@ -37,26 +29,11 @@ export default defineEventHandler(async (event) => {
`
UPDATE user_other_data SET ${requestChange} = $1
WHERE username = $2`,
[apiKeyqq[0], checkUserToken[0].username],
[apiKeyqq[0], token.user],
);
/**
* // Example of how requestChange might be validated
const allowedColumns = ['groq_api_key', 'another_column_name'];
if (!allowedColumns.includes(requestChange)) {
throw new Error('Invalid column name provided');
}
const sqlC = await sql`
UPDATE user_other_data SET ${sql.identifier([requestChange])} = ${apiKeyqq[0]}
WHERE username = ${checkUserToken[0].username}`;
*/
return {
body: body,
allowed: allowed,
data: body.value.match(clearBadDataRegex),
sqlC: sqlC,
success: true,
};
}
});