feat(Chat): display clickable links, sanitize them and XSS protect

Closes #458
This commit is contained in:
Iuri Matias 2020-06-17 08:53:51 -04:00
parent 6d6f67476b
commit 716258156e
5 changed files with 1366 additions and 7 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ npm-shrinkwrap.json
yarn-debug.log*
yarn-error.log*
yarn.lock
TODO

View File

@ -18,7 +18,7 @@ proc renderInline(self: ChatMessageList, elem: TextItem): string =
of "code": result = fmt("<span style=\"background-color: #1a356b; color: #FFFFFF\">{elem.literal}</span> ")
of "emph": result = fmt("<span style=\"font-style: italic;\">{elem.literal}</span> ")
of "strong": result = fmt("<span style=\"font-weight: bold;\">{elem.literal}</span> ")
of "link": result = "TODO: write safe link here: " & elem.destination
of "link": result = elem.destination
of "mention": result = fmt("<span style=\"color: #000000;\">{self.mention(elem.literal)}</span> ")
# See render-block in status-react/src/status_im/ui/screens/chat/message/message.cljs

View File

@ -1,7 +1,10 @@
import QtQuick 2.13
import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
import QtQuick 2.3
import QtQuick.Controls 2.3
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.3
import Qt.labs.platform 1.1
import "../../../../shared"
import "../../../../shared/xss.js" as XSS
import "../../../../imports"
import "../components"
@ -36,6 +39,23 @@ Item {
}
}
function linkify(inputText) {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, "<a href='$1'>$1</a>");
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, "$1<a href='http://$2'>$2</a>");
replacedText = XSS.filterXSS(replacedText)
return replacedText;
}
ProfilePopup {
id: profilePopup
}
Item {
id: channelIdentifier
visible: authorCurrentMsg == ""
@ -236,7 +256,8 @@ Item {
StyledTextEdit {
id: chatText
text: message
text: linkify(message)
textFormat: TextEdit.RichText
anchors.left: parent.left
anchors.leftMargin: parent.chatHorizontalPadding
anchors.right: message.length > 52 ? parent.right : undefined
@ -250,7 +271,12 @@ Item {
selectByMouse: true
color: !isCurrentUser ? Theme.black : Theme.white
visible: contentType == Constants.messageType
textFormat: TextEdit.RichText
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.NoButton // we don't want to eat clicks on the Text
cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
}
}
Image {

View File

@ -3,6 +3,7 @@ import QtQuick.Controls 2.13
import QtQuick.Layouts 1.13
import "../../../../imports"
import "../../../../shared"
import "../../../../shared/xss.js" as XSS
Item {
id: helpContainer
@ -11,6 +12,19 @@ Item {
Layout.fillHeight: true
Layout.fillWidth: true
function linkify(inputText) {
//URLs starting with http://, https://, or ftp://
var replacePattern1 = /(\b(https?|ftp):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/gim;
replacedText = inputText.replace(replacePattern1, "<a href='$1'>$1</a>");
//URLs starting with "www." (without // before it, or it'd re-link the ones done above).
var replacePattern2 = /(^|[^\/])(www\.[\S]+(\b|$))/gim;
replacedText = replacedText.replace(replacePattern2, "$1<a href='http://$2'>$2</a>");
replacedText = XSS.filterXSS(replacedText)
return replacedText;
}
StyledText {
id: element8
text: qsTr("Help menus: FAQ, Glossary, etc.")
@ -24,7 +38,7 @@ Item {
StyledText {
anchors.centerIn: parent
text: "<a href='https://status.im/docs/FAQs.html'>Frequently asked questions</a>"
text: linkify(link)
onLinkActivated: Qt.openUrlExternally(link)
MouseArea {

1318
ui/shared/xss.js Normal file

File diff suppressed because it is too large Load Diff