-
-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔒 Escaped HTML in bookmark text fields (#1394)
ref https://linear.app/ghost/issue/ENG-1751 - to prevent potential cross-site scripting (XSS) vulnerabilities or HTML injection, we are now escaping bookmark text fields for email - bookmark text fields were already escaped for web
- Loading branch information
Showing
4 changed files
with
105 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import {escapeHtml} from './escape-html'; | ||
|
||
export function truncateText(text, maxLength) { | ||
if (text && text.length > maxLength) { | ||
return text.substring(0, maxLength - 1).trim() + '…'; | ||
} else { | ||
return text ?? ''; | ||
} | ||
} | ||
|
||
export function truncateHtml(text, maxLength, maxLengthMobile) { | ||
// If no mobile length specified or mobile length is larger than desktop, | ||
// just do a simple truncate | ||
if (!maxLengthMobile || maxLength <= maxLengthMobile) { | ||
return escapeHtml(truncateText(text, maxLength)); | ||
} | ||
|
||
// Handle text shorter than mobile length | ||
if (text.length <= maxLengthMobile) { | ||
return escapeHtml(text); | ||
} | ||
|
||
if (text && text.length > maxLengthMobile) { | ||
let ellipsis = ''; | ||
|
||
if (text.length > maxLengthMobile && text.length <= maxLength) { | ||
ellipsis = '<span class="hide-desktop">…</span>'; | ||
} else if (text.length > maxLength) { | ||
ellipsis = '…'; | ||
} | ||
|
||
return escapeHtml(text.substring(0, maxLengthMobile - 1)) + '<span class="desktop-only">' + escapeHtml(text.substring(maxLengthMobile - 1, maxLength - 1)) + '</span>' + ellipsis; | ||
} else { | ||
return escapeHtml(text ?? ''); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters