Skip to content

rpc: RPC over HTTP validates arguments before the access check #774

Open

Description

@lionelhorn

Over RpcHttpClientAdapter, arguments are validated before hasControllerAccess runs, so a caller without access sees validation errors. Over WebSocket the access check comes first and the same call gets "Access denied".

import { App } from '@deepkit/app';
import { ApplicationServer, FrameworkModule } from '@deepkit/framework';
import { rpc, RpcClient, RpcControllerAccess, RpcHttpClientAdapter, RpcKernelSecurity, RpcWebSocketClient, Session } from '@deepkit/rpc';
import { MinLength } from '@deepkit/type';

class AdminOnly extends RpcKernelSecurity {
    async hasControllerAccess(session: Session, access: RpcControllerAccess): Promise<boolean> {
        if (access.actionGroups.includes('admin')) return !session.isAnonymous();
        return true;
    }
}

type Password = string & MinLength<12>;

@rpc.controller('admin')
class AdminController {
    @rpc.action().group('admin')
    resetPassword(userId: number, password: Password): boolean {
        return true;
    }
}

const app = new App({
    controllers: [AdminController],
    providers: [{ provide: RpcKernelSecurity, useClass: AdminOnly, scope: 'rpc' }],
    imports: [new FrameworkModule({ port: 8080, httpRpcBasePath: '/api' })],
});
await app.get(ApplicationServer).start();

const ws = new RpcWebSocketClient('ws://127.0.0.1:8080').controller<AdminController>('admin');
console.log(await ws.resetPassword(1, 'short').catch((error: Error) => error.message));
// 'Access denied to action resetPassword'

const http = new RpcClient(new RpcHttpClientAdapter('http://127.0.0.1:8080/api')).controller<AdminController>('admin');
console.log(await http.resetPassword(1, 'long enough password').catch((error: Error) => error.message));
// 'Access denied to action resetPassword'
console.log(await http.resetPassword(1, 'short').catch((error: Error) => error.message));
// actual: 'Validation error:\npassword(minLength): Min length is 12 caused by value "short"'
// should be: 'Access denied to action resetPassword' (as over WebSocket)

Master 0bb9fba, Node 26.13.0.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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