Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-doors-shine.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/lib-service-mongodb': patch
---

Fix authentication failure when MongoDB password contains URL-encoded characters.
7 changes: 5 additions & 2 deletions libs/lib-mongodb/src/types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,11 @@ export function normalizeMongoConfig(options: BaseMongoConfigDecoded): Normalize
}

const database = options.database ?? uri.pathname.split('/')[1] ?? '';
const username = options.username ?? uri.username;
const password = options.password ?? uri.password;
// ConnectionURI's username/password getters return URL-encoded values.
// Decode them so SCRAM auth uses the actual credentials, not the encoded form.
// Without this, passwords containing characters like '=', '@', '+' fail authentication.
const username = options.username ?? decodeURIComponent(uri.username);
const password = options.password ?? decodeURIComponent(uri.password);

uri.password = '';
uri.username = '';
Expand Down
10 changes: 10 additions & 0 deletions libs/lib-mongodb/test/src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,16 @@ describe('config', () => {
expect(normalized.password).equals('pass');
});

test('Should decode URL-encoded credentials', () => {
const uri = 'mongodb://user:pass%3Dword%40host@localhost:27017/powersync_test';
const normalized = normalizeMongoConfig({
type: 'mongodb',
uri
});
expect(normalized.username).equals('user');
expect(normalized.password).equals('pass=word@host');
});

test('Should normalize a +srv URI', () => {
const uri = 'mongodb+srv://user:pass@localhost/powersync_test';
const normalized = normalizeMongoConfig({
Expand Down
Loading