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

feat: enhance domain validate with regex, support wildcard domain and path validate #150

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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