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

Improving the urls to not break protocols and adding tests #3995

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions shared/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export const USER_PRESENCE_INTERVAL = 5000;

export const MAX_AVATAR_DISPLAY = 6;

// Protocols that need to be sanitized
export const blockedProtocols = ["javascript://", "file://"];
11 changes: 11 additions & 0 deletions shared/utils/urls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,15 @@ describe("sanitizeUrl Method", () => {
);
});
});

describe("Blocked protocols", () => {
it("should be sanitized", () => {
expect(urlsUtils.sanitizeUrl("file://localhost/outline.txt")).toEqual(
"https://file://localhost/outline.txt"
);
expect(urlsUtils.sanitizeUrl("javascript://whatever")).toEqual(
"https://javascript://whatever"
);
});
});
});
13 changes: 8 additions & 5 deletions shared/utils/urls.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { blockedProtocols } from "../constants";
import env from "../env";
import { parseDomain } from "./domains";

Expand Down Expand Up @@ -86,12 +87,14 @@ export function sanitizeUrl(url: string | null | undefined) {
return undefined;
}

const protocol = url.match(/(\w+):\/\//);

if (
!isUrl(url) &&
!url.startsWith("/") &&
!url.startsWith("#") &&
!url.startsWith("mailto:") &&
!url.includes("://")
(!isUrl(url) &&
!url.startsWith("/") &&
!url.startsWith("#") &&
!url.startsWith("mailto:")) ||
(protocol && blockedProtocols.includes(protocol[0]))
tommoor marked this conversation as resolved.
Show resolved Hide resolved
) {
return `https://${url}`;
}
Expand Down