Skip to content

Commit

Permalink
fix(sanitiseUrl): fix limitations with library
Browse files Browse the repository at this point in the history
  • Loading branch information
kishore03109 committed Mar 5, 2024
1 parent 99949cc commit da1888f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/templates/contact-us/ContactsSection.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { sanitizeUrl } from "@braintree/sanitize-url"
import DOMPurify from "dompurify"
import PropTypes from "prop-types"
import { forwardRef } from "react"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { sanitiseTemplateUrl } from "templates/utils/sanitiseTemplateUrl"
import { getClassNames } from "templates/utils/stylingUtils"

const Contact = forwardRef(({ contact }, ref) => (
Expand All @@ -30,7 +30,7 @@ const Contact = forwardRef(({ contact }, ref) => (
])}
>
<a
href={sanitizeUrl(`tel:${d[key].replace(/\s/g, "")}`)}
href={sanitiseTemplateUrl(`tel:${d[key].replace(/\s/g, "")}`)}
onClick={(event) => event.preventDefault()}
>
<u>{d[key]}</u>
Expand All @@ -47,7 +47,7 @@ const Contact = forwardRef(({ contact }, ref) => (
])}
>
<a
href={sanitizeUrl(`mailto:${d[key]}`)}
href={sanitiseTemplateUrl(`mailto:${d[key]}`)}
onClick={(event) => event.preventDefault()}
>
<u>{d[key]}</u>
Expand Down
4 changes: 2 additions & 2 deletions src/templates/contact-us/FeedbackSection.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sanitizeUrl } from "@braintree/sanitize-url"
import PropTypes from "prop-types"
import { forwardRef } from "react"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { sanitiseTemplateUrl } from "templates/utils/sanitiseTemplateUrl"
import { getClassNames } from "templates/utils/stylingUtils"

const TemplateFeedbackSection = forwardRef(({ feedback }, ref) => (
Expand Down Expand Up @@ -38,7 +38,7 @@ const TemplateFeedbackSection = forwardRef(({ feedback }, ref) => (
If you have a query, feedback or wish to report a problem related to
this website, please fill in the{" "}
<a
href={sanitizeUrl(feedback)}
href={sanitiseTemplateUrl(feedback)}
rel="noopener noreferrer"
target="_blank"
onClick={(event) => event.preventDefault()}
Expand Down
6 changes: 3 additions & 3 deletions src/templates/contact-us/LocationsSection.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { sanitizeUrl } from "@braintree/sanitize-url"
import PropTypes from "prop-types"
import { forwardRef } from "react"

import editorStyles from "styles/isomer-cms/pages/Editor.module.scss"

import { sanitiseTemplateUrl } from "templates/utils/sanitiseTemplateUrl"
import { getClassNames } from "templates/utils/stylingUtils"

const LocationHours = ({ operatingHours }) => (
Expand Down Expand Up @@ -38,8 +38,8 @@ const LocationAddress = ({ location }) => (
<a
href={
location.maps_link
? sanitizeUrl(location.maps_link)
: sanitizeUrl(
? sanitiseTemplateUrl(location.maps_link)
: sanitiseTemplateUrl(
`https://maps.google.com/?q=${location.address
.join("+")
.replace(/\s/g, "+")}`
Expand Down
14 changes: 14 additions & 0 deletions src/templates/utils/sanitiseTemplateUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { sanitizeUrl } from "@braintree/sanitize-url"

export function sanitiseTemplateUrl(userUrl: string): string {
const allowedProtocols = ["mailto:", "https:", "tel:"]
try {
const url = new URL(userUrl)
if (allowedProtocols.includes(url.protocol)) {
return sanitizeUrl(url.href)
}
} catch (e) {
return "about:blank"
}
return "about:blank"
}

0 comments on commit da1888f

Please sign in to comment.