CVE-2023-48711 (published 2023-11-24T17:15Z)

google-translate-api-browser is an npm package which interfaces with the google translate web api. A Server-Side Request Forgery (SSRF) Vulnerability is present in applications utilizing the `google-translate-api-browser` package and exposing the `translateOptions` to the end user. An attacker can set a malicious `tld`, causing the application to return unsafe URLs pointing towards local resources. The `translateOptions.tld` field is not properly sanitized before being placed in the Google translate URL. This can allow an attacker with control over the `translateOptions` to set the `tld` to a payload such as `@127.0.0.1`. This causes the full URL to become `https://translate.google.@127.0.0.1/...`, where `translate.google.` is the username used to connect to localhost. An attacker can send requests within internal networks and the local host. Should any HTTPS application be present on the internal network with a vulnerability exploitable via a GET call, then it would be possible to exploit this using this vulnerability. This issue has been addressed in release version 4.1.3. Users are advised to upgrade. There are no known workarounds for this vulnerability.

CVSS v3 base score: 3.7, LOW (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:N/I:L/A:N)

Weakness information:
cwe_id: CWE-918
cwe_name: Server-Side Request Forgery (SSRF)
description: The web server receives a URL or similar request from an upstream component and retrieves the contents of this URL, but it does not sufficiently ensure that the request is being sent to the expected destination.
extended_description: By providing URLs to unexpected hosts or ports, attackers can make it appear that the server is sending the request, possibly bypassing access controls such as firewalls that prevent the attackers from accessing the URLs directly. The server can be used as a proxy to conduct port scanning of hosts in internal networks, use other URLs such as that can access documents on the system (using file://), or use other protocols such as gopher:// or tftp://, which may provide greater control over the contents of requests.
         

Fix information:
repo_url: https://github.com/cjvnjde/google-translate-api-browser
commit_details: repo_url: https://github.com/cjvnjde/google-translate-api-browser
author: Vitalij Nykyforenko
author_date: 2023-11-22 23:54:10+02:00
msg: Add tld validation
repository: repo_url: https://github.com/cjvnjde/google-translate-api-browser
repo_name: cjvnjde/google-translate-api-browser
description: A free and unlimited API for Google Translate
repo_language: TypeScript
owner: cjvnjde
file_changes: filename: package.json
change_type: ModificationType.MODIFY
diff: @@ -1,6 +1,6 @@
 {
   "name": "google-translate-api-browser",
-  "version": "4.1.1",
+  "version": "4.1.3",
   "description": "A free and unlimited API for Google Translate that works in browser",
   "repository": {
     "type": "git",

programming_language: JavaScript
filename: generateRequestUrl.ts
change_type: ModificationType.MODIFY
diff: @@ -1,9 +1,17 @@
 import { defaultTranslateOptions } from "./defaultTranslateOptions";
 import { TranslateOptions } from "./TranslateOptions";
 
+function validateTLD(tld: string) {
+  return Boolean(tld.match(/^[a-zA-Z]{2,63}$/));
+}
+
 export function generateRequestUrl(options: Partial<Omit<TranslateOptions, 'raw'>> = {}): string {
   const translateOptions = { ...defaultTranslateOptions, ...options };
 
+  if (!validateTLD(translateOptions.tld)) {
+    throw new Error("Invalid TLD: Must be 2-63 letters only")
+  }
+
   const queryParams = {
     rpcids: translateOptions.rpcids,
     'source-path': '/',

programming_language: TypeScript
filename: normaliseResponse.ts
change_type: ModificationType.MODIFY
diff: @@ -29,7 +29,7 @@ export function normaliseResponse(rawBody: string, raw = false): TranslationResu
   }
 
   if (!data) {
-    throw new Error('No data')
+    throw new Error('Data is either empty or corrupted')
   }
 
   const result: TranslationResult = {

programming_language: TypeScript
filename: translate.server.ts
change_type: ModificationType.MODIFY
diff: @@ -4,13 +4,16 @@ import { TranslateOptions } from "./TranslateOptions";
 import { normaliseResponse, TranslationResult } from "./normaliseResponse";
 import * as https from 'node:https';
 
+export function createRequestBody(text: string, translateOptions: Pick<TranslateOptions, "to" | "from" | "rpcids">) {
+    const encodedData = encodeURIComponent(`[[["${translateOptions.rpcids}","[[\\"${text}\\",\\"${translateOptions.from}\\",\\"${translateOptions.to}\\",true],[1]]",null,"generic"]]]`);
+    return `f.req=${encodedData}&`;
+}
+
 export function translate(text: string, options: Partial<TranslateOptions> = {}): Promise<TranslationResult> {
   const translateOptions = { ...defaultTranslateOptions, ...options };
 
   return new Promise((resolve, reject) => {
-    const encodedData = encodeURIComponent(`[[["${translateOptions.rpcids}","[[\\"${text}\\",\\"${translateOptions.from}\\",\\"${translateOptions.to}\\",true],[1]]",null,"generic"]]]`);
-    const body = `f.req=${encodedData}&`;
-
+    const body = createRequestBody(text, translateOptions);
     const url = generateRequestUrl(translateOptions);
 
     const req = https.request(url, {
@@ -29,9 +32,7 @@ export function translate(text: string, options: Partial<TranslateOptions> = {})
       resp.on('end', () => {
         resolve(normaliseResponse(data))
       });
-    }).on('error', (err) => {
-      reject(err)
-    })
+    }).on('error', reject);
 
     req.write(body);
     req.end();

programming_language: TypeScript