Skip to content

Latest commit

 

History

History
402 lines (350 loc) · 19 KB

updating.md

File metadata and controls

402 lines (350 loc) · 19 KB

Updating an application

Viva ships an application as a native program with no updater inside it. The github.com/vivaui/viva/update package adds one when an application imports it, and nothing is linked into a program that does not.

An update is either full or a delta. A full update downloads the complete archive the feed lists. A delta downloads a signed patch made between the installed payload and the new one: the .app bundle on macOS, the executable on Linux. The client applies the patch into a staging directory, verifies every file it produced, and hands the result to the same installer a full update uses. A patch that does not fit the running version, or fails at any point, leaves the installation untouched; the application downloads the full archive instead.

The package splits the work in two:

Step Who does it
Fetch and verify the feed update.Client.Check
Download and hash the archive update.Client.Download
Decide whether to ask, when to install the application
Replace the application and relaunch update.Install

The feed

A feed is two files served over HTTPS:

https://example.com/app/stable/latest.json
https://example.com/app/stable/latest.json.sig

latest.json is a small JSON document. The signature covers its exact bytes: the client fetches both, verifies the signature, and only then parses the document. A feed signed by a key the application does not trust is refused with update.ErrSignature, and a feed that parses but names something the client must not act on is refused with update.ErrManifest.

{
  "schema": 1,
  "app": "dev.example.notes",
  "channel": "stable",
  "expires": "2027-02-02T12:00:00Z",
  "latest": {
    "version": "1.4.2",
    "date": "2026-11-04T12:00:00Z",
    "notes_url": "https://example.com/notes/1.4.2",
    "min_os": {"darwin": "12.0", "windows": "10.0"},
    "assets": [
      {
        "os": "darwin",
        "arch": "arm64",
        "format": "zip",
        "url": "https://example.com/app/stable/Notes-1.4.2-darwin-arm64.zip",
        "size": 18432000,
        "sha256": "9f2c…",
        "patches": [
          {
            "from_version": "1.4.1",
            "url": "https://example.com/app/stable/Notes-1.4.2-darwin-arm64.zip.1.4.1.patch",
            "size": 240000,
            "sha256": "5b71…"
          }
        ]
      },
      {
        "os": "windows",
        "arch": "amd64",
        "format": "nsis",
        "url": "https://example.com/app/stable/Notes-1.4.2-windows-amd64-setup.exe",
        "size": 20111360,
        "sha256": "41ab…"
      }
    ]
  }
}

app is the application's identity ([app] id in viva.toml). schema is 1; a feed with any other schema is refused rather than guessed at. channel is optional, and a client configured with a channel requires an exact match. Each asset names an OS and architecture (darwin, windows, linux; amd64, arm64), a format (zip, nsis, tar.gz), a size and a lowercase SHA-256.

expires is optional. The signature covers the document but not the moment it is served, so a feed without an expiry can be replayed by anyone who can answer the request, offering a release that was since replaced because it was vulnerable. When expires is present a client past it refuses the feed (update.ErrExpired, which wraps ErrManifest); Config.Now replaces the clock in tests. viva feed -valid-for 2160h sets it; the default writes none. The cost of an expiry is that the feed has to be republished before it runs out, so choose a lifetime you will actually keep up with.

min_os is carried for the application: the client does not compare it, because only the application knows what it supports.

Keys

A feed is signed with Ed25519. The public key is compiled into the application; the private key stays with the release machine. viva update-keygen writes both:

viva update-keygen -private update.key -public update.pub

The private key file holds the standard-base64 form of the 64-byte key, and the public file the base64 of its 32 bytes. The default private key is update.key in the project root, which this repository's .gitignore excludes; never commit a key that signs releases. update.ParsePublicKey reads either base64 or hexadecimal, so the public key can live in viva.toml, a go:embed file, or a constant. More than one public key may be configured:

client, err := update.New(update.Config{
	FeedURL:    "https://example.com/app/stable/latest.json",
	PublicKeys: []ed25519.PublicKey{currentKey, nextKey},
	App:        "dev.example.notes",
	Current:    version,
})

