mirror of
https://github.com/outline/outline.git
synced 2026-12-01 01:14:11 +03:00
fix: Improved resiliency to invalid GitLab data (#11669)
This commit is contained in:
1 parent
3d1c2a8759
commit
c875930430
4 files changed
+105
-65
No files matched your search
+24
-8
@@ -30,9 +30,15 @@ export const isURLMentionable = ({
|
||||
case IntegrationService.GitLab: {
|
||||
const settings =
|
||||
integration.settings as IntegrationSettings<IntegrationType.Embed>;
|
||||
const gitlabHostname = settings.gitlab?.url
|
||||
? new URL(settings.gitlab?.url).hostname
|
||||
: undefined;
|
||||
let gitlabHostname: string | undefined;
|
||||
try {
|
||||
gitlabHostname = settings.gitlab?.url
|
||||
? new URL(settings.gitlab.url).hostname
|
||||
: undefined;
|
||||
} catch {
|
||||
// Invalid URL stored in settings
|
||||
return false;
|
||||
}
|
||||
|
||||
return hostname === "gitlab.com" || hostname === gitlabHostname;
|
||||
}
|
||||
@@ -68,11 +74,21 @@ export const determineMentionType = ({
|
||||
}
|
||||
|
||||
case IntegrationService.GitLab: {
|
||||
return pathname.includes("merge_requests")
|
||||
? MentionType.PullRequest
|
||||
: pathname.includes("issues")
|
||||
? MentionType.Issue
|
||||
: undefined;
|
||||
const hasShowParam = url.searchParams.has("show");
|
||||
|
||||
if (
|
||||
/\/-\/merge_requests\/\d+/.test(pathname) ||
|
||||
(/\/-\/merge_requests\/?$/.test(pathname) && hasShowParam)
|
||||
) {
|
||||
return MentionType.PullRequest;
|
||||
}
|
||||
if (
|
||||
/\/-\/issues\/\d+/.test(pathname) ||
|
||||
(/\/-\/issues\/?$/.test(pathname) && hasShowParam)
|
||||
) {
|
||||
return MentionType.Issue;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
default:
|
||||
|
||||
@@ -60,6 +60,21 @@ describe("GitLabUtils.parseUrl", () => {
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for an issues list URL without an ID", () => {
|
||||
const result = GitLabUtils.parseUrl(
|
||||
"https://gitlab.com/speak/purser/-/issues"
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
|
||||
it("should return undefined for an invalid custom URL", () => {
|
||||
const result = GitLabUtils.parseUrl(
|
||||
"https://gitlab.example.com/team/project/-/issues/10",
|
||||
"not-a-valid-url"
|
||||
);
|
||||
expect(result).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe("base64 show parameter URLs", () => {
|
||||
|
||||
@@ -104,21 +104,57 @@ export class GitLabUtils {
|
||||
* @returns An object containing resource identifiers or undefined if the URL is invalid.
|
||||
*/
|
||||
public static parseUrl(url: string, customUrl?: string) {
|
||||
const parsed = new URL(url);
|
||||
const urlHostname = new URL(this.getGitlabUrl(customUrl)).hostname;
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
const urlHostname = new URL(this.getGitlabUrl(customUrl)).hostname;
|
||||
|
||||
if (parsed.hostname !== urlHostname) {
|
||||
return;
|
||||
}
|
||||
if (parsed.hostname !== urlHostname) {
|
||||
return;
|
||||
}
|
||||
|
||||
const parts = parsed.pathname.split("/").filter(Boolean);
|
||||
const parts = parsed.pathname.split("/").filter(Boolean);
|
||||
|
||||
// Try base64-encoded `show` query parameter first
|
||||
// e.g. /owner/repo/-/issues?show=eyJ...
|
||||
const showParam = parsed.searchParams.get("show");
|
||||
if (showParam && parts.length >= 4) {
|
||||
// Try base64-encoded `show` query parameter first
|
||||
// e.g. /owner/repo/-/issues?show=eyJ...
|
||||
const showParam = parsed.searchParams.get("show");
|
||||
if (showParam && parts.length >= 4) {
|
||||
const resourceType = parts.pop();
|
||||
parts.pop(); // separator ("-")
|
||||
const repo = parts.pop();
|
||||
const owner = parts.join("/");
|
||||
|
||||
const type =
|
||||
resourceType === "issues"
|
||||
? UnfurlResourceType.Issue
|
||||
: resourceType === "merge_requests"
|
||||
? UnfurlResourceType.PR
|
||||
: undefined;
|
||||
|
||||
if (!type || !this.supportedResources.includes(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = JSON.parse(atob(decodeURIComponent(showParam)));
|
||||
const iid = Number(decoded.iid);
|
||||
if (!iid) {
|
||||
return;
|
||||
}
|
||||
return { owner, repo, type, id: iid, url };
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.length < 5) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Direct URL: /owner/repo/-/issues/123 or /owner/repo/-/merge_requests/123
|
||||
const resourceId = parts.pop();
|
||||
const resourceType = parts.pop();
|
||||
parts.pop(); // separator ("-")
|
||||
|
||||
const repo = parts.pop();
|
||||
const owner = parts.join("/");
|
||||
|
||||
@@ -133,48 +169,16 @@ export class GitLabUtils {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const decoded = JSON.parse(atob(decodeURIComponent(showParam)));
|
||||
const iid = Number(decoded.iid);
|
||||
if (!iid) {
|
||||
return;
|
||||
}
|
||||
return { owner, repo, type, id: iid, url };
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (parts.length < 5) {
|
||||
return {
|
||||
owner,
|
||||
repo,
|
||||
type,
|
||||
id: Number(resourceId),
|
||||
url,
|
||||
};
|
||||
} catch {
|
||||
return;
|
||||
}
|
||||
|
||||
// Direct URL: /owner/repo/-/issues/123 or /owner/repo/-/merge_requests/123
|
||||
const resourceId = parts.pop();
|
||||
const resourceType = parts.pop();
|
||||
parts.pop(); // separator ("-")
|
||||
|
||||
const repo = parts.pop();
|
||||
const owner = parts.join("/");
|
||||
|
||||
const type =
|
||||
resourceType === "issues"
|
||||
? UnfurlResourceType.Issue
|
||||
: resourceType === "merge_requests"
|
||||
? UnfurlResourceType.PR
|
||||
: undefined;
|
||||
|
||||
if (!type || !this.supportedResources.includes(type)) {
|
||||
return;
|
||||
}
|
||||
|
||||
return {
|
||||
owner,
|
||||
repo,
|
||||
type,
|
||||
id: Number(resourceId),
|
||||
url,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -311,14 +311,19 @@ export const MentionIssue = observer((props: IssuePrProps) => {
|
||||
}
|
||||
|
||||
const issue = unfurl as UnfurlResponse[UnfurlResourceType.Issue];
|
||||
const url = new URL(issue.url);
|
||||
|
||||
const service =
|
||||
url.hostname === "github.com"
|
||||
? IntegrationService.GitHub
|
||||
: url.hostname === "linear.app"
|
||||
? IntegrationService.Linear
|
||||
: IntegrationService.GitLab;
|
||||
let service = IntegrationService.GitLab;
|
||||
try {
|
||||
const parsedUrl = new URL(issue.url);
|
||||
service =
|
||||
parsedUrl.hostname === "github.com"
|
||||
? IntegrationService.GitHub
|
||||
: parsedUrl.hostname === "linear.app"
|
||||
? IntegrationService.Linear
|
||||
: IntegrationService.GitLab;
|
||||
} catch {
|
||||
// Invalid URL in unfurl data, default to GitLab
|
||||
}
|
||||
|
||||
return (
|
||||
<a
|
||||
|
||||
Reference in new issue
Block a user