Skip to content

Commit a9c62bf

Browse files
committed
feat: Add validation for protocol
1 parent 58e5851 commit a9c62bf

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,33 @@ exports[`
363363
}
364364
`;
365365

366+
exports[`
367+
findFeed(url: "ftp://example.com") { link }
368+
1`] = `
369+
{
370+
"data": null,
371+
"errors": [
372+
{
373+
"extensions": {
374+
"code": "invalid-url",
375+
"message": "Invalid url",
376+
"type": "InvalidUrlError",
377+
},
378+
"locations": [
379+
{
380+
"column": 3,
381+
"line": 2,
382+
},
383+
],
384+
"message": "Invalid url",
385+
"path": [
386+
"findFeed",
387+
],
388+
},
389+
],
390+
}
391+
`;
392+
366393
exports[`
367394
findFeed(url: "https://non--------existing-domain.com") { link }
368395
1`] = `

src/__tests__/api.tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ testGraphqlApi`
6969
findFeed(url: "not-a-url") { link }
7070
`;
7171

72+
testGraphqlApi`
73+
findFeed(url: "ftp://example.com") { link }
74+
`;
75+
7276
testGraphqlApi`
7377
findFeed(url: "https://non--------existing-domain.com") { link }
7478
`;

src/handlers/feed.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export async function parseFromQuery({
5555
endTime?: string;
5656
startTime?: string;
5757
}): Promise<ParserResponse> {
58-
if (!isUrl(url)) {
58+
if (!/^https?/.test(url) || !isUrl(url)) {
5959
throw new InvalidUrlError(url);
6060
}
6161
const response = await request(url);

src/handlers/findFeed.ts

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

6-
import { BaseError, InvalidUrlError } from "../errors.js";
6+
import { InvalidUrlError } from "../errors.js";
77
import { logger } from "../logger.js";
88
import request from "../request.js";
99
import { parseFromQuery, parseFromString } from "./feed.js";
@@ -90,18 +90,20 @@ async function findRssFeedsInDom(
9090
export async function findFeed({
9191
url,
9292
normalize = false,
93+
withGuessFallback = false,
9394
}: {
9495
url: string;
9596
normalize?: boolean;
97+
withGuessFallback?: boolean;
9698
}): Promise<FindFeedResponse[]> {
97-
if (!isUrl(url)) {
99+
if (!/^https?/.test(url) || !isUrl(url)) {
98100
throw new InvalidUrlError(url);
99101
}
100102

101103
const normalizedUrl = normalize ? url : normalizeUrl(url, normalizeOptions);
102104

103105
if (!normalizedUrl) {
104-
throw new BaseError("Empty url is not allowed", "missing-url");
106+
throw new InvalidUrlError(url);
105107
}
106108

107109
const response = await request(normalizedUrl);

0 commit comments

Comments
 (0)