Skip to content

deploy-to-aca.ps1: StateStore connection string truncated to first character on macOS/Linux #210

@sebastianstephan

Description

@sebastianstephan

When running deploy-to-aca.ps1 on macOS, the StateStore connection string is truncated to its first character, causing the web app start to fail with:

[2026-02-26T14:25:13.207Z] Error: System.InvalidOperationException: Failed to initialize DocumentDBPersistence. Details: MongoDB.Driver.MongoConfigurationException: The connection string '"m"' is not valid.
   at MongoDB.Driver.Core.Configuration.ConnectionString.Parse()
   at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString, Boolean isInternalRepresentation, IDnsResolver dnsResolver)
   at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString)
   at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
   at MongoDB.Driver.MongoUrlBuilder..ctor(String url)
   at MongoDB.Driver.MongoUrl..ctor(String url)
   at MongoDB.Driver.MongoClientSettings.FromConnectionString(String connectionString)
   at MongoDB.Driver.MongoClient..ctor(String connectionString)
   at OnlineMongoMigrationProcessor.Persistence.DocumentDBPersistence.Initialize(String connectionStringOrPath, String appId) in /src/OnlineMongoMigrationProcessor/Persistence/DocumentDBPersistence.cs:line 110
 ---> MongoDB.Driver.MongoConfigurationException: The connection string '"m"' is not valid.
   at MongoDB.Driver.Core.Configuration.ConnectionString.Parse()
   at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString, Boolean isInternalRepresentation, IDnsResolver dnsResolver)
   at MongoDB.Driver.Core.Configuration.ConnectionString..ctor(String connectionString)
   at MongoDB.Driver.MongoUrlBuilder.Parse(String url)
   at MongoDB.Driver.MongoUrlBuilder..ctor(String url)

Root Cause:

The issue stems from two bugs in the connection string handling:

Wrong marshalling method for BSTR on non-Windows platforms

SecureStringToBSTR returns a pointer to a BSTR, which is always UTF-16 encoded — every ASCII character occupies 2 bytes (e.g., m → 0x6D 0x00). PtrToStringAuto selects encoding based on the platform: on Windows it uses UTF-16 (which happens to work), but on macOS/Linux (.NET Core) it uses UTF-8. When interpreting the UTF-16 bytes as UTF-8, it reads 0x6D (m) followed by 0x00 (null terminator) and stops immediately, yielding only the first character.

Additionally, the BSTR pointer was never freed, causing a memory leak.

Escaped quotes around the parameter value

The connection string was wrapped in escaped double quotes before being passed as a Bicep parameter, so the value m became "m" (with literal quote characters) — which is what the MongoDB driver reports as invalid.

Fix:

Replace PtrToStringAuto with PtrToStringBSTR, which correctly reads the BSTR length prefix and decodes the full UTF-16 string regardless of platform.
Free the BSTR pointer with ZeroFreeBSTR in a finally block to prevent memory leaks.
Remove the escaped quotes from the Bicep parameter value.
Impact: Deployment via deploy-to-aca.ps1 fails on all non-Windows platforms. Windows users are unaffected because PtrToStringAuto coincidentally uses UTF-16 there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions