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

fix(security): require screenshot protocol to be http/https #48

Merged
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
19 changes: 17 additions & 2 deletions src/lib/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@ const zodStringBool = z
.transform(x => x === "true")
.pipe(z.boolean());

const zodStringUrl = z.string().url();
const urlSchema = z
.string()
.url()
.refine(
val => {
try {
const url = new URL(val);
return url.protocol === "http:" || url.protocol === "https:";
} catch (err) {
return false;
}
},
{
message: "must start with http or https",
},
);

export const PlainConfigSchema = z.object({
url: zodStringUrl,
url: urlSchema,
width: z.coerce.number().nullish(),
height: z.coerce.number().nullish(),
viewPortWidth: z.coerce.number().nullish(),
Expand Down
10 changes: 9 additions & 1 deletion src/middlewares/extract_query_params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,15 @@ export function handleExtractQueryParamsMiddleware(encryptionService?: StringEnc
const { validData, errors } = parseForm({ data: input, schema: PlainConfigSchema });

if (errors) {
throw new HTTPException(400, { message: "Invalid query parameters", cause: errors });
let message: string = "Invalid query parameters: ";

const specificErrors = Object.entries(errors).map(([key, value]) => `(${key} - ${value})`).join(" ")

message = `${message} ${specificErrors}`;

console.log(message);

throw new HTTPException(400, { message, cause: errors });
}

if (validData.width && validData.width > 1920) {
Expand Down
12 changes: 12 additions & 0 deletions tests/app.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,18 @@ suite("app", () => {
expect(res.status).toBe(400);
expect(await res.text()).toMatch(/Invalid query/gi);
});

[
"file:///etc/passwd&width=4000",
"view-source:file:///home/&width=4000",
"view-source:file:///home/ec2-user/url-to-png/.env",
].forEach(invalidDomain => {
it(`throws when invalid protocol ${invalidDomain}`, async () => {
const res = await app.request(`/?url=${invalidDomain}`);
expect(res.status).toBe(400);
expect(await res.text()).toMatch(/url - must start with http or https/gi);
});
});
});

describe("GET /?hash=", () => {
Expand Down