Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate post length and comment length and url #195

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/frontpage/app/api/receive_hook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { atprotoGetRecord } from "@/lib/data/atproto/record";
import { Commit } from "@/lib/data/atproto/event";
import * as atprotoPost from "@/lib/data/atproto/post";
import * as dbPost from "@/lib/data/db/post";
import { CommentCollection, getComment } from "@/lib/data/atproto/comment";
import * as atprotoComment from "@/lib/data/atproto/comment";
import { VoteRecord } from "@/lib/data/atproto/vote";
import { getPdsUrl } from "@/lib/data/atproto/did";
import {
Expand Down Expand Up @@ -64,9 +64,9 @@ export async function POST(request: Request) {
}
}
// repo is actually the did of the user
if (collection === CommentCollection) {
if (collection === atprotoComment.CommentCollection) {
if (op.action === "create") {
const comment = await getComment({ rkey, repo });
const comment = await atprotoComment.getComment({ rkey, repo });

await unauthed_createComment({
cid: comment.cid,
Expand Down Expand Up @@ -104,7 +104,8 @@ export async function POST(request: Request) {
cid: hydratedRecord.cid,
});
} else if (
hydratedVoteRecordValue.subject.uri.collection === CommentCollection
hydratedVoteRecordValue.subject.uri.collection ===
atprotoComment.CommentCollection
) {
await unauthed_createCommentVote({
cid: hydratedRecord.cid,
Expand Down
10 changes: 8 additions & 2 deletions packages/frontpage/lib/data/atproto/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import { DataLayerError } from "../error";
import { z } from "zod";
import { PostCollection } from "./post";
import { DID, getPdsUrl } from "./did";
import { MAX_COMMENT_LENGTH } from "../db/constants";

export const CommentCollection = "fyi.unravel.frontpage.comment";

export const CommentRecord = z.object({
content: z.string(),
content: z.string().max(MAX_COMMENT_LENGTH),
parent: z
.object({
cid: z.string(),
Expand Down Expand Up @@ -53,7 +54,12 @@ export async function createComment({ parent, post, content }: CommentInput) {
createdAt: new Date().toISOString(),
};

CommentRecord.parse(record);
const parseResult = CommentRecord.safeParse(record);
if (!parseResult.success) {
throw new DataLayerError("Invalid comment record", {
cause: parseResult.error,
});
}

const result = await atprotoCreateRecord({
record,
Expand Down
12 changes: 9 additions & 3 deletions packages/frontpage/lib/data/atproto/post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ import {
import { z } from "zod";
import { DataLayerError } from "../error";
import { DID, getPdsUrl } from "./did";
import { MAX_POST_TITLE_LENGTH, MAX_POST_URL_LENGTH } from "../db/constants";

export const PostCollection = "fyi.unravel.frontpage.post";

export const PostRecord = z.object({
title: z.string(),
url: z.string(),
title: z.string().max(MAX_POST_TITLE_LENGTH),
url: z.string().url().max(MAX_POST_URL_LENGTH),
createdAt: z.string(),
});

Expand All @@ -25,7 +26,12 @@ type PostInput = {

export async function createPost({ title, url }: PostInput) {
const record = { title, url, createdAt: new Date().toISOString() };
PostRecord.parse(record);
const parseResult = PostRecord.safeParse(record);
if (!parseResult.success) {
throw new DataLayerError("Invalid post record", {
cause: parseResult.error,
});
}

const result = await atprotoCreateRecord({
record,
Expand Down