Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
1 change: 1 addition & 0 deletions src/api/integrations/chatbot/n8n/services/n8n.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export class N8nService extends BaseChatbotService<N8n, N8nSetting> {
pushName: pushName,
keyId: msg?.key?.id,
fromMe: msg?.key?.fromMe,
quotedMessage: msg?.contextInfo?.quotedMessage,
instanceName: instance.instanceName,
serverUrl: this.configService.get<HttpServer>('SERVER').URL,
apiKey: instance.token,
Expand Down
35 changes: 17 additions & 18 deletions src/utils/makeProxyAgent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { HttpsProxyAgent } from 'https-proxy-agent';
import { SocksProxyAgent } from 'socks-proxy-agent';

import { ProxyAgent } from 'undici'
import { ProxyAgent } from 'undici';

type Proxy = {
host: string;
Expand Down Expand Up @@ -46,38 +45,38 @@
}

export function makeProxyAgentUndici(proxy: Proxy | string): ProxyAgent {
let proxyUrl: string
let protocol: string
let proxyUrl: string;
let protocol: string;

if (typeof proxy === 'string') {
const url = new URL(proxy)
protocol = url.protocol.replace(':', '')
proxyUrl = proxy
const url = new URL(proxy);
protocol = url.protocol.replace(':', '');
proxyUrl = proxy;
} else {
const { host, password, port, protocol: proto, username } = proxy
protocol = (proto || 'http').replace(':', '')
const { host, password, port, protocol: proto, username } = proxy;

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.
protocol = (proto || 'http').replace(':', '');

if (protocol === 'socks') {
protocol = 'socks5'
protocol = 'socks5';
}

const auth = username && password ? `${username}:${password}@` : ''
proxyUrl = `${protocol}://${auth}${host}:${port}`
const auth = username && password ? `${username}:${password}@` : '';
proxyUrl = `${protocol}://${auth}${host}:${port}`;
Comment on lines +75 to +76
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Authentication string construction may not handle special characters in username or password.

Encoding the username and password will prevent malformed URLs when special characters are present.

Suggested change
const auth = username && password ? `${username}:${password}@` : '';
proxyUrl = `${protocol}://${auth}${host}:${port}`;
const encodedUsername = username ? encodeURIComponent(username) : '';
const encodedPassword = password ? encodeURIComponent(password) : '';
const auth = encodedUsername && encodedPassword ? `${encodedUsername}:${encodedPassword}@` : '';
proxyUrl = `${protocol}://${auth}${host}:${port}`;

}

const PROXY_HTTP_PROTOCOL = 'http'
const PROXY_HTTPS_PROTOCOL = 'https'
const PROXY_SOCKS4_PROTOCOL = 'socks4'
const PROXY_SOCKS5_PROTOCOL = 'socks5'
const PROXY_HTTP_PROTOCOL = 'http';
const PROXY_HTTPS_PROTOCOL = 'https';
const PROXY_SOCKS4_PROTOCOL = 'socks4';
const PROXY_SOCKS5_PROTOCOL = 'socks5';

switch (protocol) {
case PROXY_HTTP_PROTOCOL:
case PROXY_HTTPS_PROTOCOL:
case PROXY_SOCKS4_PROTOCOL:
case PROXY_SOCKS5_PROTOCOL:
return new ProxyAgent(proxyUrl)
return new ProxyAgent(proxyUrl);

default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
throw new Error(`Unsupported proxy protocol: ${protocol}`);
Comment on lines 105 to +109
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Error message could include the original input for better debugging.

Adding the full proxy input to the error message will make it easier to diagnose issues with malformed or unexpected input.

Suggested change
default:
throw new Error(`Unsupported proxy protocol: ${protocol}`)
throw new Error(`Unsupported proxy protocol: ${protocol}`);
default:
throw new Error(`Unsupported proxy protocol: ${protocol}. Full proxy input: ${typeof proxy === 'string' ? proxy : JSON.stringify(proxy)}`);

}
}
Loading