From 09269b3cecc9f44a2cb5e701d5f825e18efc470c Mon Sep 17 00:00:00 2001 From: Andrew Bastin Date: Wed, 19 Jan 2022 23:11:26 +0530 Subject: [PATCH] fix: urlencoded form evaluation issues --- .../helpers/functional/debug.ts | 9 ++++++ .../helpers/functional/record.ts | 7 +++++ .../helpers/strategies/AxiosStrategy.ts | 7 +++-- .../helpers/utils/EffectiveURL.ts | 24 ++++++++++++++++ packages/hoppscotch-app/package.json | 1 + pnpm-lock.yaml | 28 +++++++++---------- 6 files changed, 58 insertions(+), 18 deletions(-) create mode 100644 packages/hoppscotch-app/helpers/functional/debug.ts create mode 100644 packages/hoppscotch-app/helpers/functional/record.ts diff --git a/packages/hoppscotch-app/helpers/functional/debug.ts b/packages/hoppscotch-app/helpers/functional/debug.ts new file mode 100644 index 000000000..74c29642b --- /dev/null +++ b/packages/hoppscotch-app/helpers/functional/debug.ts @@ -0,0 +1,9 @@ +/** + * Logs the current value and returns the same value + * @param x The value to log + * @returns The parameter `x` passed to this + */ +export const trace = (x: T) => { + console.log(x) + return x +} diff --git a/packages/hoppscotch-app/helpers/functional/record.ts b/packages/hoppscotch-app/helpers/functional/record.ts new file mode 100644 index 000000000..35ac793c5 --- /dev/null +++ b/packages/hoppscotch-app/helpers/functional/record.ts @@ -0,0 +1,7 @@ +export const tupleToRecord = < + KeyType extends string | number | symbol, + ValueType +>( + tuples: [KeyType, ValueType][] +): Record => + (Object.assign as any)(...tuples.map(([key, val]) => ({ [key]: val }))) diff --git a/packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts b/packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts index 82e62129e..5d21ab14e 100644 --- a/packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts +++ b/packages/hoppscotch-app/helpers/strategies/AxiosStrategy.ts @@ -122,8 +122,9 @@ const axiosWithoutProxy: NetworkStrategy = (req) => ) const axiosStrategy: NetworkStrategy = (req) => - settingsStore.value.PROXY_ENABLED - ? axiosWithProxy(req) - : axiosWithoutProxy(req) + pipe( + req, + settingsStore.value.PROXY_ENABLED ? axiosWithProxy : axiosWithoutProxy + ) export default axiosStrategy diff --git a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts index 85630f12f..73dbd0928 100644 --- a/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-app/helpers/utils/EffectiveURL.ts @@ -1,3 +1,7 @@ +import * as RA from "fp-ts/ReadonlyArray" +import * as S from "fp-ts/string" +import qs from "qs" +import { pipe, flow } from "fp-ts/function" import { combineLatest, Observable } from "rxjs" import { map } from "rxjs/operators" import { @@ -6,6 +10,7 @@ import { HoppRESTRequest, } from "@hoppscotch/data" import { parseTemplateString, parseBodyEnvVariables } from "../templating" +import { tupleToRecord } from "../functional/record" import { Environment, getGlobalVariables } from "~/newstore/environments" export interface EffectiveHoppRESTRequest extends HoppRESTRequest { @@ -58,6 +63,25 @@ function getFinalBodyFromRequest( return null } + if (request.body.contentType === "application/x-www-form-urlencoded") { + return pipe( + request.body.body, + S.split("\n"), + RA.map( + flow( + // Define how each lines are parsed + + S.split(":"), // Split by ":" + RA.map(S.trim), // Remove trailing spaces in key/value begins and ends + ([key, value]) => [key, value ?? ""] as [string, string] // Add a default empty by default + ) + ), + RA.toArray, + tupleToRecord, // Convert the tuple to a record + qs.stringify + ) + } + if (request.body.contentType === "multipart/form-data") { const formData = new FormData() diff --git a/packages/hoppscotch-app/package.json b/packages/hoppscotch-app/package.json index 053585b44..c6683ab36 100644 --- a/packages/hoppscotch-app/package.json +++ b/packages/hoppscotch-app/package.json @@ -94,6 +94,7 @@ "openapi-types": "^10.0.0", "paho-mqtt": "^1.1.0", "postman-collection": "^4.1.1", + "qs": "^6.10.3", "rxjs": "^7.5.2", "socket.io-client-v2": "npm:socket.io-client@^2.4.0", "socket.io-client-v3": "npm:socket.io-client@^3.1.3", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index e52495288..a110d19ad 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -153,6 +153,7 @@ importers: paho-mqtt: ^1.1.0 postman-collection: ^4.1.1 prettier: ^2.5.1 + qs: ^6.10.3 raw-loader: ^4.0.2 rxjs: ^7.5.2 sass: ^1.47.0 @@ -245,6 +246,7 @@ importers: openapi-types: 10.0.0 paho-mqtt: 1.1.0 postman-collection: 4.1.1 + qs: 6.10.3 rxjs: 7.5.2 socket.io-client-v2: /socket.io-client/2.4.0 socket.io-client-v3: /socket.io-client/3.1.3 @@ -3764,11 +3766,11 @@ packages: ufo: 0.7.9 dev: false - /@nuxt/kit-edge/3.0.0-27376999.6abf75d: - resolution: {integrity: sha512-9VdJR9s9TXHd+lRRZnZRV+5GwkWKYomIS5NONMVgBDFjxotvqkVsCbpHvUja2I0NlcAxKnFePgqeDC20CBrgHQ==} + /@nuxt/kit-edge/3.0.0-27379437.ed2d26f: + resolution: {integrity: sha512-6rGJDifwA4J/9+sN0yVKU3CLsQmK/fRQUvQLqq830wBI8ORPhNu4gXULoT0cdWPzKcera8e8Hz9iqL9aCjFNBw==} engines: {node: ^14.16.0 || ^16.11.0 || ^17.0.0} dependencies: - '@nuxt/schema': /@nuxt/schema-edge/3.0.0-27376999.6abf75d + '@nuxt/schema': /@nuxt/schema-edge/3.0.0-27379437.ed2d26f consola: 2.15.3 defu: 5.0.1 dotenv: 14.2.0 @@ -3806,8 +3808,8 @@ packages: node-fetch: 2.6.6 dev: false - /@nuxt/schema-edge/3.0.0-27376999.6abf75d: - resolution: {integrity: sha512-BdoTPuXoHT3aqQL37g/SPk1jOFO4pHbEClsj/5X6cLKwktTcqSbMkwZJF0tTyhvAwiOSFdlTvI34juPhvLNzPw==} + /@nuxt/schema-edge/3.0.0-27379437.ed2d26f: + resolution: {integrity: sha512-2KJ5/cQasFv0nrkwc2+mi9oGRFkU73luRQGA0SFAONsPTa+x+4C2Qdnav7F9KNnl95OCsGgZMblQUyCTLVdHxA==} engines: {node: ^14.16.0 || ^16.11.0 || ^17.0.0} dependencies: create-require: 1.1.1 @@ -8617,7 +8619,7 @@ packages: is-shared-array-buffer: 1.0.1 is-string: 1.0.7 is-weakref: 1.0.1 - object-inspect: 1.11.0 + object-inspect: 1.12.0 object-keys: 1.1.1 object.assign: 4.1.2 string.prototype.trimend: 1.0.4 @@ -13547,7 +13549,7 @@ packages: /nuxt-windicss/2.2.2: resolution: {integrity: sha512-4tvzk9d2TUFxloty187D+wmO8ZNAvpSmRJ5HQO3/AvZdMMhAl4gomXR9fCgFKQe7Fxcj9nIKNInOx8TWowjiqA==} dependencies: - '@nuxt/kit': /@nuxt/kit-edge/3.0.0-27376999.6abf75d + '@nuxt/kit': /@nuxt/kit-edge/3.0.0-27379437.ed2d26f '@windicss/plugin-utils': 1.6.1 consola: 2.15.3 defu: 5.0.0 @@ -13611,12 +13613,8 @@ packages: define-property: 0.2.5 kind-of: 3.2.2 - /object-inspect/1.11.0: - resolution: {integrity: sha512-jp7ikS6Sd3GxQfZJPyH3cjcbJF6GZPClgdV+EFygjFLQ5FmW/dRUnTd9PQ9k0JhoNDabWFbpF1yCdSWCC6gexg==} - /object-inspect/1.12.0: resolution: {integrity: sha512-Ho2z80bVIvJloH+YzRmpZVQe87+qASmBUKZDWgx9cu+KDrX2ZDH/3tMy+gXbZETVGs2M8YdxObOh7XAtim9Y0g==} - dev: false /object-keys/1.1.1: resolution: {integrity: sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==} @@ -13971,7 +13969,7 @@ packages: dependencies: is-ssh: 1.3.3 protocols: 1.4.8 - qs: 6.10.1 + qs: 6.10.3 query-string: 6.14.1 dev: false @@ -15209,8 +15207,8 @@ packages: resolution: {integrity: sha1-fjL3W0E4EpHQRhHxvxQQmsAGUdc=} engines: {node: '>=0.6.0', teleport: '>=0.2.0'} - /qs/6.10.1: - resolution: {integrity: sha512-M528Hph6wsSVOBiYUnGf+K/7w0hNshs/duGsNXPUCLH5XAqjEtiPGwNONLV0tBH8NoGb0mvD5JubnUTrujKDTg==} + /qs/6.10.3: + resolution: {integrity: sha512-wr7M2E0OFRfIfJZjKGieI8lBKb7fRCH4Fv5KNPEs7gJ8jadvotdsS08PzOKR7opXhZ/Xkjtt3WF9g38drmyRqQ==} engines: {node: '>=0.6'} dependencies: side-channel: 1.0.4 @@ -16094,7 +16092,7 @@ packages: dependencies: call-bind: 1.0.2 get-intrinsic: 1.1.1 - object-inspect: 1.11.0 + object-inspect: 1.12.0 /sigmund/1.0.1: resolution: {integrity: sha1-P/IfGYytIXX587eBhT/ZTQ0ZtZA=}