Skip to content

Commit

Permalink
fix: fixes for server auth for MacUpdater (#6587)
Browse files Browse the repository at this point in the history
  • Loading branch information
devinbinnie authored Jan 28, 2022
1 parent 1de0adb commit 8746f91
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .changeset/good-scissors-listen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"electron-updater": patch
---

fix: fixes for server auth for MacUpdater
49 changes: 25 additions & 24 deletions packages/electron-updater/src/MacUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { ResolvedUpdateFileInfo, UpdateDownloadedEvent } from "./main"
import { findFile } from "./providers/Provider"
import AutoUpdater = Electron.AutoUpdater
import { execFileSync } from "child_process"
import crypto from "crypto"
import { randomBytes } from "crypto"

export class MacUpdater extends AppUpdater {
private readonly nativeUpdater: AutoUpdater = require("electron").autoUpdater
Expand Down Expand Up @@ -114,35 +114,36 @@ export class MacUpdater extends AppUpdater {
}

return await new Promise<Array<string>>((resolve, reject) => {
const pass = crypto.randomBytes(64).toString("base64").replace(/\//g, "_").replace(/\+/g, "-")
const authInfo = Buffer.from(`autoupdater:${pass}`, "base64")
const pass = randomBytes(64).toString("base64").replace(/\//g, "_").replace(/\+/g, "-")
const authInfo = Buffer.from(`autoupdater:${pass}`, "ascii")

// insecure random is ok
const fileUrl = `/${Date.now().toString(16)}-${Math.floor(Math.random() * 9999).toString(16)}.zip`
this.server!.on("request", (request: IncomingMessage, response: ServerResponse) => {
// check for basic auth header
if (!request.headers.authorization || request.headers.authorization.indexOf("Basic ") === -1) {
response.statusCode = 401
response.statusMessage = "Invalid Authentication Credentials"
response.end()
log.warn("No authenthication info")
}

// verify auth credentials
const base64Credentials = request.headers.authorization!.split(" ")[1]
const credentials = Buffer.from(base64Credentials, "base64").toString("ascii")
const [username, password] = credentials.split(":")
if (username !== "autoupdater" || password !== pass) {
response.statusCode = 401
response.statusMessage = "Invalid Authentication Credentials"
response.end()
log.warn("Invalid authenthication credentials")
return
}

const requestUrl = request.url!
log.info(`${requestUrl} requested`)
if (requestUrl === "/") {
// check for basic auth header
if (!request.headers.authorization || request.headers.authorization.indexOf("Basic ") === -1) {
response.statusCode = 401
response.statusMessage = "Invalid Authentication Credentials"
response.end()
log.warn("No authenthication info")
return
}

// verify auth credentials
const base64Credentials = request.headers.authorization.split(" ")[1]
const credentials = Buffer.from(base64Credentials, "base64").toString("ascii")
const [username, password] = credentials.split(":")
if (username !== "autoupdater" || password !== pass) {
response.statusCode = 401
response.statusMessage = "Invalid Authentication Credentials"
response.end()
log.warn("Invalid authenthication credentials")
return
}

const data = Buffer.from(`{ "url": "${getServerUrl(this.server!)}${fileUrl}" }`)
response.writeHead(200, { "Content-Type": "application/json", "Content-Length": data.length })
response.end(data)
Expand Down Expand Up @@ -193,7 +194,7 @@ export class MacUpdater extends AppUpdater {
url: getServerUrl(this.server!),
headers: {
"Cache-Control": "no-cache",
Authorization: `Basic ${authInfo.toString("ascii")}`,
Authorization: `Basic ${authInfo.toString("base64")}`,
},
})

Expand Down

0 comments on commit 8746f91

Please sign in to comment.