Skip to content

Commit 3b9dd56

Browse files
authored
Merge pull request quirrel-dev#796 from quirrel-dev/regression-absolute-endpoints
only allow absolute endpoints
2 parents 723fce8 + 2025d96 commit 3b9dd56

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/api/scheduler/routes/queues.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { QueuesUpdateCronBody } from "../types/queues/update-cron";
1313
import { isValidCronExpression } from "../../../shared/is-valid-cron";
1414
import { isValidTimezone } from "../../../shared/repeat";
1515

16+
import * as Url from "url"
17+
1618
const jobs: FastifyPluginCallback = (fastify, opts, done) => {
1719
const jobsRepo = fastify.jobs;
1820
const queueRepo = jobsRepo.queueRepo;
@@ -35,6 +37,11 @@ const jobs: FastifyPluginCallback = (fastify, opts, done) => {
3537
return true;
3638
}
3739

40+
function isAbsoluteURL(string: string): boolean {
41+
const url = Url.parse(string);
42+
return Boolean(url.protocol && url.hostname);
43+
}
44+
3845
const baseSchema = {
3946
tags: ["Queueing"],
4047
security: fastify.adminBasedAuthEnabled
@@ -54,6 +61,12 @@ const jobs: FastifyPluginCallback = (fastify, opts, done) => {
5461
"body.repeat.cron uses unsupported syntax. See https://github.com/harrisiirak/cron-parser for reference.",
5562
};
5663

64+
const INVALID_ENDPOINT_ERROR = {
65+
statusCode: 400,
66+
error: "Bad Request",
67+
message: "endpoint needs to be absolute URL.",
68+
};
69+
5770
const INVALID_TIMEZONE_ERROR = {
5871
statusCode: 400,
5972
error: "Bad Request",
@@ -77,6 +90,10 @@ const jobs: FastifyPluginCallback = (fastify, opts, done) => {
7790
const { tokenId, body } = request;
7891
const { endpoint } = request.params;
7992

93+
if (!isAbsoluteURL(endpoint)) {
94+
return reply.status(400).send(INVALID_ENDPOINT_ERROR);
95+
}
96+
8097
if (!hasValidCronExpression(body)) {
8198
return reply.status(400).send(INVALID_CRON_EXPRESSION_ERROR);
8299
}
@@ -127,6 +144,10 @@ const jobs: FastifyPluginCallback = (fastify, opts, done) => {
127144
);
128145
}
129146

147+
if (!isAbsoluteURL(endpoint)) {
148+
return reply.status(400).send(INVALID_ENDPOINT_ERROR);
149+
}
150+
130151
if (!body.every(hasValidCronExpression)) {
131152
return reply.status(400).send(INVALID_CRON_EXPRESSION_ERROR);
132153
}

src/api/test/jobs.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,21 @@ describeAcrossBackends("Jobs", (backend) => {
542542
expect(bodies).toEqual(["delay & repeat.every", "delay & repeat.every"]);
543543
});
544544

545+
test("regression: non-absolute URLs shouldn't be accepted", async () => {
546+
await request(quirrel)
547+
.post(
548+
"/queues/" + encodeURIComponent("https://${SOME_ENV_VAR}/api/someQueue")
549+
)
550+
.send({
551+
body: "something",
552+
})
553+
.expect(400, {
554+
statusCode: 400,
555+
error: "Bad Request",
556+
message: "endpoint needs to be absolute URL.",
557+
});
558+
});
559+
545560
describe("cron jobs", () => {
546561
test("work", async () => {
547562
await request(quirrel)

0 commit comments

Comments
 (0)