Hangar Floor
DemoDownloadGitHub

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

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

3. Make the distribution

SettingValue
OriginThe bucket, with origin access control
Viewer protocolRedirect HTTP to HTTPS
CertificateA certificate of ACM in us-east-1 for your domain (to verify)
Default root objectindex.html
CompressionOn. CloudFront compresses files from 1,000 bytes to 10,000,000 bytes (to verify). ztk.wasm is near that size.
Cache policyOne 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 methodsGET and HEAD
Response headers policyThe headers below

The deploy sets the Cache-Control of each file:

FilesCache-Control
a/*public, max-age=31536000, immutable
index.html and the other filespublic, max-age=60, must-revalidate

Security headers

Put these headers in a response headers policy of the distribution:

HeaderValue
Content-Security-Policydefault-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-Securitymax-age=31536000
X-Content-Type-Optionsnosniff
Referrer-Policyno-referrer
X-Frame-OptionsDENY

The policy permits:

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:

  1. aws s3 sync of a/, one command for each kind of file, so each file has its Content-Type and its long cache time.
  2. aws s3 cp of index.html with a cache time of 60 seconds.
  3. One aws cloudfront create-invalidation of /index.html and /.

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.

PartAmount each monthPriceCost each month
S3 storageunder 1 GBabout $0.023 for each GB (to verify)under $0.03
S3 requests from CloudFrontabout 30,000 GETabout $0.0004 for each 1,000 (to verify)about $0.01
CloudFront data outabout 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 requestsabout 300,000 HTTPSthe first 10,000,000 each month have no charge (to verify)$0
Invalidationsunder 1,000 pathsthe first 1,000 paths each month have no charge (to verify)$0
Route 53 hosted zone1$0.50 (to verify)$0.50
ACM certificate1no charge for a public certificate (to verify)$0
Domain1about $12 to $15 each year for a .com or .org (to verify)about $1.25
Totalabout $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:

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:

Threat model

ThreatWhat stops it, or what is left
A script of another site reads the tokenNo 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 codeThe 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 hostconnect-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 historyThe 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 errorThe code writes no header and no token into a log line or an error text.
A person with the device reads localStorageLeft: 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 pageLeft: an extension with access to the site can read the page and the storage.
The token has more rights than the page needsThe 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 repositoryLeft 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 coordinatorEach 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

SettingValue
Callback URLhttps://YOUR-DOMAIN/ (the page itself)
Expire user authorization tokensOn: a token lasts 8 hours, and a refresh token 6 months (to verify)
Request user authorization (OAuth) during installationOff
WebhookOff
Repository permissionsMetadata: 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

SettingValue
RuntimeNode.js 20 or later (to verify)
Handlerauth.handler
Memory128 MB
Reserved concurrency5
Function URLOn, in front of CloudFront only (origin access control for Lambda, to verify)
LayerThe AWS Parameters and Secrets Lambda Extension (to verify)
EnvironmentCLIENT_ID, SITE_ORIGIN (such as https://floor.example.org), SECRET_PARAM, KEY_PARAM
ParametersTwo 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 groupA 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

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

ThreatWhat stops it, or what is left
A forged sign-in, or a code of another siteA 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 addressThe 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 tokenIt is in an HttpOnly cookie that no script can read, and only /auth/ gets it.
A copy of the cookieIt 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 functionSameSite=Strict and the check of the header Origin.
The function logs a secretIt logs no code, token, cookie, secret, or body. A test checks the log lines.
The client secret leaksIt 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 abuseA 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.