Skip to content

Commit

Permalink
Check file names for upload content from other apps
Browse files Browse the repository at this point in the history
Signed-off-by: alperozturk <[email protected]>
  • Loading branch information
alperozturk96 committed Jul 12, 2024
1 parent d357e3c commit acec2dd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ open class FolderPickerActivity :

targetFilePaths?.let { filePaths ->
if (!isFolderAndFilePathsValid(filePaths)) {
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move)
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters)
Handler(Looper.getMainLooper()).postDelayed({
setResult(RESULT_CANCELED, resultData)
finish()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ public void onClick(View v) {

boolean isPathValid = FileNameValidator.INSTANCE.checkFolderPath(mUploadPath, getCapabilities(), this);
if (!isPathValid) {
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_copy_or_move);
DisplayUtils.showSnackMessage(this, R.string.file_name_validator_error_contains_reserved_names_or_invalid_characters);
return;
}

Expand Down
21 changes: 20 additions & 1 deletion app/src/main/java/com/owncloud/android/ui/helpers/UriUploader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import android.net.Uri
import android.os.Parcelable
import com.nextcloud.client.account.User
import com.nextcloud.client.jobs.upload.FileUploadHelper
import com.nextcloud.utils.fileNameValidator.FileNameValidator
import com.owncloud.android.R
import com.owncloud.android.files.services.NameCollisionPolicy
import com.owncloud.android.lib.common.utils.Log_OC
Expand All @@ -23,6 +24,7 @@ import com.owncloud.android.ui.activity.FileActivity
import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask
import com.owncloud.android.ui.asynctasks.CopyAndUploadContentUrisTask.OnCopyTmpFilesTaskListener
import com.owncloud.android.ui.fragment.TaskRetainerFragment
import com.owncloud.android.utils.DisplayUtils
import com.owncloud.android.utils.UriUtils.getDisplayNameForUri

/**
Expand Down Expand Up @@ -70,9 +72,26 @@ class UriUploader(
Log_OC.e(TAG, "Sensitive URI detected, aborting upload.")
code = UriUploaderResultCode.ERROR_SENSITIVE_PATH
} else {
val uris = mUrisToUpload.filterNotNull()
var isInvalidPathMessageDisplayed = false
val uris = mUrisToUpload
.filterNotNull()
.map { it as Uri }
.map { Pair(it, getRemotePathForUri(it)) }
.filter { (_, filename) ->
val isValid = (FileNameValidator.checkFileName(
filename.removePrefix("/"),
mActivity.capabilities,
mActivity, null
) == null)
if (!isValid && !isInvalidPathMessageDisplayed) {
isInvalidPathMessageDisplayed = true
DisplayUtils.showSnackMessage(
mActivity,
R.string.file_name_validator_upload_content_error
)
}
isValid
}

val fileUris = uris
.filter { it.first.scheme == ContentResolver.SCHEME_FILE }
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,8 @@
<string name="secure_share_not_set_up">Secure sharing is not set up for this user</string>
<string name="share_not_allowed_when_file_drop">Resharing is not allowed during secure file drop</string>

<string name="file_name_validator_error_copy_or_move">Folder path contains reserved names or invalid character</string>
<string name="file_name_validator_upload_content_error">Some contents cannot able to uploaded due to contains reserved names or invalid character.</string>
<string name="file_name_validator_error_contains_reserved_names_or_invalid_characters">Folder path contains reserved names or invalid character</string>
<string name="file_name_validator_error_invalid_character">File name contains invalid characters: %s</string>
<string name="file_name_validator_error_reserved_names">%s is a reserved name</string>
<string name="file_name_validator_error_ends_with_space_period">File name ends with a space or a period</string>
Expand Down

0 comments on commit acec2dd

Please sign in to comment.