Two keys let a release rotate the signing key without the previous release refusing the next feed. latest.json.sig holds the signature in standard base64; raw base64 is accepted too.

Publishing a feed

viva feed reads the archives viva package wrote and builds the two files:

viva package --target darwin --format zip
viva package --target windows --format nsis
viva package --target linux --format tar.gz

viva feed \
  -version 1.4.2 \
  -dir build \
  -base-url https://example.com/app/stable \
  -channel stable \
  -notes https://example.com/notes/1.4.2 \
  -key update.key \
  -out build/stable/latest.json

It recognises the names viva package produces — <program>-<version>-darwin-<arch>.zip, …-windows-<arch>-setup.exe, …-windows-<arch>.zip, …-linux-<arch>.tar.gz — and ignores everything else. The application identifier and version come from viva.toml ([app] id, [app] version) unless -app or -version overrides them. The version in every archive's file name must equal the feed's: a feed cannot name one release and list an archive built for another. The private key comes from -key or VIVA_UPDATE_KEY. The command writes latest.json and latest.json.sig; the signature is over the exact bytes of the JSON file, so the file must be published as written.

Every archive's URL is -base-url joined with its file name. A GitHub Release works as the host: upload the archives and the two feed files, and point -base-url at the release's asset directory. Channels are directories:

app/stable/latest.json
app/beta/latest.json

A feed can be checked from any HTTP client; the signature is the only thing that makes it authoritative.

Delta updates

A patch is made at release time from the previous release archive:

viva feed -dir dist -base-url https://example.com/app/stable \
  --patch-from darwin/arm64=1.4.1=old/Notes-1.4.1-darwin-arm64.zip \
  --patch-from linux/amd64=1.4.1=old/Notes-1.4.1-linux-amd64.tar.gz \
  --patch-from windows/amd64=1.4.1=old/Notes-1.4.1-windows-amd64.zip

Each --patch-from is os/arch=version=archive, repeatable, for darwin, linux and windows. The previous archive is supplied by the release process; viva feed extracts both releases and writes <archive>.<from_version>.patch beside the archives, listing it on the asset in the feed. A patch is a gzip tar holding patch.json and one bsdiff per changed file; patch.json names the app, both versions, the OS, the architecture and every file with its mode, size and SHA-256. The feed's signature covers the patch's URL, size and digest, and the client checks every file it writes against the digest in patch.json.

On Windows the payload is a directory: the program and the folders beside it from the portable zip. The same patch applies to an NSIS installation and to a portable copy, because both hold those files at the same paths. A portable copy also holds the user's data folder; leave it out of the patch with --patch-exclude motty-data (the path is relative to the payload). Install replaces the files and starts the new program without running NSIS, so shortcuts and the uninstall entry stay as they are. A machine-wide installation under Program Files is not writable and falls back to the full installer.

The application decides whether to use a patch. The order that keeps the fallback simple:

if patch, ok := asset.PatchFrom(running); ok {
	if path, err := client.DownloadPatch(ctx, asset, patch, dir, progress); err == nil {
		if staged, err := update.ApplyPatch(path, config.App, running, release.Version); err == nil {
			return update.Install(update.InstallOptions{
				Payload: staged, Asset: asset, Version: release.Version, App: config.App,
			}, quit)
		}
	}
}
// download and install the full archive

ApplyPatch refuses a patch whose application, versions, OS, architecture or payload kind do not match, copies the installed payload into a staging directory, and applies the patch there. Install then runs the same checks as a full update (the bundle identifier, version and codesign on macOS) and replaces the application. A failure in either step changes nothing: the application keeps running the old version and can download the whole archive. Install copies the payload before it calls quit, so the directory ApplyPatch returned is still the application's to delete once quit has been called.

Updating in the application

Check somewhere that does not block the UI thread, and hand the result back through the window's invalidator:

