Fix SIGSEGV bug!!

This commit is contained in:
Jesse Wierzbinski 2023-11-26 11:54:20 -10:00
parent 9514deb330
commit 63b13ca5e8
No known key found for this signature in database
5 changed files with 48 additions and 31 deletions

BIN
bun.lockb

Binary file not shown.

View file

@ -9,7 +9,6 @@ import type { StatusWithRelations } from "./Status";
/** /**
* Represents a Like entity in the database. * Represents a Like entity in the database.
*/ */
export const toLysand = (like: Like): LysandLike => { export const toLysand = (like: Like): LysandLike => {
return { return {
id: like.id, id: like.id,
@ -21,6 +20,11 @@ export const toLysand = (like: Like): LysandLike => {
}; };
}; };
/**
* Create a like
* @param user User liking the status
* @param status Status being liked
*/
export const createLike = async ( export const createLike = async (
user: UserWithRelations, user: UserWithRelations,
status: StatusWithRelations status: StatusWithRelations
@ -47,6 +51,11 @@ export const createLike = async (
} }
}; };
/**
* Delete a like
* @param user User deleting their like
* @param status Status being unliked
*/
export const deleteLike = async ( export const deleteLike = async (
user: UserWithRelations, user: UserWithRelations,
status: StatusWithRelations status: StatusWithRelations
@ -68,7 +77,7 @@ export const deleteLike = async (
}, },
}); });
if (user.instanceId === null) { if (user.instanceId === null && status.author.instanceId !== null) {
// User is local, federate the delete // User is local, federate the delete
// TODO: Federate this // TODO: Federate this
} }

View file

@ -4,7 +4,6 @@ import type { MatchedRoute } from "bun";
import chalk from "chalk"; import chalk from "chalk";
import { appendFile } from "fs/promises"; import { appendFile } from "fs/promises";
import { matches } from "ip-matching"; import { matches } from "ip-matching";
import "reflect-metadata";
import type { AuthData } from "~database/entities/User"; import type { AuthData } from "~database/entities/User";
import { getFromRequest } from "~database/entities/User"; import { getFromRequest } from "~database/entities/User";
import type { APIRouteMeta } from "~types/api"; import type { APIRouteMeta } from "~types/api";
@ -121,7 +120,7 @@ Bun.serve({
} }
} }
return file.default(req, matchedRoute, auth); return await file.default(req.clone(), matchedRoute, auth);
} else { } else {
return new Response(undefined, { return new Response(undefined, {
status: 404, status: 404,

View file

@ -78,10 +78,7 @@
"isomorphic-dompurify": "^1.9.0", "isomorphic-dompurify": "^1.9.0",
"jsonld": "^8.3.1", "jsonld": "^8.3.1",
"marked": "^9.1.2", "marked": "^9.1.2",
"pg": "^8.11.3",
"prisma": "latest", "prisma": "latest",
"reflect-metadata": "^0.1.13", "sharp": "^0.32.6"
"sharp": "^0.32.6",
"typeorm": "^0.3.17"
} }
} }

View file

@ -7,6 +7,7 @@
*/ */
export async function parseRequest<T>(request: Request): Promise<Partial<T>> { export async function parseRequest<T>(request: Request): Promise<Partial<T>> {
const query = new URL(request.url).searchParams; const query = new URL(request.url).searchParams;
let output: Partial<T> = {};
// Parse SearchParams arrays into JSON arrays // Parse SearchParams arrays into JSON arrays
const arrayKeys = [...query.keys()].filter(key => key.endsWith("[]")); const arrayKeys = [...query.keys()].filter(key => key.endsWith("[]"));
@ -17,24 +18,46 @@ export async function parseRequest<T>(request: Request): Promise<Partial<T>> {
query.append(key, JSON.stringify(value)); query.append(key, JSON.stringify(value));
} }
// If body is empty const queryEntries = [...query.entries()];
if (request.body === null) {
return {}; if (queryEntries.length > 0) {
const data: Record<string, string | string[]> = {};
const arrayKeys = [...query.keys()].filter(key => key.endsWith("[]"));
for (const key of arrayKeys) {
const value = query.getAll(key);
query.delete(key);
// @ts-expect-error JSON arrays are valid
data[key] = JSON.parse(value);
}
output = {
...output,
...(data as T),
};
} }
// if request contains a JSON body // if request contains a JSON body
if (request.headers.get("Content-Type")?.includes("application/json")) { if (request.headers.get("Content-Type")?.includes("application/json")) {
return (await request.json()) as T; try {
output = {
...output,
...((await request.json()) as T),
};
} catch {
// Invalid JSON
}
} }
// If request contains FormData // If request contains FormData
if (request.headers.get("Content-Type")?.includes("multipart/form-data")) { if (request.headers.get("Content-Type")?.includes("multipart/form-data")) {
const formData = await request.formData(); const formData = [...(await request.formData()).entries()];
if ([...formData.entries()].length > 0) { if (formData.length > 0) {
const data: Record<string, string | File> = {}; const data: Record<string, string | File> = {};
for (const [key, value] of formData.entries()) { for (const [key, value] of formData) {
// If object, parse as JSON // If object, parse as JSON
try { try {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-base-to-string // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-base-to-string
@ -51,23 +74,12 @@ export async function parseRequest<T>(request: Request): Promise<Partial<T>> {
} }
} }
return data as T; output = {
...output,
...(data as T),
};
} }
} }
if ([...query.entries()].length > 0) { return output;
const data: Record<string, string | string[]> = {};
for (const [key, value] of query.entries()) {
try {
data[key] = JSON.parse(value) as string[];
} catch {
data[key] = value.toString();
}
}
return data as T;
}
return {};
} }