Skip to content

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

Refactor to pass app instance id via separate optional header (#1967)

Leendert de Borstd2b95c53253d
diff --git a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
index f19fa2e4d..89da94e5e 100644
--- a/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
+++ b/apps/mobile-app/android/app/src/main/java/net/aliasvault/app/webapi/WebApiService.kt
@@ -141,6 +141,7 @@ class WebApiService(private val context: Context) {
// Add client version header
requestHeaders["X-AliasVault-Client"] = getClientVersionHeader()
+ requestHeaders["X-AliasVault-AppInstanceId"] = appInstanceId
// Execute the request
val response = executeRawRequest(
@@ -160,6 +161,7 @@ class WebApiService(private val context: Context) {
val retryHeaders = headers.toMutableMap()
retryHeaders["Authorization"] = "Bearer $newToken"
retryHeaders["X-AliasVault-Client"] = getClientVersionHeader()
+ retryHeaders["X-AliasVault-AppInstanceId"] = appInstanceId
val retryResponse = executeRawRequest(
method = method,
@@ -290,6 +292,7 @@ class WebApiService(private val context: Context) {
"X-Ignore-Failure" to "true",
)
headers["X-AliasVault-Client"] = getClientVersionHeader()
+ headers["X-AliasVault-AppInstanceId"] = appInstanceId
val response = executeRawRequest(
method = "POST",
@@ -326,18 +329,17 @@ class WebApiService(private val context: Context) {
/**
* Get the client version header value.
- * Format: "android-{version}-{appInstanceId}"
- * The app instance ID uniquely identifies this app installation/Android user profile.
+ * Format: "android-{version}".
*/
private fun getClientVersionHeader(): String {
return try {
val packageInfo = context.packageManager.getPackageInfo(context.packageName, 0)
val version = packageInfo.versionName ?: "0.0.0"
val baseVersion = version.split("-").firstOrNull() ?: "0.0.0"
- "android-$baseVersion-$appInstanceId"
+ "android-$baseVersion"
} catch (e: PackageManager.NameNotFoundException) {
Log.e(TAG, "Error getting package version", e)
- "android-0.0.0-$appInstanceId"
+ "android-0.0.0"
}
}
diff --git a/apps/server/AliasVault.Api/Controllers/AuthController.cs b/apps/server/AliasVault.Api/Controllers/AuthController.cs
index b44949e1a..5f51b1a1e 100644
--- a/apps/server/AliasVault.Api/Controllers/AuthController.cs
+++ b/apps/server/AliasVault.Api/Controllers/AuthController.cs
@@ -12,6 +12,7 @@ using System.Security.Claims;
using System.Security.Cryptography;
using System.Text;
using AliasServerDb;
+using AliasVault.Api.Headers;
using AliasVault.Api.Helpers;
using AliasVault.Auth;
using AliasVault.Cryptography.Client;
diff --git a/apps/server/AliasVault.Api/Headers/AppInstanceIdHeaderInfo.cs b/apps/server/AliasVault.Api/Headers/AppInstanceIdHeaderInfo.cs
new file mode 100644
index 000000000..96c2a1465
--- /dev/null
+++ b/apps/server/AliasVault.Api/Headers/AppInstanceIdHeaderInfo.cs
@@ -0,0 +1,35 @@
+//-----------------------------------------------------------------------
+// <copyright file="AppInstanceIdHeaderInfo.cs" company="aliasvault">
+// Copyright (c) aliasvault. All rights reserved.
+// Licensed under the AGPLv3 license. See LICENSE.md file in the project root for full license information.
+// </copyright>
+//-----------------------------------------------------------------------
+
+namespace AliasVault.Api.Headers;
+
+/// <summary>
+/// Parsed value of the optional X-AliasVault-AppInstanceId request header.
+///
+/// This header is sent by clients that need to scope sessions per app instance, in addition to
+/// the user/device combo. Currently used by the Android app to support multiple Android User
+/// Profiles on the same physical device — each profile generates a unique UUID (without dashes)
+/// on first launch that persists for the lifetime of that installation.
+/// </summary>
+/// <param name="AppInstanceId">Per-install app instance identifier, or null when the header is absent or empty.</param>
+public sealed record AppInstanceIdHeaderInfo(string? AppInstanceId)
+{
+ /// <summary>
+ /// Name of the optional header used by clients to scope sessions per app instance.
+ /// </summary>
+ public const string HeaderName = "X-AliasVault-AppInstanceId";
+
+ /// <summary>
+ /// Parse a raw X-AliasVault-AppInstanceId header value.
+ /// </summary>
+ /// <param name="headerValue">Raw header value, may be null or empty.</param>
+ /// <returns>Parsed AppInstanceIdHeaderInfo. AppInstanceId is null when the header is missing or empty.</returns>
+ public static AppInstanceIdHeaderInfo Parse(string? headerValue)
+ {
+ return new AppInstanceIdHeaderInfo(string.IsNullOrEmpty(headerValue) ? null : headerValue);
+ }
+}
diff --git a/apps/server/AliasVault.Api/Helpers/ClientHeaderInfo.cs b/apps/server/AliasVault.Api/Headers/ClientHeaderInfo.cs
similarity index 62%
rename from apps/server/AliasVault.Api/Helpers/ClientHeaderInfo.cs
rename to apps/server/AliasVault.Api/Headers/ClientHeaderInfo.cs
index 85ab9475f..da0a4bce2 100644
--- a/apps/server/AliasVault.Api/Helpers/ClientHeaderInfo.cs
+++ b/apps/server/AliasVault.Api/Headers/ClientHeaderInfo.cs
@@ -5,26 +5,17 @@
// </copyright>
//-----------------------------------------------------------------------
-namespace AliasVault.Api.Helpers;
+namespace AliasVault.Api.Headers;
/// <summary>
/// Parsed components of the X-AliasVault-Client request header.
///
-/// Header format is "{client}-{version}", optionally followed by additional dash-separated
-/// segments.
-///
-/// Note: currently only the Android app appends a third segment with a per-install
-/// app instance id (UUID without dashes) to support multiple Android User Profiles.
-///
-/// Examples:
-/// - "chrome-0.29.0"
-/// - "ios-0.29.0"
-/// - "android-0.29.0-550e8400e29b41d4a716446655440000".
+/// Header format is "{client}-{version}", e.g. "chrome-0.29.0", "ios-0.29.0", "android-0.29.0".
+/// Any additional dash-separated segments are tolerated for backwards compatibility but ignored.
/// </summary>
/// <param name="ClientName">Lowercased client/platform identifier (e.g. "chrome", "android"). "unknown" when the header is missing or empty.</param>
/// <param name="ClientVersion">Client version string (e.g. "0.29.0"), or null when not present.</param>
-/// <param name="AppInstanceId">Per-install app instance identifier, or null when not present.</param>
-public sealed record ClientHeaderInfo(string ClientName, string? ClientVersion, string? AppInstanceId)
+public sealed record ClientHeaderInfo(string ClientName, string? ClientVersion)
{
/// <summary>
/// Header name used by AliasVault clients to identify themselves.
@@ -35,19 +26,18 @@ public sealed record ClientHeaderInfo(string ClientName, string? ClientVersion,
/// Parse a raw X-AliasVault-Client header value into its components.
/// </summary>
/// <param name="headerValue">Raw header value, may be null or empty.</param>
- /// <returns>Parsed ClientHeaderInfo. Missing trailing segments are returned as null.</returns>
+ /// <returns>Parsed ClientHeaderInfo. Missing version is returned as null.</returns>
public static ClientHeaderInfo Parse(string? headerValue)
{
if (string.IsNullOrEmpty(headerValue))
{
- return new ClientHeaderInfo("unknown", null, null);
+ return new ClientHeaderInfo("unknown", null);
}
var parts = headerValue.Split('-');
var clientName = parts[0].ToLowerInvariant();
var clientVersion = parts.Length > 1 ? parts[1] : null;
- var appInstanceId = parts.Length > 2 ? parts[2] : null;
- return new ClientHeaderInfo(clientName, clientVersion, appInstanceId);
+ return new ClientHeaderInfo(clientName, clientVersion);
}
}
diff --git a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs b/apps/server/AliasVault.Api/Helpers/AuthHelper.cs
index a0bd98d2b..d79b263ae 100644
--- a/apps/server/AliasVault.Api/Helpers/AuthHelper.cs
+++ b/apps/server/AliasVault.Api/Helpers/AuthHelper.cs
@@ -8,6 +8,7 @@
namespace AliasVault.Api.Helpers;
using AliasServerDb;
+using AliasVault.Api.Headers;
using AliasVault.Cryptography.Client;
using Microsoft.Extensions.Caching.Memory;
using SecureRemotePassword;
@@ -86,10 +87,9 @@ public static class AuthHelper
/// conflicts when a user is logged in on multiple clients from the same browser/device.
/// For example, logging out from the browser extension won't affect the web app session.
///
- /// For Android, the identifier also includes an app instance ID at the end to support
- /// multiple Android User Profiles on the same physical device. Each Android User Profile
- /// generates a unique UUID (without dashes) on first launch that persists for the lifetime
- /// of that installation.
+ /// When the optional X-AliasVault-AppInstanceId header is present (currently only sent by the
+ /// Android app to support multiple User Profiles on the same physical device), it is appended
+ /// to keep device identifiers unique across those profiles.
///
/// Device identifier format examples:
/// - Web/Browser: "chrome|Mozilla/5.0...|en-US"
@@ -103,17 +103,21 @@ public static class AuthHelper
/// <returns>Unique device identifier as string.</returns>
public static string GenerateDeviceIdentifier(HttpRequest request)
{
- var userAgent = request.Headers.UserAgent.ToString();
- var acceptLanguage = request.Headers.AcceptLanguage.ToString();
-
var clientInfo = ClientHeaderInfo.Parse(request.Headers[ClientHeaderInfo.HeaderName].ToString());
+ var appInstanceInfo = AppInstanceIdHeaderInfo.Parse(request.Headers[AppInstanceIdHeaderInfo.HeaderName].ToString());
+
+ List<string?> parts =
+ [
+ clientInfo.ClientName,
+ request.Headers.UserAgent.ToString(),
+ request.Headers.AcceptLanguage.ToString(),
+ ];
- // Only Android appends an app instance id, used to keep device identifiers unique
- // across multiple Android User Profiles on the same physical device.
- var appInstanceSuffix = clientInfo.ClientName == "android" && clientInfo.AppInstanceId is not null
- ? $"|{clientInfo.AppInstanceId}"
- : string.Empty;
+ if (appInstanceInfo.AppInstanceId is not null)
+ {
+ parts.Add(appInstanceInfo.AppInstanceId);
+ }
- return $"{clientInfo.ClientName}|{userAgent}|{acceptLanguage}{appInstanceSuffix}";
+ return string.Join('|', parts);
}
}