Skip to content

Commit 613b52e

Browse files
authored
Merge pull request #45 from powersync-ja/fix-websocket-authentication-errors
Require authentication on websocket connection level
2 parents cd19960 + 2a8c614 commit 613b52e

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

.changeset/popular-rivers-smell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/service-image': patch
3+
---
4+
5+
Fix websockets not being closed on authentication error

service/src/runners/server.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -85,28 +85,29 @@ export async function startServer(runnerConfig: core.utils.RunnerConfig) {
8585
const { token } = core.routes.RSocketContextMeta.decode(deserialize(data) as any);
8686

8787
if (!token) {
88-
throw new errors.ValidationError('No token provided in context');
88+
throw new errors.AuthorizationError('No token provided');
8989
}
9090

9191
try {
9292
const extracted_token = core.routes.auth.getTokenFromHeader(token);
9393
if (extracted_token != null) {
94-
const { context, errors } = await core.routes.auth.generateContext(system, extracted_token);
94+
const { context, errors: token_errors } = await core.routes.auth.generateContext(system, extracted_token);
95+
if (context?.token_payload == null) {
96+
throw new errors.AuthorizationError(token_errors ?? 'Authentication required');
97+
}
9598
return {
9699
token,
97100
...context,
98-
token_errors: errors,
101+
token_errors: token_errors,
99102
system
100103
};
104+
} else {
105+
throw new errors.AuthorizationError('No token provided');
101106
}
102107
} catch (ex) {
103108
logger.error(ex);
109+
throw ex;
104110
}
105-
106-
return {
107-
token,
108-
system
109-
};
110111
},
111112
endpoints: [core.routes.endpoints.syncStreamReactive(SocketRouter)],
112113
metaDecoder: async (meta: Buffer) => {

0 commit comments

Comments
 (0)