Server
The package server can be started with filepack serve. A demo server is
available at filepack.com.
The server has a web frontend that can be used to browse packages, as well as an API for uploading and downloading packages.
See API and Web for documentation of the API and web routes.
The server responds to requests for /{fingerprint}, /{number},
/{revision}, and /{signature} which redirect to
/package/{package}, /{key} which redirects to
/key/{key}, and /{hash} which redirects to
/directory/{hash} for directories and
/file/{hash} otherwise, and can be used as convenient
shortcuts.
Authorization
By default, the Filepack server listens on 127.0.0.1:80 and does not allow
writes, but does allow reads. Anyone who can reach the server can browse and
download packages.
Writes, including uploading and deleting packages, can be allowed with
--unrestricted-writes or --admin-key.
The --unrestricted-writes flag allows write access without authorization.
The --admin-key option, along with --domain, can be used to authorize write
access by public key. The holder of the admin private key may authorize requests
by including a tagged-hex bearer token with their requests in
the Authorization header.
Bearer tokens contain a domain, which must match the server’s --domain; a
timestamp, which must be current; a method, which must match the method of the
request; and a path, which must match the path and query string of the request.
Although bearer tokens are scoped to domain, method, and path, they can be
replayed while valid. As such, filepack commands with --auth will refuse to
run unless the connection to the server is over loopback or HTTPS.
Cross-Origin Requests
The Filepack server rejects cross-origin browser requests with methods other
than GET, HEAD, or OPTIONS. Requests are cross-origin when indicated by
the Sec-Fetch-Site header, or the Origin header when Sec-Fetch-Site is
absent.
Server responses include Cross-Origin-Resource-Policy: same-origin and
Content-Security-Policy: frame-ancestors 'self', which forbid cross-origin
embedding.
DNS Rebinding
The Filepack server protects against
DNS rebinding by requiring that
request Host headers match localhost, one of the server’s configured
domains, or be an IP address.
The --domain option can be used to add a domain that the Filepack server
accepts in the Host header, and is necessary whenever the server is accessed
with a hostname other than localhost.
HTTPS
The Filepack server can request a TLS certificate from Let’s Encrypt using the ACME protocol.
To serve HTTPS on port 443, request a TLS certificate for example.com, and
redirect requests on port 80 to port 443:
filepack serve --address 0.0.0.0 --domain example.com --https --redirect-http-to-https
ACME TLS certificates are stored in acme-cache/ in the
Data Directory.
Responses served over HTTPS include
Strict-Transport-Security: max-age=31536000. If you serve a domain over HTTPS
and then later switch to HTTP, browsers that have previously visited using HTTPS
will refuse to connect.
Storage
Server state, including the database, ACME cache, uploaded files, and incomplete files, as well as the keychain containing admin keys, is stored in the Data Directory.
The database is stored in database.redb and uses
redb.
ACME TLS certificates are cached in acme-cache/.
Uploaded files are stored in files/{hash}, where hash is the
tagged-hex BLAKE3 hash of the file contents.
Uploads are written to temporary files in incoming/ and atomically moved to
files/{hash} upon completion.
There is no disk quota, so a writable server can be filled by anyone with access.
API
API routes begin with /api. See API for details.
Typed request and response bodies are encoded with deco.
The optional Filepack-API-Version header may be included with API requests. If
included, the value should be the version of the Filepack API that the client
supports. If omitted, requests are treated as having Filepack-API-Version: 0.
Requests with an unsupported API version return 400.
API responses include a Filepack-API-Version header with the server’s maximum
supported API version.
The only supported API version is 0.
Caching
All routes that return hashed files include an ETag header whose value is the
tagged-hex hash of the file as a strong entity tag. These routes
also respect the If-None-Match header for GET and HEAD requests, including
range requests, and will return a 304 Not Modified with no body if the
If-None-Match header entity tag matches the response ETag header.
Immutable routes return Cache-Control: public, max-age=31536000, immutable
with successful responses. Immutable routes are those whose path contains a hash
which commits to the response. Note that web routes, such as
/package/{fingerprint}, are not immutable, since the HTML served may change,
for example if filepack is updated.
Package Numbers and Revisions
When a client uploads a package to the server, it uploads a revision, similar to a Git commit.
Revisions have a package field containing a package fingerprint, and a
parents field containing a set of parent revision hashes.
Uploaded packages are assigned sequential package numbers starting from zero. Deleted package numbers are not reused.
A revision with a number is a head, and a revision with no parents is a root.
Newly uploaded packages are assigned numbers and are thus heads. When a new revision of the same package is published, the package number is updated to point at the new revision, whose parents field contains the hash of the old head revision.
Server URLs
Commands like filepack upload which take server URLs also accept IP addresses
and domains without a scheme. IPs and localhost use http://; all other
domains use https://.
Uploading Packages
Packages can be uploaded to the server with
filepack upload. Uploading a package uploads
the package content, the current revision, as well as signatures over the
current revision.
filepack upload actually performs four distinct operations: package creation,
update, replacement, and preview.
After successfully creating, updating, or replacing a package on the server, the
package number and new head for that server are recorded in a state entry in
.filepack/state.json in the package root.
The current package state can be printed with
filepack state.
The state entry for a server can be removed with
filepack forget.
filepack upload defaults to operating on the package number recorded in the
destination server’s state entry, unless --number N is used to override it.
Package Creation
Package creation assigns a new package number to the current revision. It is the default when uploading a root revision to a server without a state entry.
Running filepack upload with a revision that is currently a head will return
the existing package number.
Revisions with parents that do not exist on the server cannot be uploaded, to ensure that there are no gaps in package history.
Package Update
A previously uploaded package may be updated with:
filepack upload --server <SERVER>
The server head must be a parent of the current revision. If the destination server does not have a state entry, the package whose head is the parent of the current revision is updated.
If two clients race to update the same package, the server will accept the first
and reject the second with 409 Conflict.
Package Replacement
Package replacement makes an existing package number point at a new revision,
making the old revision and its parents collectable by filepack gc if they are
not reachable from any of the server’s heads.
A previously uploaded package can be replaced with:
filepack upload --server <SERVER> --replace
The parent revisions of the replacement must already be present on the server.
Package Preview
The contents of a package can be uploaded for preview with:
filepack upload --server <SERVER> --no-revision
This uploads the package content, but does not create a revision or assign a
package number. This allows packages with parents of the current revision that
do not exist on the destination server to be uploaded. The uploaded package can
be previewed by visiting /package/<FINGERPRINT>. Packages which are uploaded
with --no-revision do not have a package number and are eligible for garbage
collection if they are not reachable by any head.
Upload API
Package uploads are content-addressed and bottom up.
Files, directories, revisions, and signatures are uploaded using
PUT /api/file/{hash}.
Directories are verified using
POST /api/directory/{hash}, which requires that
all files have been uploaded, all subdirectories have been verified, and package
metadata in that directory is valid.
The package is verified using
POST /api/package/{fingerprint}, which
requires that the package root directory has already been verified.
The revision is verified and the package number obtained using
POST /api/revision/{revision}, which requires
that the package has been verified and the revision has been uploaded and is
valid.
Finally, signatures are verified using
POST /api/signature/{hash}, which requires that
the revision has been verified and the signature has been uploaded and is valid.
Downloading Packages
Packages and files may be downloaded from the server with
filepack download.
filepack download verifies the hash of all files it downloads, so servers do
not need to be trusted, as long as the client has a known-good package
fingerprint, revision hash, or file hash.
Downloading a package by revision also downloads any signatures for that
revision uploaded to the server, and the presence of a valid signature by a
public key or key name can be asserted with the --key <KEY> option.
Deleting Packages
Packages may be deleted from the server with
filepack delete.
Garbage Collection
Deleting a package causes the package number to no longer point to a package, and replacing a package causes the number to point to a different package. These packages are still served by fingerprint and revision hash, and their files are not immediately removed from the server.
After a package is deleted or replaced, running filepack gc will remove all
revisions, signatures, packages, directories, and files which are not reachable
from the current heads or mounted packages.
Previous versions of updated packages are still reachable from the parent revisions of the new head revisions, and will continue to be served by fingerprint and revision hash.
If a file uploaded with filepack upload --file is not reachable from any
package head, it will be deleted by the next run of filepack gc.
Web Packages
Arbitrary web content can be hosted on a Filepack server using mounted web packages.
Web packages contain web content with a static/index.html entry point. They
are currently unstable and will be rejected for upload
unless the server is started with unstable features enabled.
By default, web packages are not mounted. Uploaded web packages can be viewed
and browsed at /package/{package}, but all active content, including HTML and
JavaScript, is served with Content-Security-Policy: sandbox, a non-active
Content-Type such as text/plain;charset=utf-8 or application/octet-stream,
and X-Content-Type-Options: nosniff.
Web packages can be mounted with:
filepack serve --mount <PACKAGE>
When mounted, /mount/{package}/, where package is a mounted package
fingerprint, revision hash, or number, returns static/index.html of the
mounted package, and /mount/{package}/{path} returns the package file at
static/{path}.
Mounted files are served unsandboxed from the server origin with unrestricted
content types, including text/html and text/javascript, guessed from file
extensions. Furthermore, packages mounted by number serve the latest version of
the package. On a world-writable server, anyone may update the package, causing
arbitrary HTML and JavaScript to be served from the server origin. Make sure you
understand the consequences of this and only mount packages you trust, and only
mount packages by number on servers with restricted write access.
Mounts may optionally be given a name with --mount PACKAGE:NAME, in which case
they are also served under /{NAME}/. Built-in routes take precedence over
mounts.
This book is served as a web package, mounted at filepack.com/book/.