Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(session): reject proxy credentials with an invalid percent-escape

Yudhi Armyndharis108485d1c0ac
diff --git a/src/modules/session/dto/create-session.dto.ts b/src/modules/session/dto/create-session.dto.ts
index c14d44acf..46414012a 100644
--- a/src/modules/session/dto/create-session.dto.ts
+++ b/src/modules/session/dto/create-session.dto.ts
@@ -1,5 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
-import { IsString, IsOptional, MaxLength, MinLength, Matches, IsIn, IsUrl } from 'class-validator';
+import { IsIn, IsOptional, IsString, IsUrl, Matches, MaxLength, MinLength, Validate } from 'class-validator';
+import { HasDecodableProxyCredentialsConstraint } from './has-decodable-proxy-credentials.validator';
export class CreateSessionDto {
@ApiProperty({
@@ -56,6 +57,7 @@ export class CreateSessionDto {
},
{ message: 'proxyUrl must be a valid http(s)/socks4/socks5 URL' },
)
+ @Validate(HasDecodableProxyCredentialsConstraint)
proxyUrl?: string;
@ApiPropertyOptional({
diff --git a/src/modules/session/dto/has-decodable-proxy-credentials.validator.ts b/src/modules/session/dto/has-decodable-proxy-credentials.validator.ts
new file mode 100644
index 000000000..3469bfded
--- /dev/null
+++ b/src/modules/session/dto/has-decodable-proxy-credentials.validator.ts
@@ -0,0 +1,42 @@
+import { ValidatorConstraint, ValidatorConstraintInterface } from 'class-validator';
+
+/**
+ * Whether a proxy URL's credentials can be percent-decoded.
+ *
+ * `@IsUrl` accepts `http://user:pa%ss@proxy:8080`, and so does `new URL()`, which keeps the
+ * credentials percent-encoded. Every consumer decodes them, and a lone `%` that begins no valid
+ * escape makes `decodeURIComponent` throw `URI malformed`: inside the Baileys WebSocket agent, inside
+ * undici's ProxyAgent, inside the whatsapp-web.js Chromium launch, and on the SSRF-guarded fetch of a
+ * caller-supplied URL. Three of those four are library code, so the throw surfaces as an opaque
+ * failure with nothing naming the credentials, and the session stays unstartable until an operator
+ * guesses.
+ *
+ * Checked at the boundary instead, where the message can say what to do. A literal `%` in a password
+ * is written `%25`.
+ */
+export function hasDecodableProxyCredentials(value: unknown): boolean {
+ if (typeof value !== 'string' || value === '') return true; // other validators own shape and emptiness
+ let url: URL;
+ try {
+ url = new URL(value);
+ } catch {
+ return true; // not a URL at all: @IsUrl reports that, and a second message would only confuse
+ }
+ try {
+ decodeURIComponent(url.username);
+ decodeURIComponent(url.password);
+ return true;
+ } catch {
+ return false;
+ }
+}
+
+@ValidatorConstraint({ name: 'hasDecodableProxyCredentials', async: false })
+export class HasDecodableProxyCredentialsConstraint implements ValidatorConstraintInterface {
+ validate(value: unknown): boolean {
+ return hasDecodableProxyCredentials(value);
+ }
+ defaultMessage(): string {
+ return 'proxyUrl credentials contain an invalid percent-escape; write a literal % as %25';
+ }
+}
diff --git a/src/modules/session/dto/session-proxy-credentials.spec.ts b/src/modules/session/dto/session-proxy-credentials.spec.ts
new file mode 100644
index 000000000..2566ee953
--- /dev/null
+++ b/src/modules/session/dto/session-proxy-credentials.spec.ts
@@ -0,0 +1,23 @@
+import { validate } from 'class-validator';
+import { plainToInstance } from 'class-transformer';
+import { UpdateSessionProxyDto } from './session-proxy.dto';
+
+describe('UpdateSessionProxyDto proxy credential escapes', () => {
+ const errorsFor = async (proxyUrl: string): Promise<string[]> => {
+ const dto = plainToInstance(UpdateSessionProxyDto, { proxyUrl });
+ const errors = await validate(dto);
+ return errors.flatMap(e => Object.values(e.constraints ?? {}));
+ };
+
+ it('rejects credentials carrying a percent that begins no escape', async () => {
+ expect(await errorsFor('http://u:pa%[email protected]:8080')).toEqual([
+ expect.stringContaining('invalid percent-escape'),
+ ]);
+ });
+
+ it('accepts a properly escaped percent and an ordinary credentialed URL', async () => {
+ expect(await errorsFor('http://u:pa%[email protected]:8080')).toEqual([]);
+ expect(await errorsFor('socks5://user:p%[email protected]:1080')).toEqual([]);
+ expect(await errorsFor('http://proxy.local:8080')).toEqual([]);
+ });
+});
diff --git a/src/modules/session/dto/session-proxy.dto.ts b/src/modules/session/dto/session-proxy.dto.ts
index f01d8afcb..ac73296cd 100644
--- a/src/modules/session/dto/session-proxy.dto.ts
+++ b/src/modules/session/dto/session-proxy.dto.ts
@@ -1,5 +1,6 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
-import { IsString, IsOptional, MaxLength, IsUrl } from 'class-validator';
+import { IsOptional, IsString, IsUrl, MaxLength, Validate } from 'class-validator';
+import { HasDecodableProxyCredentialsConstraint } from './has-decodable-proxy-credentials.validator';
import type { Session } from '../entities/session.entity';
export type SessionProxyType = 'http' | 'https' | 'socks4' | 'socks5';
@@ -70,6 +71,7 @@ export class UpdateSessionProxyDto {
},
{ message: 'proxyUrl must be a valid http(s)/socks4/socks5 URL' },
)
+ @Validate(HasDecodableProxyCredentialsConstraint)
proxyUrl?: string | null;
}