Skip to content

Commit

Permalink
Randomise multipart boundary, and include mxids in invite email notifs
Browse files Browse the repository at this point in the history
  • Loading branch information
erikjohnston authored and Richard van der Hoff committed Apr 9, 2021
1 parent abf7114 commit 4469d1d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 53 deletions.
31 changes: 15 additions & 16 deletions res/matrix-org/invite_template.eml
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@ To: %(to)s
Message-ID: %(messageid)s
Subject: %(subject_header_value)s
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ"
Content-Type: multipart/alternative;
boundary="%(multipart_boundary)s"

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
--%(multipart_boundary)s
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Hi,
%(sender_display_name)s has invited you into a room %(bracketed_room_name)s on
Matrix. To join the conversation, either pick a Matrix client from
https://matrix.org/docs/projects/try-matrix-now.html or use the single-click
link below to join via Element (requires Chrome, Firefox, Safari, iOS or Android)
%(sender_display_name)s %(bracketed_verified_sender)shas invited you into a room
%(bracketed_room_name)son Matrix. To join the conversation, either pick a
Matrix client from https://matrix.org/docs/projects/try-matrix-now.html or use
the single-click link below to join via Element (requires Chrome, Firefox,
Safari, iOS or Android)
%(web_client_location)/#/room/%(room_id_forurl)s?email=%(to_forurl)s&signurl=https%%3A%%2F%%2Fmatrix.org%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D%(token)s%%26private_key%%3D%(ephemeral_private_key)s&room_name=%(room_name_forurl)s&room_avatar_url=%(room_avatar_url_forurl)s&inviter_name=%(sender_display_name_forurl)s&guest_access_token=%(guest_access_token_forurl)s&guest_user_id=%(guest_user_id_forurl)s
Expand All @@ -38,12 +39,7 @@ Thanks,
Matrix
--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
Content-Type: multipart/related;
boundary="M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR";
type="text/html"
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR
--%(multipart_boundary)s
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

