Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't use the content-length HTTP header to check size of data #7544

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 6 additions & 0 deletions .changeset/little-spies-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"builder-util-runtime": patch
"electron-updater": patch
---

Fix differential downloads when the server compresses the blockmap file HTTP response
41 changes: 9 additions & 32 deletions packages/builder-util-runtime/src/httpExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Please double check that your authentication token is correct. Due to security r

async downloadToBuffer(url: URL, options: DownloadOptions): Promise<Buffer> {
return await options.cancellationToken.createPromise<Buffer>((resolve, reject, onCancel) => {
let result: Buffer | null = null
const responseChunks: Buffer[] = []
const requestOptions = {
headers: options.headers || undefined,
// because PrivateGitHubProvider requires HttpExecutor.prepareRedirectUrlOptions logic, so, we need to redirect manually
Expand All @@ -233,46 +233,23 @@ Please double check that your authentication token is correct. Due to security r
onCancel,
callback: error => {
if (error == null) {
resolve(result!)
resolve(Buffer.concat(responseChunks))
} else {
reject(error)
}
},
responseHandler: (response, callback) => {
const contentLength = safeGetHeader(response, "content-length")
let position = -1
if (contentLength != null) {
const size = parseInt(contentLength, 10)
if (size > 0) {
if (size > 524288000) {
callback(new Error("Maximum allowed size is 500 MB"))
return
}

result = Buffer.alloc(size)
position = 0
}
}
let receivedLength = 0
response.on("data", (chunk: Buffer) => {
if (position !== -1) {
chunk.copy(result!, position)
position += chunk.length
} else if (result == null) {
result = chunk
} else {
if (result.length > 524288000) {
callback(new Error("Maximum allowed size is 500 MB"))
return
}
result = Buffer.concat([result, chunk])
receivedLength += chunk.length
if (receivedLength > 524288000) {
callback(new Error("Maximum allowed size is 500 MB"))
return
}
responseChunks.push(chunk)
})
response.on("end", () => {
if (result != null && position !== -1 && position !== result.length) {
callback(new Error(`Received data length ${position} is not equal to expected ${result.length}`))
} else {
callback(null)
}
callback(null)
})
},
},
Expand Down