HugoRodriguesQW / rsa-bridge

Encrypted communication bridge using RSA keys between frontend and backend
https://rsa-bridge.vercel.app
MIT License
1 stars 0 forks source link

RSA Gate function not working with express #4

Open HugoRodriguesQW opened 4 months ago

HugoRodriguesQW commented 4 months ago
 gate(handler) { 
        if (handler)
            handler.__preserved = handler === null || handler === void 0 ? void 0 : handler.bind(handler);
        const PreservedSend = handler;

        return async (req, res, ...props) => {
            var _a;
            const clientKey = req.headers["x-client-key"];
            if (!(clientKey === null || clientKey === void 0 ? void 0 : clientKey.length))
                return this.refuse(res, 400);
            req.body = await this.readRequestDataFrom(req);
            if (req.body) {
                try {
                    req.body = this.decrypt(req.body);
                }
                catch {
                    return this.refuse(res);
                }
            }
            Object.assign(req, { RSA: this });
            (_a = res.removeHeader) === null || _a === void 0 ? void 0 : _a.call(res, "x-client-key");
            res.send = this.injectedSend(res.send.bind(res), clientKey);
            if (handler) {
                return PreservedSend.__preserved.call(handler, ...[req, res, ...props]);
            }
            if (typeof props[0] === "function") {
                return props[0]();
            }
        };
    }

it is not working the way below in express, because when it is called by express it passes the req res and next parameters (as expected), but rsaBridge understands it as a handler and tries to preserve it....

app.use(rsa.gate);

Expect to treat its inputs differently between the handler and the parameters of a request.

This error also applies to RSAServer.publish not working as express handler #2