func (model *model) checkForUpdate() {
	go func() {
		feed, err := model.updates.Check(context.Background())
		if err != nil || feed == nil {
			return
		}
		model.window.Invalidate() // a field now says an update is available
	}()
}

Check returns (nil, nil) when the running version is not older than the feed's, so an application does not need to compare versions itself. When the user accepts, download into a directory the application owns and then install:

feed, err := client.Check(ctx)
if err != nil || feed == nil {
	return
}
asset, err := feed.Asset(runtime.GOOS, runtime.GOARCH)
if err != nil {
	return
}
archive, err := client.Download(ctx, asset, downloadDir, progress)
if err != nil {
	return
}
err = update.Install(update.InstallOptions{
	Artifact: archive,
	Asset:    asset,
	Version:  feed.Latest.Version,
	App:      "dev.example.notes",
	Args:     os.Args[1:],
}, application.Quit)

A download that stops part way returns an error wrapping update.ErrInterrupted: the connection failed, nothing arrived for a minute, the host answered with a temporary status (408, 429 or 5xx), or ctx ended (the error then wraps context.Canceled or context.DeadlineExceeded as well). The bytes that arrived stay in downloadDir, and calling Download again with the same directory sends a Range request for the rest; a host that ignores it sends the whole file, which is fine. When the directory already holds the verified file, Download returns it without a request. Keep downloadDir per release and remove it once the release is installed or skipped. Two processes must not download the same asset into the same directory at once. DownloadPatch behaves the same way, so a cut-off patch is worth resuming before falling back to the full archive.

Install returns only after it has started the helper that replaces the application; the helper waits for this process to exit. If Install returns an error nothing has been changed and quit was not called.

What each platform does

