Skip to content
Open
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
21 changes: 17 additions & 4 deletions src/sources/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,31 @@ export const createHTTPSource: SourceFactory<HTTPSourceOptions> = (options) => {
if (!HTTP_RE.test(d)) {
d = "http://" + d;
}
return new URL(d).hostname;
const { hostname, pathname } = new URL(d);
const regExpUrl = `${hostname}${pathname}`.replaceAll("*.", "(\\w*.)?");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Allow for multiple subdomains?

Suggested change
const regExpUrl = `${hostname}${pathname}`.replaceAll("*.", "(\\w*.)?");
const regExpUrl = `${hostname}${pathname}`.replaceAll("*.", "(\\w*.)+?");

return new RegExp(`^${regExpUrl}`);
})
.filter(Boolean)
);
const validateDomain = (requestUrl: string): boolean => {
for (const domain of domains) {
if (domain.test(requestUrl)) {
return true;
}
}
return false;
};

return async (id: string, requestOptions) => {
// Check hostname
const hostname = new URL(id).hostname;
// Check hostname and path ( include wildcard subdomain )
const { hostname, pathname } = new URL(id);
if (!hostname) {
throw createError("Hostname is missing", 403, id);
}
if (!requestOptions?.bypassDomain && !domains.has(hostname)) {
if (
!requestOptions?.bypassDomain &&
!validateDomain(`${hostname}${pathname}`)
) {
throw createError("Forbidden host", 403, hostname);
}

Expand Down