Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix Android attachment upload base64 encode handling (#1586)

Leendert de Borst16f44a6f00e2
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
index a621c300a..3c2199a6f 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/vaultstore/VaultStore.kt
@@ -40,6 +40,13 @@ class VaultStore(
@Volatile
private var instance: VaultStore? = null
+ /**
+ * Prefix used to identify base64-encoded blob data from React Native.
+ * The React Native side prefixes base64 strings with this to indicate
+ * they should be converted to ByteArray for SQLite BLOB storage.
+ */
+ private const val BASE64_BLOB_PREFIX = "av-base64-to-blob:"
+
/**
* Get the instance of the vault store.
* @param keystoreProvider The keystore provider
@@ -258,11 +265,15 @@ class VaultStore(
fun executeQuery(queryString: String, params: Array<Any?>): List<Map<String, Any?>> {
val db = databaseComponent.dbConnection ?: error("Database not initialized")
- // Convert params to strings for SQLite
+ // Process params - convert base64-prefixed strings to ByteArray for blob binding
val convertedParams = params.map { param ->
- when (param) {
- null -> null
- is ByteArray -> String(param, Charsets.UTF_8)
+ when {
+ param == null -> null
+ param is String && param.startsWith(BASE64_BLOB_PREFIX) -> {
+ val base64 = param.removePrefix(BASE64_BLOB_PREFIX)
+ android.util.Base64.decode(base64, android.util.Base64.NO_WRAP)
+ }
+ param is ByteArray -> param
else -> param.toString()
}
}.toTypedArray()
@@ -275,20 +286,14 @@ class VaultStore(
while (it.moveToNext()) {
val row = mutableMapOf<String, Any?>()
for (columnName in columnNames) {
- when (it.getType(it.getColumnIndexOrThrow(columnName))) {
+ val colIndex = it.getColumnIndexOrThrow(columnName)
+ val colType = it.getType(colIndex)
+ when (colType) {
android.database.Cursor.FIELD_TYPE_NULL -> row[columnName] = null
- android.database.Cursor.FIELD_TYPE_INTEGER -> row[columnName] = it.getLong(
- it.getColumnIndexOrThrow(columnName),
- )
- android.database.Cursor.FIELD_TYPE_FLOAT -> row[columnName] = it.getDouble(
- it.getColumnIndexOrThrow(columnName),
- )
- android.database.Cursor.FIELD_TYPE_STRING -> row[columnName] = it.getString(
- it.getColumnIndexOrThrow(columnName),
- )
- android.database.Cursor.FIELD_TYPE_BLOB -> row[columnName] = it.getBlob(
- it.getColumnIndexOrThrow(columnName),
- )
+ android.database.Cursor.FIELD_TYPE_INTEGER -> row[columnName] = it.getLong(colIndex)
+ android.database.Cursor.FIELD_TYPE_FLOAT -> row[columnName] = it.getDouble(colIndex)
+ android.database.Cursor.FIELD_TYPE_STRING -> row[columnName] = it.getString(colIndex)
+ android.database.Cursor.FIELD_TYPE_BLOB -> row[columnName] = it.getBlob(colIndex)
}
}
results.add(row)
@@ -304,23 +309,32 @@ class VaultStore(
fun executeUpdate(queryString: String, params: Array<Any?>): Int {
val db = databaseComponent.dbConnection ?: error("Database not initialized")
- val convertedParams = params.map { param ->
- when (param) {
- null -> null
- is ByteArray -> String(param, Charsets.UTF_8)
+ // Process params - convert base64-prefixed strings to ByteArray for blob binding
+ val processedParams = params.map { param ->
+ when {
+ param == null -> null
+ param is String && param.startsWith(BASE64_BLOB_PREFIX) -> {
+ val base64 = param.removePrefix(BASE64_BLOB_PREFIX)
+ android.util.Base64.decode(base64, android.util.Base64.NO_WRAP)
+ }
+ param is ByteArray -> param
else -> param.toString()
}
- }.toTypedArray()
+ }
val stmt = db.compileStatement(queryString)
- convertedParams.forEachIndexed { index, value ->
- if (value == null) {
- stmt.bindNull(index + 1)
- } else {
- stmt.bindString(index + 1, value)
+ try {
+ processedParams.forEachIndexed { index, value ->
+ when (value) {
+ null -> stmt.bindNull(index + 1)
+ is ByteArray -> stmt.bindBlob(index + 1, value)
+ else -> stmt.bindString(index + 1, value.toString())
+ }
}
+ stmt.execute()
+ } finally {
+ stmt.close()
}
- stmt.execute()
// Get the number of affected rows
val affectedCursor = db.rawQuery("SELECT changes()", null)
diff --git a/apps/mobile-app/components/items/details/AttachmentSection.tsx b/apps/mobile-app/components/items/details/AttachmentSection.tsx
index bc6f7c366..ef09f5190 100644
--- a/apps/mobile-app/components/items/details/AttachmentSection.tsx
+++ b/apps/mobile-app/components/items/details/AttachmentSection.tsx
@@ -44,10 +44,11 @@ export const AttachmentSection: React.FC<AttachmentSectionProps> = ({ item }): R
*/
const handleAttachment = async (attachment: Attachment): Promise<void> => {
try {
- const filename = attachment.Filename;
- const fileExtension = filename.split('.').pop()?.toLowerCase() ?? '';
+ // Sanitize filename
+ const sanitizedFilename = attachment.Filename.replace(/[/\\]/g, '_');
+ const fileExtension = sanitizedFilename.split('.').pop()?.toLowerCase() ?? '';
const downloadsDir = FileSystem.documentDirectory + 'Downloads/';
- const filePath = downloadsDir + filename;
+ const filePath = downloadsDir + sanitizedFilename;
// Ensure Downloads directory exists
const dirInfo = await FileSystem.getInfoAsync(downloadsDir);
@@ -55,19 +56,21 @@ export const AttachmentSection: React.FC<AttachmentSectionProps> = ({ item }): R
await FileSystem.makeDirectoryAsync(downloadsDir, { intermediates: true });
}
- // Check if file already exists, if not, create it
+ // Always recreate file to ensure fresh data
const fileInfo = await FileSystem.getInfoAsync(filePath);
- if (!fileInfo.exists) {
- if (typeof attachment.Blob === 'string') {
- await FileSystem.writeAsStringAsync(filePath, attachment.Blob, {
- encoding: FileSystem.EncodingType.Base64,
- });
- } else {
- const base64Data = Buffer.from(attachment.Blob as unknown as string, 'base64');
- await FileSystem.writeAsStringAsync(filePath, base64Data.toString(), {
- encoding: FileSystem.EncodingType.Base64,
- });
- }
+ if (fileInfo.exists) {
+ await FileSystem.deleteAsync(filePath);
+ }
+
+ if (typeof attachment.Blob === 'string') {
+ await FileSystem.writeAsStringAsync(filePath, attachment.Blob, {
+ encoding: FileSystem.EncodingType.Base64,
+ });
+ } else {
+ const base64Data = Buffer.from(attachment.Blob as unknown as string, 'base64');
+ await FileSystem.writeAsStringAsync(filePath, base64Data.toString(), {
+ encoding: FileSystem.EncodingType.Base64,
+ });
}
// Define supported file types
@@ -78,14 +81,14 @@ export const AttachmentSection: React.FC<AttachmentSectionProps> = ({ item }): R
if (previewableExtensions.includes(fileExtension)) {
// Show preview modal for supported file types
setSelectedFile({
- name: filename,
+ name: sanitizedFilename,
path: filePath,
extension: fileExtension,
});
setPreviewModalVisible(true);
} else {
// For other file types, offer download directly
- await downloadFileToSystem(filePath, filename);
+ await downloadFileToSystem(filePath, sanitizedFilename);
}
} catch (error) {
console.error('Error handling attachment:', error);