Skip to content

Commit 4620b01

Browse files
committed
feat: Add url validation on findFeed
1 parent f95897a commit 4620b01

File tree

4 files changed

+84
-1
lines changed

4 files changed

+84
-1
lines changed

src/__tests__/__snapshots__/api.tests.ts.snap

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,35 @@ exports[`
334334
}
335335
`;
336336

337+
exports[`
338+
findFeed(url: "https://non--------existing-domain.com") {
339+
link
340+
}
341+
1`] = `
342+
{
343+
"data": null,
344+
"errors": [
345+
{
346+
"extensions": {
347+
"code": "dns-lookup-error",
348+
"message": "Could not find domain",
349+
"type": "DnsLookupError",
350+
},
351+
"locations": [
352+
{
353+
"column": 3,
354+
"line": 2,
355+
},
356+
],
357+
"message": "Could not find domain",
358+
"path": [
359+
"findFeed",
360+
],
361+
},
362+
],
363+
}
364+
`;
365+
337366
exports[`
338367
findFeed(url: "https://rolflekang.com") {
339368
link
@@ -352,3 +381,32 @@ exports[`
352381
},
353382
}
354383
`;
384+
385+
exports[`
386+
findFeed(url: "not-a-url") {
387+
link
388+
}
389+
1`] = `
390+
{
391+
"data": null,
392+
"errors": [
393+
{
394+
"extensions": {
395+
"code": "invalid-url",
396+
"message": "Invalid url",
397+
"type": "InvalidUrlError",
398+
},
399+
"locations": [
400+
{
401+
"column": 3,
402+
"line": 2,
403+
},
404+
],
405+
"message": "Invalid url",
406+
"path": [
407+
"findFeed",
408+
],
409+
},
410+
],
411+
}
412+
`;

src/__tests__/api.tests.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,15 @@ testGraphqlApi`
6262
items { title }
6363
}
6464
`;
65+
66+
testGraphqlApi`
67+
findFeed(url: "not-a-url") {
68+
link
69+
}
70+
`;
71+
72+
testGraphqlApi`
73+
findFeed(url: "https://non--------existing-domain.com") {
74+
link
75+
}
76+
`;

src/errors.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@ export class ConnectionFailedError extends BaseError {
139139
}
140140
}
141141

142+
export class InvalidUrlError extends BaseError {
143+
url: string;
144+
constructor(url: string) {
145+
super("Invalid url", "invalid-url");
146+
this.url = url;
147+
}
148+
}
149+
142150
export function createErrorFormatter(
143151
Sentry: any,
144152
): ApolloServerOptions<any>["formatError"] {

src/handlers/findFeed.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import type { CheerioAPI } from "cheerio";
33
import type { Element, Node } from "domhandler";
44
import normalizeUrl from "normalize-url";
55

6-
import { BaseError } from "../errors.js";
6+
import { BaseError, InvalidUrlError } from "../errors.js";
77
import { logger } from "../logger.js";
88
import request from "../request.js";
99
import { parseFromQuery, parseFromString } from "./feed.js";
10+
import isUrl from "is-url";
1011

1112
type FindFeedResponse = {
1213
title: string;
@@ -93,6 +94,10 @@ export async function findFeed({
9394
url: string;
9495
normalize?: boolean;
9596
}): Promise<FindFeedResponse[]> {
97+
if (!isUrl(url)) {
98+
throw new InvalidUrlError(url);
99+
}
100+
96101
const normalizedUrl = normalize ? url : normalizeUrl(url, normalizeOptions);
97102

98103
if (!normalizedUrl) {

0 commit comments

Comments
 (0)