Skip to content

Commit 5fed2bd

Browse files
authored
No password non trusted users (#68)
* Handle authenticator errors * Add test for no password auth of non-trusted users * add stack trace to error from authenticator * bump version and update changelogs * fix typo
1 parent 6381668 commit 5fed2bd

File tree

4 files changed

+43
-3
lines changed

4 files changed

+43
-3
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 2.5.2
4+
- Connecting without a password for non-trusted users throws an exception instead of timing out [#68](https://github.com/isoos/postgresql-dart/pull/68) by [osaxma](https://github.com/osaxma).
5+
36
## 2.5.1
47
- Use `substitutionValues` with `useSimpleQueryProtocol` [#62](https://github.com/isoos/postgresql-dart/pull/62) by [osaxma](https://github.com/osaxma)
58

lib/src/connection_fsm.dart

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ class _PostgreSQLConnectionStateAuthenticating
108108
case AuthenticationMessage.KindOK:
109109
return _PostgreSQLConnectionStateAuthenticated(completer);
110110
case AuthenticationMessage.KindMD5Password:
111+
// this means the server is requesting an md5 challenge
112+
// so the password must not be null
113+
if (connection!.password == null) {
114+
completer.completeError(PostgreSQLException(
115+
'Password is required for "${connection!.username}" user to establish a connection',
116+
));
117+
break;
118+
}
111119
_authenticator =
112120
createAuthenticator(connection!, AuthenticationScheme.MD5);
113121
continue authMsg;
@@ -122,14 +130,29 @@ class _PostgreSQLConnectionStateAuthenticating
122130
break;
123131
}
124132
case AuthenticationMessage.KindSASL:
133+
// this means the server is requesting a scram-sha-256 challenge
134+
// so the password must not be null
135+
if (connection!.password == null) {
136+
completer.completeError(PostgreSQLException(
137+
'Password is required for "${connection!.username}" user to establish a connection',
138+
));
139+
break;
140+
}
125141
_authenticator = createAuthenticator(
126142
connection!, AuthenticationScheme.SCRAM_SHA_256);
127143
continue authMsg;
128144
authMsg:
129145
case AuthenticationMessage.KindSASLContinue:
130146
case AuthenticationMessage.KindSASLFinal:
131-
_authenticator.onMessage(message);
132-
return this;
147+
try {
148+
_authenticator.onMessage(message);
149+
return this;
150+
} catch (e, st) {
151+
// an exception occurred in the authenticator that isn't a PostgreSQL
152+
// Exception (e.g. `Null check operator used on a null value`)
153+
completer.completeError(e, st);
154+
break;
155+
}
133156
}
134157

135158
completer.completeError(PostgreSQLException(

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: postgres
22
description: PostgreSQL database driver. Supports statement reuse and binary protocol.
3-
version: 2.5.1
3+
version: 2.5.2
44
homepage: https://github.com/isoos/postgresql-dart
55

66
environment:

test/connection_test.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,20 @@ void main() {
185185
expect(await conn.execute('select 1'), equals(1));
186186
});
187187

188+
test('Connect with no auth throws for non trusted users', () async {
189+
conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
190+
username: 'dart');
191+
try {
192+
await conn.open();
193+
} catch (e) {
194+
expect(e, isA<PostgreSQLException>());
195+
expect(
196+
(e as PostgreSQLException).message,
197+
contains('Password is required'),
198+
);
199+
}
200+
});
201+
188202
test('SSL Connect with no auth required', () async {
189203
conn = PostgreSQLConnection('localhost', 5432, 'dart_test',
190204
username: 'darttrust', useSSL: true);

0 commit comments

Comments
 (0)