macOS Windows Linux
Archive the signed .app in a zip the signed NSIS installer the executable in a tar.gz
Checks the bundle id and version in Info.plist, then codesign --verify --deep --strict the installer's Authenticode signature when it has one; the feed's Ed25519 signature always the feed's Ed25519 signature
Replaces the .app bundle, by swapping it with two renames while the previous one is kept until the new version starts runs the installer silently (/S), user scope renames the new executable over the old one; the old inode keeps running until exit
Relaunches open -n through a detached helper after the process exits a detached PowerShell helper waits, installs a full setup with /S (or copies a patch's files over the installation), and starts the program starts the new executable, then the application quits
Refuses a translocated bundle, a read-only parent, a bundle without a valid signature an installer whose signature fails to verify, a read-only install directory an AppImage, a .deb or .rpm installed by its package manager, a read-only directory

A program that a package manager installed is not updated by itself. Linux detects the .deb case with dpkg-query and the .rpm case with rpm -qf, and reports update.ErrManagedExternally; a macOS Homebrew Cask and a Windows package-managed install cannot be told apart from a drag-installed copy, so the application has to avoid offering self-update there. Windows installations made by the Viva NSIS installer are per-user by default and need no elevation.

Security properties

  • HTTPS only. The feed, its signature and every archive must be https. A redirect that leaves HTTPS is refused before the request is sent (ErrInsecureURL). The client is copied, never changed: a Config.HTTP of your own keeps its settings.
  • Signature before parse. The detached signature covers the exact feed bytes and is checked before the JSON is decoded.
  • One identity. A client configured with an app or channel refuses a signed feed for another one, and Install refuses an archive the feed listed for another operating system or architecture.
  • Exact bytes. A download is written while it is hashed and is kept only when both its length and SHA-256 match the feed (ErrSize, ErrChecksum). A refused download leaves no file behind. An interrupted one keeps its bytes in a hidden file named by the feed's digest, so they are only ever continued with the rest of the same file, and the whole file is hashed again before it is kept. Install hashes the file again immediately before handing it to the platform, because the file sits on disk between the two calls.
  • Bounded. latest.json is limited to 1 MiB, its signature to 4 KiB, an archive to 512 MiB by default (Config.MaxAssetBytes), and an unpacked archive to 2 GiB.
  • Signed before replacing. The macOS installer verifies the new bundle with codesign --verify --deep --strict, and the Windows installer verifies an Authenticode signature when the installer carries one. A signature that exists but fails is ErrUntrusted; an unsigned Windows installer is accepted because the feed is the trust anchor.
  • No downgrade, no replay. A feed older than the running version is treated as "nothing newer", and a signed feed past its expires is refused. A feed with no expires can be replayed indefinitely, and one replayed within its lifetime can still offer a release that is newer than the running one but not the latest; set -valid-for as short as you can republish if that matters.
  • No telemetry. The package sends no identifier and nothing but the configured requests. It does not check at import or on a timer.

The feed's signature says the release came from whoever holds the signing key; that is the trust anchor. The macOS installer additionally verifies that the new bundle's code signature is intact (codesign --verify --deep --strict), and the Windows installer verifies an Authenticode signature when one is present. Neither pins a publisher identity: a valid signature from any developer passes. An unsigned Windows installer is accepted because the feed authenticated its bytes; SmartScreen still warns about it, and signing it is what removes that warning.

Limits

  • Trust anchor. The feed key is the only thing that makes a release authoritative. Code-signature checks catch a damaged or unsigned bundle; they do not tell the developer you meant from another one. Protect update.key like a release signing key and rotate through Config.PublicKeys.
  • Time of check, time of use. Install hashes the artifact again right before it is used, but on Windows the installer runs after the application has exited, and the file stays in the download directory between those moments. Download into a directory only the user can write.
  • macOS bundles with symbolic links. The unpacker refuses links, so a bundle that embeds a third-party framework (Versions/Current is a link) is rejected with ErrUnsafeArchive. A Viva application has none.
  • Linux replaces one executable. Files beside the program in the tar.gz are not installed. The old executable is restored only when the new one exits with a failure within two seconds of starting.
  • Silent helper timeout. If the old process does not exit within 60 seconds the macOS and Linux helpers give up without changing anything and without a message; the application keeps running the old version and can offer the update again.
  • Windows patches replace the application's files. The installer's uninstaller, shortcuts and registry entries are not changed; a release that adds or removes files needs the full installer. A machine-wide installation under Program Files is not writable and reports ErrNotWritable.
  • No update service. The package checks a URL the application names. It does not host, stage, roll out or force anything.
  • No automatic install. The application decides when to check, whether to ask, and when to call Install.
  • No Linux AppImage support and no self-update of a .deb or .rpm. viva package makes tar.gz, .deb, PKGBUILD and (with --format rpm) .rpm; the .deb and .rpm are the package manager's.
  • No elevation. The Windows helper runs the installer without asking for administrator rights, and a patch only writes a writable directory. A [windows] scope = "machine" installation under Program Files cannot be replaced without elevation; the default per-user scope needs none.
  • macOS needs a code signature. The new bundle is checked with codesign; an ad-hoc signature is enough for local testing, an unsigned build is refused.
  • No Windows arm64 installer changes. The NSIS installer is per-architecture, as viva package makes it.

Testing an update path

go test ./update ./internal/bsdiff ./tools/patchgen covers the feed parser, signatures, version ordering, download checks, archive unpacking, patch application and patch generation on every host. The parts that replace a real application need a machine and a person:

  1. Build version 1, package it, install it, run it.
  2. Build version 2, package it, run viva feed --patch-from <os>/<arch>=<version>=<archive of version 1>, host the files over HTTPS.
  3. Start version 1, check, download, install, and confirm version 2 starts.
  4. Install version 1 again and repeat through the delta, then repeat the delta with a changed installed payload, with a truncated patch, and with a patch from another version: each must fall back to the full archive.
  5. Repeat with the network dropped mid-download, with a tampered archive, and with a feed signed by another key.
  6. On macOS, confirm codesign --verify --deep --strict passes on the new bundle and that the app opens with the network off (the ticket is stapled). This also covers a bundle applied from a patch.
  7. On Windows, confirm the installer is silent, per-user, and that the new program starts after the old one exits.