Expand All @@ -68,6 +64,10 @@ pre, code {
padding: 20px;
}
#low-contrast {
color: #666666
}
#inner {
width: 640px;
}
Expand Down Expand Up @@ -102,7 +102,7 @@ pre, code {

<p>Hi,</p>

<p>%(sender_display_name_forhtml)s has invited you into a room %(bracketed_room_name_forhtml)s on
<p>%(sender_display_name_forhtml)s <span class="low-contrast">%(bracketed_verified_sender_for_html)s</span> has invited you into a room %(bracketed_room_name_forhtml)s on
Matrix. To join the conversation, either <a href="https://matrix.org/docs/projects/try-matrix-now.html">pick a Matrix client</a> or use the single-click
link below to join via Element (requires
<a href="https://www.google.com/chrome">Chrome</a>,
Expand Down Expand Up @@ -139,6 +139,5 @@ create new communication solutions or extend the capabilities and reach of exist
</table>
</body>
</html>
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR--

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ--
--%(multipart_boundary)s--
10 changes: 5 additions & 5 deletions res/matrix-org/verification_template.eml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ To: %(to)s
Message-ID: %(messageid)s
Subject: Confirm your email address for Matrix
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ"
Content-Type: multipart/alternative;
boundary="%(multipart_boundary)s"

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
--%(multipart_boundary)s
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Expand Down Expand Up @@ -35,7 +35,7 @@ Matrix defines the standard, and provides open source reference implementations
Matrix-compatible Servers, Clients, Client SDKs and Application Services to help you
create new communication solutions or extend the capabilities and reach of existing ones.
--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
--%(multipart_boundary)s
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

Expand Down Expand Up @@ -85,4 +85,4 @@ create new communication solutions or extend the capabilities and reach of exist
</body>
</html>

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ--
--%(multipart_boundary)s--
29 changes: 14 additions & 15 deletions res/vector-im/invite_template.eml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ To: %(to)s
Message-ID: %(messageid)s
Subject: %(subject_header_value)s
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ"
Content-Type: multipart/alternative;
boundary="%(multipart_boundary)s"

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
--%(multipart_boundary)s
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Hi,
%(sender_display_name)s has invited you into a room %(bracketed_room_name)s on
Element. To join the conversation please follow the link below.
%(sender_display_name)s %(bracketed_verified_sender)shas invited you into a room
%(bracketed_room_name)son Element. To join the conversation please follow the
link below.
%(web_client_location)/#/room/%(room_id_forurl)s?email=%(to_forurl)s&signurl=https%%3A%%2F%%2Fvector.im%%2F_matrix%%2Fidentity%%2Fapi%%2Fv1%%2Fsign-ed25519%%3Ftoken%%3D%(token)s%%26private_key%%3D%(ephemeral_private_key)s&room_name=%(room_name_forurl)s&room_avatar_url=%(room_avatar_url_forurl)s&inviter_name=%(sender_display_name_forurl)s&guest_access_token=%(guest_access_token_forurl)s&guest_user_id=%(guest_user_id_forurl)s
Expand Down Expand Up @@ -51,12 +52,7 @@ decentralized communication delivering a community of users, bridged networks,
integrated bots and applications plus full end-to-end encryption. To learn more about
Matrix visit https://matrix.org.
--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
Content-Type: multipart/related;
boundary="M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR";
type="text/html"
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR
--%(multipart_boundary)s
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

Expand All @@ -81,6 +77,10 @@ pre, code {
padding: 20px;
}
#low-contrast {
color: #666666
}
#inner {
width: 640px;
}
Expand Down Expand Up @@ -123,8 +123,8 @@ pre, code {

<p>Hi,</p>

<p>%(sender_display_name_forhtml)s has invited you into a room %(bracketed_room_name_forhtml)s on
Element.</p>
<p>%(sender_display_name_forhtml)s <span class="low-contrast">%(bracketed_verified_sender_for_html)s</span> has invited you into a
room %(bracketed_room_name_forhtml)s on Element.</p>

<p>
<a
Expand Down Expand Up @@ -173,6 +173,5 @@ Matrix visit https://matrix.org.</p>
</table>
</body>
</html>
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR--

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ--
--%(multipart_boundary)s--
16 changes: 5 additions & 11 deletions res/vector-im/verification_template.eml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ To: %(to)s
Message-ID: %(messageid)s
Subject: Confirm your email address for Element
MIME-Version: 1.0
Content-Type: multipart/alternative;
boundary="7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ"
Content-Type: multipart/alternative;
boundary="%(multipart_boundary)s"

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
--%(multipart_boundary)s
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Expand Down Expand Up @@ -51,12 +51,7 @@ decentralized communication delivering a community of users, bridged networks,
integrated bots and applications plus full end-to-end encryption. To learn more about
Matrix visit https://matrix.org.
--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ
Content-Type: multipart/related;
boundary="M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR";
type="text/html"
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR
--%(multipart_boundary)s
Content-Type: text/html; charset=UTF-8
Content-Disposition: inline

Expand Down Expand Up @@ -171,6 +166,5 @@ pre, code {
</table>
</body>
</html>
--M3yzHl5YZehm9v4bAM8sKEdcOoVnRnKR--

--7REaIwWQCioQ6NaBlAQlg8ztbUQj6PKJ--
--%(multipart_boundary)s--
19 changes: 13 additions & 6 deletions sydent/http/servlets/store_invite_servlet.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@
from sydent.db.invite_tokens import JoinTokenStore
from sydent.db.threepid_associations import GlobalAssociationStore

from sydent.http.servlets import get_args, send_cors, jsonwrap
from sydent.http.servlets import get_args, send_cors, jsonwrap, MatrixRestError
from sydent.http.auth import authV2
from sydent.util.emailutils import sendEmail


class StoreInviteServlet(Resource):
def __init__(self, syd, require_auth=False):
self.sydent = syd
Expand All @@ -42,15 +41,19 @@ def __init__(self, syd, require_auth=False):
def render_POST(self, request):
send_cors(request)

if self.require_auth:
authV2(self.sydent, request)

args = get_args(request, ("medium", "address", "room_id", "sender",))
medium = args["medium"]
address = args["address"]
roomId = args["room_id"]
sender = args["sender"]

verified_sender = None
if self.require_auth:
account = authV2(self.sydent, request)
verified_sender = sender
if account.userId != sender:
raise MatrixRestError(403, "M_UNAUTHORIZED", "'sender' doesn't match")

globalAssocStore = GlobalAssociationStore(self.sydent)
mxid = globalAssocStore.getMxid(medium, address)
if mxid:
Expand Down Expand Up @@ -104,9 +107,13 @@ def render_POST(self, request):
for k in extra_substitutions:
substitutions.setdefault(k, '')

substitutions["bracketed_verified_sender"] = ""
if verified_sender:
substitutions["bracketed_verified_sender"] = "(%s) " % (verified_sender,)

substitutions["ephemeral_private_key"] = ephemeralPrivateKeyBase64
if substitutions["room_name"] != '':
substitutions["bracketed_room_name"] = "(%s)" % substitutions["room_name"]
substitutions["bracketed_room_name"] = "(%s) " % substitutions["room_name"]

substitutions["web_client_location"] = self.sydent.default_web_client_location
if 'org.matrix.web_client_location' in substitutions:
Expand Down
5 changes: 5 additions & 0 deletions sydent/util/emailutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import email.utils

from sydent.util import time_msec
from sydent.util.tokenutils import generateAlphanumericTokenOfLength

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -74,6 +75,10 @@ def sendEmail(sydent, templateFile, mailTo, substitutions):
allSubstitutions[k+"_forhtml"] = escape(v)
allSubstitutions[k+"_forurl"] = urllib.parse.quote(v)

# We add randomize the multipart boundary to stop user input from
# conflicting with it.
allSubstitutions["multipart_boundary"] = generateAlphanumericTokenOfLength(32)

mailString = open(templateFile).read() % allSubstitutions
parsedFrom = email.utils.parseaddr(mailFrom)[1]
parsedTo = email.utils.parseaddr(mailTo)[1]
Expand Down

0 comments on commit 4469d1d

Please sign in to comment.