Skip to content

Commit d4bc34e

Browse files
authored
fix url parsing inside project manager shim (#13740)
1 parent c7ced77 commit d4bc34e

File tree

1 file changed

+12
-14
lines changed
  • app/gui/project-manager-shim-middleware

1 file changed

+12
-14
lines changed

app/gui/project-manager-shim-middleware/index.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,16 @@ export default function projectManagerShimMiddleware(
147147
response: http.ServerResponse,
148148
next: () => void,
149149
) {
150-
const requestUrl = request.url
151-
const requestPath = requestUrl?.split('?')[0]?.split('#')[0]
152-
if (requestUrl != null && requestUrl.startsWith('/api/project-manager/')) {
153-
const actualUrl = new URL(
154-
requestUrl.replace(/^\/api\/project-manager/, GLOBAL_CONFIG.projectManagerHttpEndpoint),
150+
const requestUrl = request.url ?? ''
151+
if (!requestUrl.startsWith('/api/')) return next()
152+
const url = new URL(requestUrl, 'https://apishim.local')
153+
const requestPath = url.pathname
154+
if (requestPath.startsWith('/api/project-manager/')) {
155+
const urlString = requestUrl.replace(
156+
/^\/api\/project-manager/,
157+
GLOBAL_CONFIG.projectManagerHttpEndpoint,
155158
)
159+
const actualUrl = new URL(urlString)
156160
request.pipe(
157161
http.request(
158162
// `...actualUrl` does NOT work because `URL` properties are not enumerable.
@@ -181,7 +185,6 @@ export default function projectManagerShimMiddleware(
181185
} else if (requestUrl != null && requestUrl.startsWith('/api/cloud/')) {
182186
switch (requestPath) {
183187
case '/api/cloud/download-project': {
184-
const url = new URL(`https://example.com/${requestUrl}`)
185188
const downloadUrl = url.searchParams.get('downloadUrl')
186189
const projectId = url.searchParams.get('projectId')
187190

@@ -227,7 +230,6 @@ export default function projectManagerShimMiddleware(
227230
break
228231
}
229232
case '/api/cloud/get-project-archive': {
230-
const url = new URL(`https://example.com/${requestUrl}`)
231233
const parentDir = url.searchParams.get('directory')
232234

233235
if (parentDir == null) {
@@ -261,21 +263,19 @@ export default function projectManagerShimMiddleware(
261263
}
262264
}
263265
} else if (request.method === 'POST') {
264-
const params = new URL(requestUrl ?? '').searchParams
265266
switch (requestPath) {
266267
case `/api/${EXPORT_ARCHIVE_PATH}`: {
267-
httpDownloadArchive(request, response, params)
268+
httpDownloadArchive(request, response, url.searchParams)
268269
break
269270
}
270271
case '/api/upload-file': {
271-
httpUploadFile(request, response, params)
272+
httpUploadFile(request, response, url.searchParams)
272273
break
273274
}
274275
// This endpoint should only be used when accessing the app from the browser.
275276
// When accessing the app from Electron, the file input event will have the
276277
// full system path.
277278
case '/api/upload-project': {
278-
const url = new URL(`https://example.com/${requestUrl}`)
279279
const directory = url.searchParams.get('directory')
280280
const name = url.searchParams.get('name')
281281
void projectManagement
@@ -295,9 +295,7 @@ export default function projectManagerShimMiddleware(
295295
break
296296
}
297297
case '/api/run-project-manager-command': {
298-
const cliArguments: unknown = JSON.parse(
299-
new URL(`https://example.com/${requestUrl}`).searchParams.get('cli-arguments') ?? '[]',
300-
)
298+
const cliArguments: unknown = JSON.parse(url.searchParams.get('cli-arguments') ?? '[]')
301299
if (
302300
!Array.isArray(cliArguments) ||
303301
!cliArguments.every((item): item is string => typeof item === 'string')

0 commit comments

Comments
 (0)