-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Client side validation of alias max length #4060
Changes from 3 commits
3da5641
c3b65a9
7636b4d
489aedb
ba80bf5
f385e74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Validate public space address length when user clicks away from |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2021 The Matrix.org Foundation C.I.C. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.matrix.android.sdk.api | ||
|
||
/** | ||
* This object define some global constants regarding the Matrix specification | ||
*/ | ||
object MatrixConstants { | ||
/** | ||
* Max length for an alias. Room aliases MUST NOT exceed 255 bytes (including the # sigil and the domain). | ||
* See [maxAliasLocalPartLength] | ||
* Ref. https://matrix.org/docs/spec/appendices#room-aliases | ||
*/ | ||
const val ALIAS_MAX_LENGTH = 255 | ||
|
||
fun maxAliasLocalPartLength(domain: String): Int { | ||
return (ALIAS_MAX_LENGTH - 1 /* # sigil */ - 1 /* ':' */ - domain.length) | ||
.coerceAtLeast(0) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -162,10 +162,14 @@ object MatrixPatterns { | |
return order != null && order.length < 50 && order matches ORDER_STRING_REGEX | ||
} | ||
|
||
fun candidateAliasFromRoomName(name: String): String { | ||
return Regex("\\s").replace(name.lowercase(), "_").let { | ||
"[^a-z0-9._%#@=+-]".toRegex().replace(it, "") | ||
} | ||
fun candidateAliasFromRoomName(roomName: String, domain: String): String { | ||
return roomName.lowercase() | ||
// Replace spaces by '_' | ||
.let { Regex("\\s").replace(it, "_") } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: these comments could be replaced by extensions with the same name as the comment (and potentially be reused) roomName
.lowercase()
.replaceSpacesWithUnderscore()
.removeInvalidRoomNameChars()
.limitLength(length = MatrixConstants.maxAliasLocalPartLength(domain))
fun String.replaceSpacesWithUnderscore() = Regex("\\s").replace(this, "_")
fun String.removeInvalidRoomNameChars() = "[^a-z0-9._%#@=+-]".toRegex().replace(this, "")
fun String.limitLength(length: Int) = this.substring(0, length) I'm more in the self documenting code camp than adding comments to describe what the code does, where possible! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 done in f385e74 |
||
// Remove all invalid chars | ||
.let { "[^a-z0-9._%#@=+-]".toRegex().replace(it, "") } | ||
// limit length | ||
.substring(0, MatrixConstants.maxAliasLocalPartLength(domain)) | ||
} | ||
|
||
/** | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