Stricter mxc/mxid validation #105
No reviewers
Labels
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
cadence/out-of-your-element!105
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "l1s5/out-of-your-element:stricter-mxc-mxid-validation"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Here are some centralized and well tested regexes for mxc and mxid parsing :)
yay
@ -11,2 +11,4 @@/** @type {import("./txnid")} */const makeTxnId = sync.require("./txnid")/** @type {import("./utils")} */const mUtils = sync.require("./utils")I call it
mxUtilsmore often, pls rename@ -244,16 +239,165 @@ function getPublicUrlForMxc(mxc) {return `${reg.ooye.bridge_origin}/download/matrix/${serverAndMediaID}`}const _serverNameIpv4Address = String.raw`[0-9]{1,3}(?:\.[0-9]{1,3}){3}`Since no patterns or regexps are being exported now and the only access is through functions, you can remove the leading
_, which should make the regexp construction less dense on symbols.This suggestion is optional.
@ -247,0 +291,4 @@/*** @param {string} mxc* @param {{[key: string]: string}} matchGroups* @returns {{mxc: string, serverName: string, mediaId: string} | undefined} undefined if it isn't a valid mxc URII would really like to return
nullrather thanundefinedfor all of these@ -247,0 +296,4 @@function _mxcFromMatchGroups(mxc, matchGroups) {// "." and ".." are *technically* valid server names,// but would become dot-segments (path traversal) in URL paths.if (matchGroups.serverName === "." || matchGroups.serverName === "..") return undefinedI think we should check that server names match
[^.][.][^.], i.e. a name on each side of a period. Although a name likeexample.is technically valid, it's not practically valid. This could prevent a class of typos where people don't finish typing their URL, and it takes out.and..too.Move this check to
isServerNameand call it?I've rewritten the
.check asserverName.startsWith(".") || serverName.endsWith("."), but I ended up copy pasting it in 3 locations in order not to run the same input through the same regex twice.@ -247,0 +337,4 @@* @see https://spec.matrix.org/latest/appendices/#user-identifiers specification* @see https://spec.matrix.org/latest/appendices/#historical-user-ids historical user IDs*/const _mxidLocalpartPattern = String.raw`[\x21-\x39\x3B-\x3F\x41-\x7E]+`*to match zero characters or no?@ -420,0 +472,4 @@];for (const mxc of INVALID_MXCS) {test(`parse mxc: ${String(mxc)} is invalid`, t => {String() does nothing here, remove unless you need it to keep the LSP happy
@ -22,2 +22,4 @@/** @type {import("../../m2d/actions/sticker")} */const sticker = sync.require("../../m2d/actions/sticker")/** @type {import("../../matrix/utils")} */const mUtils = sync.require("../../matrix/utils")I call it
mxUtilsmore often, pls rename@ -29,3 +29,3 @@const [error] = await tryToCatch(() => router.test("post", "/api/log-in-with-matrix", {body: {mxid: "@cadence:cadence."mxid: "@cadence:cadence_moe"Please keep
9fdc604230toc70597a665