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

COM-486: Prevent XSS attacks in isLinkTarget validator #1816

Merged
merged 5 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion packages/admin/cms-admin/src/blocks/ExternalLinkBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as React from "react";
import { FormattedMessage } from "react-intl";

import { ExternalLinkBlockData, ExternalLinkBlockInput } from "../blocks.generated";
import { isLinkTarget } from "../validation/isHref";
import { isLinkTarget } from "../validation/isLinkTarget";
import { validateUrl } from "../validation/validateUrl";

type State = ExternalLinkBlockData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@ import { isEmail, isURL } from "class-validator";
const PHONE_NUMBER_REGEX = /^\+?[0-9\s]+$/;

export function isLinkTarget(value: string): boolean {
if (value.startsWith("mailto:")) {
if (value.toLowerCase().includes("javascript:")) {
return false;
} else if (value.toLowerCase().includes("data:")) {
return false;
} else if (value.startsWith("mailto:")) {
return isEmail(value.slice(7));
} else if (value.startsWith("tel:")) {
return PHONE_NUMBER_REGEX.test(value.slice(4));
} else {
return isURL(value, { require_protocol: true, require_valid_protocol: false });
}
}

/**
* @deprecated The validator function `isHref` will be removed in a future version. Please use `isLinkTarget` instead.
*/
export const isHref = isLinkTarget;
2 changes: 1 addition & 1 deletion packages/api/blocks-api/src/blocks/ExternalLinkBlock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { IsBoolean, IsOptional } from "class-validator";

import { BlockData, BlockInput, createBlock, inputToData } from "./block";
import { BlockField } from "./decorators/field";
import { IsLinkTarget } from "./externalLinkBlock/is-href.validator";
import { IsLinkTarget } from "./externalLinkBlock/is-link-target.validator";

class ExternalLinkBlockData extends BlockData {
@BlockField({ nullable: true })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ export const IsLinkTarget = () => {
};
};

/**
* @deprecated The decorator `IsHref` will be removed in a future version. Please use `IsLinkTarget` instead.
*/
export const IsHref = IsLinkTarget;

@ValidatorConstraint({ name: "IsLinkTarget" })
@Injectable()
export class IsLinkTargetConstraint implements ValidatorConstraintInterface {
Expand All @@ -27,7 +22,11 @@ export class IsLinkTargetConstraint implements ValidatorConstraintInterface {
return false;
}

if (value.startsWith("mailto:")) {
if (value.toLowerCase().includes("javascript:")) {
return false;
} else if (value.toLowerCase().includes("data:")) {
return false;
} else if (value.startsWith("mailto:")) {
return isEmail(value.slice(7));
} else if (value.startsWith("tel:")) {
return PHONE_NUMBER_REGEX.test(value.slice(4));
Expand All @@ -40,12 +39,3 @@ export class IsLinkTargetConstraint implements ValidatorConstraintInterface {
return "Invalid link target";
}
}

/**
* @deprecated The class `IsHrefConstraint` will be removed in a future version. Please use `IsLinkTargetConstraint` instead.
*/
export class IsHrefConstraint extends IsLinkTargetConstraint {
constructor() {
super();
}
}
Loading