Host the status page on AWS
This page tells how to serve the status page from an S3 bucket through CloudFront, with its data from a branch on GitHub. You make the AWS account, the bucket, the distribution, the certificate, and the domain. The tools of this repository create nothing in AWS.
Each price and each fact about AWS or GitHub on this page that was not checked against the documentation of today has the mark (to verify). Check each one before you depend on it.
What you get
- A static page:
index.htmland the foldera/. No server runs. - The data comes from a branch of your repository, through
api.github.com(see "Data from a GitHub branch" in the status page). - The coordinator writes the data with
hangar-floor data putandhangar-floor data push. - The branch holds public data only: the floor (
state/floor.json) with its public keys. Questions, answers, the river, and the token numbers stay in the private store. The writer refuses each other document and removes each key that is not public. - A public repository needs no token. A private repository needs a token of the reader, which the browser keeps (see the last section).
- The page with this source shows the floor only, and the owner answers in chat. The setting
"write": trueturns on answers on the page, as a commit for each answer, with a token that may write. Use it only for a branch with no public reader.
1. Build the folder
Write a settings file, for example floor-home.json, outside the repository:
{"title": "Lumen floor",
"home": {"github": {"repo": "OWNER/NAME", "branch": "hangar-floor-data"}}}
Then build:
ztcl --allow file,channels,exec --tclsh tools/build-app.tcl static build/static --home floor-home.json
The folder has index.html (the full page), floor.html (the shared floor, read-only), and the folder a/. Both pages use the settings of the file; "tokens": false in home keeps the token numbers off the shared floor. Each file of a/ has the start of the hash of its content in its name, such as a/ztk.e68b658e5ea0.js. A new version of a file gets a new name, so a cache can keep each file of a/ for a long time. index.html keeps its name and names the files of a/. It has no inline script and no inline style, so the content security policy below can be strict.
2. Make the bucket
- One bucket for the page, in one region.
- Block all public access. CloudFront reads the bucket through origin access control (OAC). The bucket policy permits
s3:GetObjectonly to the servicecloudfront.amazonaws.com, on the condition that the source is your distribution (to verify). - No static website hosting on the bucket. CloudFront uses the REST endpoint of the bucket.
- Versioning is optional. The deploy does not delete old files of
a/, so a page that is open keeps working.
3. Make the distribution
| Setting | Value |
|---|---|
| Origin | The bucket, with origin access control |
| Viewer protocol | Redirect HTTP to HTTPS |
| Certificate | A certificate of ACM in us-east-1 for your domain (to verify) |
| Default root object | index.html |
| Compression | On. CloudFront compresses files from 1,000 bytes to 10,000,000 bytes (to verify). ztk.wasm is near that size. |
| Cache policy | One that follows the Cache-Control of the origin, such as the managed policy CachingOptimized, with a minimum TTL of 0 or 1 second (to verify) |
| Allowed methods | GET and HEAD |
| Response headers policy | The headers below |
The deploy sets the Cache-Control of each file:
| Files | Cache-Control |
|---|---|
a/* | public, max-age=31536000, immutable |
index.html and the other files | public, max-age=60, must-revalidate |
Security headers
Put these headers in a response headers policy of the distribution:
| Header | Value |
|---|---|
Content-Security-Policy | default-src 'none'; script-src 'self' 'wasm-unsafe-eval'; style-src 'self'; img-src 'self' data: blob:; font-src 'self'; connect-src 'self' https://api.github.com; worker-src blob:; base-uri 'none'; form-action 'none'; frame-ancestors 'none' |
Strict-Transport-Security | max-age=31536000 |
X-Content-Type-Options | nosniff |
Referrer-Policy | no-referrer |
X-Frame-Options | DENY |
The policy permits:
- scripts and styles of the site only, and the compile of WebAssembly (
'wasm-unsafe-eval'); - data from the site and from
https://api.github.comonly, so GitHub is the one data host; - a worker from a blob: the clock of the sound runs in it;
- no frame of the page in another site.
index.html also has the same policy in a meta element, with no frame-ancestors, because a meta element cannot set it. lib/static.tcl has the policy (hangar::static::csp), and a test checks it.
4. Deploy
The deploy script prints the commands of the AWS CLI and runs nothing. That is the default:
ztcl --allow file,channels --tclsh tools/deploy-aws.tcl BUCKET DISTRIBUTION_ID --dir build/static
Read the commands. Then run them yourself, or run the script with --apply in a tclsh that has the login of the AWS CLI:
tclsh tools/deploy-aws.tcl BUCKET DISTRIBUTION_ID --dir build/static --apply
The order of the commands:
aws s3 syncofa/, one command for each kind of file, so each file has itsContent-Typeand its long cache time.aws s3 cpofindex.htmlwith a cache time of 60 seconds.- One
aws cloudfront create-invalidationof/index.htmland/.
The files of a/ go first, so a new index.html never names a file that is not there yet. The sync does not delete: remove old files of a/ from time to time, for example the files older than 30 days.
Cost
An estimate for 250 readers and 1,000 repositories, each reader with 4 page loads each day. A page load sends about 10 MB the first time (ztk.wasm); the browser keeps the files of a/, so a later load sends about 100 KB. The data does not go through AWS: the browser reads it from GitHub.
| Part | Amount each month | Price | Cost each month |
|---|---|---|---|
| S3 storage | under 1 GB | about $0.023 for each GB (to verify) | under $0.03 |
| S3 requests from CloudFront | about 30,000 GET | about $0.0004 for each 1,000 (to verify) | about $0.01 |
| CloudFront data out | about 6 GB (250 first loads of 10 MB, then 30,000 loads of about 100 KB) | the first 1 TB each month has no charge in the free tier of CloudFront (to verify) | $0 |
| CloudFront requests | about 300,000 HTTPS | the first 10,000,000 each month have no charge (to verify) | $0 |
| Invalidations | under 1,000 paths | the first 1,000 paths each month have no charge (to verify) | $0 |
| Route 53 hosted zone | 1 | $0.50 (to verify) | $0.50 |
| ACM certificate | 1 | no charge for a public certificate (to verify) | $0 |
| Domain | 1 | about $12 to $15 each year for a .com or .org (to verify) | about $1.25 |
| Total | about $1.80 |
The count of repositories changes only the data on GitHub, not the cost on AWS. GitHub limits a reader with no token to about 60 requests of the API each hour (to verify). The page reads one tree each minute, and GitHub says that an answer 304 does not count against the limit (to verify).
Private repositories and the token
A private repository needs a token in the browser. There are two ways, and the same data source code uses both:
- Step A: a token that the reader makes and gives to the page.
- Step B: a sign-in with a GitHub App, through one Lambda function (next section).
In step A the reader makes a fine-grained personal access token for the one repository, with the right Contents (read-only, or read and write to answer), and pastes it into the panel GitHub token of the page.
What the page does with the token:
- It keeps the token in
localStorageof the browser, on that device only, under a key for the one repository. - It sends the token only to
https://api.github.com, only in the headerAuthorization. The address of each request starts with that host in the code (GH_APIofweb/github.mjs), and a test checks each request. - It never puts the token into a URL, a log line, an error text, or the data of the app.
- The control Forget removes the token and starts the page again.
Threat model
| Threat | What stops it, or what is left |
|---|---|
| A script of another site reads the token | No third-party script: the policy permits scripts of the site only (script-src 'self'), and the page has no inline script. |
| An injected text of the data runs as code | The app shows each text of the data as text, and the policy permits no inline script, so an injected <script> does not run. |
| A script of the page sends the token to another host | connect-src permits only the site and https://api.github.com. |
| The page shows in a frame of another site (clickjacking of the panel) | frame-ancestors 'none' and X-Frame-Options: DENY. |
| The token is in a URL, so it goes into a log of a proxy, of CloudFront, or of the history | The token is only in a header. The page reads no token from a URL. |
| The token is in the console or in a report of an error | The code writes no header and no token into a log line or an error text. |
A person with the device reads localStorage | Left: the storage of a browser is not secret from the user of the device. Use a token with the least rights and a short expiry, and click Forget on a shared device. |
| An extension of the browser reads the page | Left: an extension with access to the site can read the page and the storage. |
| The token has more rights than the page needs | The panel asks for a fine-grained token for the one repository and the right Contents only. GitHub makes the token, so the page cannot check its rights before a write (to verify). |
| A stolen token writes to the repository | Left for the time of the token. A read-only token cannot write. Revoke a token on GitHub at once when the device is lost. |
| A write of the page drops a change of the coordinator | Each write gives the SHA of the file that the page read; GitHub refuses a write on a changed file, and the page reads the branch again and tries one more time. The writer of the coordinator keeps an answer of the owner. |
Step B: the sign-in with a GitHub App
The sign-in needs a small server part for the exchange of the code: the client secret of the app must stay out of the browser. lambda/auth.mjs is that part, as one file for Node.js with no npm package. This repository deploys nothing. You make the app, the function, and the parameters.
The GitHub App
| Setting | Value |
|---|---|
| Callback URL | https://YOUR-DOMAIN/ (the page itself) |
| Expire user authorization tokens | On: a token lasts 8 hours, and a refresh token 6 months (to verify) |
| Request user authorization (OAuth) during installation | Off |
| Webhook | Off |
| Repository permissions | Metadata: read-only. Contents: read-only, or read and write to answer from the page. |
Install the app on the repository of the data. The token of a user can read only the repositories where the app is installed and the user has access (to verify).
Put the client id into #home of the page: "signin": { "clientId": "Iv1.abc..." }. The client id is not a secret.
The function
| Setting | Value |
|---|---|
| Runtime | Node.js 20 or later (to verify) |
| Handler | auth.handler |
| Memory | 128 MB |
| Reserved concurrency | 5 |
| Function URL | On, in front of CloudFront only (origin access control for Lambda, to verify) |
| Layer | The AWS Parameters and Secrets Lambda Extension (to verify) |
| Environment | CLIENT_ID, SITE_ORIGIN (such as https://floor.example.org), SECRET_PARAM, KEY_PARAM |
| Parameters | Two SecureString parameters of Parameter Store: the client secret, and 32 random bytes in base64 for the cookie key (openssl rand -base64 32). Only the role of the function may read them. |
| Log group | A retention of 14 days |
In CloudFront, add a behavior for /auth/* to the function URL: methods GET, HEAD, OPTIONS, PUT, POST, PATCH, DELETE (POST is the one that the function takes), the managed cache policy CachingDisabled, and an origin request policy that sends the cookies and the header Origin (to verify).
What the function does
POST /auth/tokenwith the code and the PKCE verifier: it gets the tokens from GitHub, gives the page the access token and its end time, and puts the refresh token into a cookie.POST /auth/refresh: a new access token from the cookie, for example after a reload.POST /auth/signout: the cookie goes, and GitHub revokes the access token of the page (to verify).
The page keeps the access token in memory only. The cookie is Secure, HttpOnly, SameSite=Strict, with the path /auth/, and its value is the refresh token encrypted with AES-256-GCM. The function takes only POST with the header Origin of the site, and a body of at most 4 KB. It logs only the method, the path, the status, and the time.
More threats of the sign-in
| Threat | What stops it, or what is left |
|---|---|
| A forged sign-in, or a code of another site | A random state, checked at the return, and PKCE with S256. The return address is the page, and GitHub permits only the callback address of the app. |
| The code stays in the address | The page removes the code and the state from the address before it posts them. A code works one time (to verify). |
| A script reads the refresh token | It is in an HttpOnly cookie that no script can read, and only /auth/ gets it. |
| A copy of the cookie | It is encrypted with a key of Parameter Store. A copy works only on this site, until the sign-out or the end of the refresh token. |
| Another site calls the function | SameSite=Strict and the check of the header Origin. |
| The function logs a secret | It logs no code, token, cookie, secret, or body. A test checks the log lines. |
| The client secret leaks | It is a SecureString of Parameter Store, and only the role of the function reads it. Rotate it in the settings of the app. |
| A large bill from abuse | A reserved concurrency of 5, an alarm of AWS Budgets, and CloudFront in front of the function. The data reads go to GitHub, not to AWS. |
The function runs about 30,000 times each month for 250 readers with 4 loads each day. The free tier of Lambda has 1 million calls each month (to verify), so its cost is about $0.
Next: the tools.