-
Notifications
You must be signed in to change notification settings - Fork 19
fix: Fix path traversal on attachment #3013
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
Open
FabianDevel
wants to merge
6
commits into
main
Choose a base branch
from
fix-path-traversal-on-attachment
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b5169bf
fix(Attachment): Fix path traversal with attachment names
FabianDevel 3895e27
fix(Attachment): Add `toSafeFileName` string extension to avoid path …
FabianDevel 909f26e
refactor(FileNameUtils): Simplify code and add comments
FabianDevel 7118c3e
refactor(FileNameUtils): Manage exceptions throws
FabianDevel 6f59631
fix(FileNameUtils): Fix Tests
FabianDevel 60cb8c8
chore(FileNameUtils): Apply suggestions
FabianDevel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
104 changes: 104 additions & 0 deletions
104
app/src/main/java/com/infomaniak/mail/utils/FileNameUtils.kt
This file contains hidden or 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,104 @@ | ||
| /* | ||
| * Infomaniak Mail - Android | ||
| * Copyright (C) 2026 Infomaniak Network SA | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| */ | ||
| package com.infomaniak.mail.utils | ||
|
|
||
| import io.sentry.Sentry | ||
| import java.io.File | ||
| import java.nio.charset.StandardCharsets | ||
| import java.text.Normalizer | ||
|
|
||
| /** Android’s filesystems limit each path component to 255 UTF-8 bytes. | ||
| * 240 leaves 15 bytes for prefixes/suffixes. | ||
| * The upload prefix (an Int hash code plus an underscore) needs at most 12 bytes, yielding at most 252 bytes. | ||
| * The remaining 3 bytes are conservative headroom. */ | ||
| private const val MAX_FILE_NAME_SIZE_BYTES = 240 | ||
| private const val MAX_PRESERVED_EXTENSION_SIZE_BYTES = 32 | ||
| private const val DEFAULT_FILE_NAME = "attachment" | ||
| private val invalidFileNameCharacters = Regex("[\\\\/:*?\"<>|%\\p{Cc}]") | ||
|
|
||
| fun String.toSafeFileName(): String { | ||
| val normalizedName = Normalizer.normalize(this, Normalizer.Form.NFC) | ||
| .replace(invalidFileNameCharacters, "_") | ||
| .trim() | ||
| .takeUnless { it.isEmpty() || it == "." || it == ".." } | ||
| ?: DEFAULT_FILE_NAME | ||
|
|
||
| return if (normalizedName.utf8Size <= MAX_FILE_NAME_SIZE_BYTES) normalizedName else computeTruncatedFileName(normalizedName) | ||
| } | ||
|
|
||
| fun File.resolveContainedPath(untrustedPath: String): File? = runCatching { | ||
| if (File(untrustedPath).isAbsolute) throw SecurityException("Absolute paths are not allowed") | ||
|
|
||
| val canonicalRoot = canonicalFile | ||
| val resolvedFile = File(canonicalRoot, untrustedPath).canonicalFile | ||
| if (resolvedFile != canonicalRoot && !resolvedFile.toPath().startsWith(canonicalRoot.toPath())) { | ||
| throw SecurityException("Resolved path escapes its allowed root") | ||
| } | ||
|
|
||
| return@runCatching resolvedFile | ||
| }.getOrElse { | ||
| Sentry.captureException(it) | ||
| null | ||
| } | ||
|
|
||
| fun File.resolveContainedFileName(untrustedName: String): File? { | ||
| val resolvedFile = File(this, untrustedName.toSafeFileName()) | ||
| return if (resolvedFile.parentFile != this) { | ||
| Sentry.captureException(SecurityException("Resolved file escapes its allowed directory")) | ||
| null | ||
| } else { | ||
| resolvedFile | ||
| } | ||
| } | ||
|
|
||
| private fun computeTruncatedFileName(normalizedName: String): String { | ||
| val extensionStart = normalizedName.lastIndexOf('.').takeIf { it in 1 until normalizedName.lastIndex } | ||
| val extension = extensionStart?.let(normalizedName::substring) | ||
| ?.takeIf { it.utf8Size <= MAX_PRESERVED_EXTENSION_SIZE_BYTES } | ||
| .orEmpty() | ||
| val fileNameWithoutExtension = normalizedName.dropLast(extension.length) | ||
| val truncatedFileName = fileNameWithoutExtension.takeUtf8Bytes(MAX_FILE_NAME_SIZE_BYTES - extension.utf8Size) | ||
|
|
||
| return truncatedFileName.ifEmpty { DEFAULT_FILE_NAME } + extension | ||
| } | ||
|
|
||
| private inline val String.utf8Size: Int get() = toByteArray(StandardCharsets.UTF_8).size | ||
|
|
||
| /** | ||
| * Compute with utf8 size because Kotlin manipulate utf16 entities | ||
| * This avoids character needing 2 bytes like 'é' to count only for 1 in a take(MAX_FILE_NAME_SIZE_BYTES) instead of 2. | ||
| */ | ||
| private fun String.takeUtf8Bytes(maxBytes: Int): String { | ||
| if (maxBytes <= 0) return "" | ||
| if (utf8Size <= maxBytes) return this | ||
|
|
||
| val result = StringBuilder() | ||
| var byteCount = 0 | ||
| var index = 0 | ||
| while (index < length) { | ||
| val codePoint = codePointAt(index) | ||
| val character = String(Character.toChars(codePoint)) | ||
| val characterSize = character.utf8Size | ||
| if (byteCount + characterSize > maxBytes) break | ||
|
|
||
| result.append(character) | ||
| byteCount += characterSize | ||
| index += Character.charCount(codePoint) | ||
| } | ||
| return result.toString() | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.