Skip to content

Commit 4b94dd3

Browse files
committed
Housekeeping
1 parent 532f100 commit 4b94dd3

59 files changed

Lines changed: 1886 additions & 145 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ make build # builds the frontend, then the binary into bin/arca
3838
./bin/arca
3939
```
4040

41-
Open <http://localhost:8080> and complete the setup wizard. There is a
41+
Open <http://localhost:8080> and complete the setup wizard. It asks for the setup token the
42+
server printed to its log on startup, so nobody else can claim a fresh instance. There is a
4243
[docker compose example](https://pixelib.github.io/arca/deploying#docker) in the docs.
4344

4445
## Docs

‎branding/arca-favicon.svg‎

Lines changed: 6 additions & 0 deletions
Loading

‎branding/arca-logo.svg‎

Lines changed: 16 additions & 0 deletions
Loading

‎branding/arca-mark-mono.svg‎

Lines changed: 9 additions & 0 deletions
Loading

‎branding/arca-mark.svg‎

Lines changed: 9 additions & 0 deletions
Loading

‎docs/deploying.md‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,28 @@ Replace the binary and restart. The schema is applied on startup, and the data d
275275
touched by a deployment, so there is no migration step and no downtime beyond the restart.
276276

277277
On the first deployment there is no database. The server starts empty and serves the setup wizard,
278-
so log in and complete it before pointing any builds at the host.
278+
which asks for the setup token the server printed to its log on startup (`docker compose logs arca`,
279+
or `journalctl -u arca`), or the one `SETUP_TOKEN` was set to. Nobody who merely finds the address
280+
can complete the wizard without it. Complete it before pointing any builds at the host.
279281

280282
## TLS and reverse proxies
281283

282284
Terminating TLS is left to a reverse proxy. Two things to get right:
283285

284286
- Raise the proxy's body size limit to match `MAX_UPLOAD_BYTES`, or large artifacts fail at the
285287
proxy with an error that looks like it came from the repository.
286-
- Forward `X-Forwarded-Proto` and `X-Forwarded-Host`. npm tarball URLs are built from the request,
287-
so without them a proxied npm repository hands out `http://` links to an internal hostname.
288+
- Set `BASE_URL` to the public address, such as `https://artifacts.example.com`. npm tarball URLs
289+
and deployment link URLs are built on it. Without it they are built from the request instead,
290+
using `X-Forwarded-Proto` and `X-Forwarded-Host`, which only works out if the proxy overwrites
291+
those headers rather than passing on whatever a client sent. nginx-ingress does; check yours.
288292

289293
Serving the repository directly on a port, with no proxy in front, needs neither.
294+
295+
## Sign-in throttling
296+
297+
Password checks are throttled: after 20 failures for one account, or 100 from one client address,
298+
within five minutes, further attempts are answered `429` until the window passes. The address is
299+
the connection's peer rather than `X-Forwarded-For`, which a client can forge, so behind a proxy
300+
every client shares one address budget and the per-account limit is what carries the load. A
301+
correct password presented as Basic credentials is remembered for five minutes, so a build that
302+
sends it on every request costs one check rather than hundreds.

‎docs/installing.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,16 @@ make build # builds the frontend, then the binary into bin/arca
1313
./bin/arca
1414
```
1515

16-
Open <http://localhost:8080> and complete the setup wizard.
16+
Open <http://localhost:8080> and complete the setup wizard. Its first field is the setup token,
17+
which the server prints to its log when it starts on an empty database:
18+
19+
```
20+
WARN this instance is not set up yet; the setup wizard requires this token: Zk3v…
21+
```
22+
23+
The token is what stops whoever reaches the address first from becoming the administrator, so the
24+
instance can be exposed before setup has run. Set `SETUP_TOKEN` to choose it yourself, which an
25+
automated install wants. Once setup has completed the wizard is gone and the token is forgotten.
1726

1827
![The setup wizard]({{ site.baseurl }}/assets/img/setup.jpg)
1928

@@ -33,6 +42,8 @@ container.
3342
| `--storage` | `STORAGE_DIR` | `./data/storage` | directory holding uploaded artifacts |
3443
| `--max-upload` | `MAX_UPLOAD_BYTES` | 256 MiB | largest artifact accepted |
3544
| `--proxy-timeout` | `PROXY_TIMEOUT_SECONDS` | `60` | how long a proxy waits for its upstream |
45+
| `--base-url` | `BASE_URL` | derived per request | public URL of this server, such as `https://artifacts.example.com` |
46+
| `--setup-token` | `SETUP_TOKEN` | generated and logged | token the first-run wizard asks for |
3647
| `--dev` | `DEV_MODE` | `false` | proxy the UI to a vite dev server |
3748
| `--vite-url` | `VITE_URL` | `http://localhost:5173` | vite dev server to proxy |
3849

‎docs/managing.md‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ the repository page. A link is:
8484
| Pattern | which file of a version to serve |
8585
| Match | how to read the pattern, as a template or a regular expression |
8686
| Resolves to | releases, prereleases, or the latest of either |
87-
| Slug | the last segment of the URL, unique across the instance |
87+
| Slug | the last segment of the URL, unique across the instance; left empty, a random one is minted |
8888
| Download as | serve the file under a fixed name instead of the published one |
8989

9090
A template is literal text with `{version}`, `{baseVersion}`, `{name}` and `{namespace}` filled in,
@@ -103,7 +103,10 @@ without giving the URL up, and downloads through it are counted.
103103

104104
The slug is the credential. It is the whole address, so treat it the way you would treat a token:
105105
anyone holding it can download that file, and nothing else about the repository is reachable
106-
through it.
106+
through it. Leaving the slug empty mints a random one for that reason; a slug you choose yourself
107+
is only as secret as it is hard to guess, and a link into a private repository deserves a secret
108+
one. A disabled link, a link that resolves to nothing yet and a slug that never existed all answer
109+
the same `404`.
107110

108111
## Proxy repositories
109112

‎internal/auth/credentials.go‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package auth
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/hex"
6+
"errors"
7+
"sync"
8+
"time"
9+
10+
"arca/internal/store/models"
11+
)
12+
13+
var (
14+
ErrInvalidCredentials = errors.New("auth: invalid email or password")
15+
ErrThrottled = errors.New("auth: too many failed attempts")
16+
)
17+
18+
// ThrottledError carries how long the caller should wait.
19+
type ThrottledError struct{ RetryAfter time.Duration }
20+
21+
func (e ThrottledError) Error() string { return ErrThrottled.Error() }
22+
func (e ThrottledError) Is(target error) bool {
23+
return target == ErrThrottled
24+
}
25+
26+
// verifiedTTL is how long a login and password pair that has been checked once
27+
// is trusted without deriving the key again. The entry is bound to the stored
28+
// hash, so a password change ends it at once.
29+
const verifiedTTL = 5 * time.Minute
30+
31+
type credentialCache struct {
32+
mutex sync.Mutex
33+
entries map[string]time.Time
34+
now func() time.Time
35+
}
36+
37+
func newCredentialCache() *credentialCache {
38+
return &credentialCache{entries: map[string]time.Time{}, now: time.Now}
39+
}
40+
41+
func credentialKey(login, password, storedHash string) string {
42+
return SHA256Hex(login + "\x00" + password + "\x00" + storedHash)
43+
}
44+
45+
func (c *credentialCache) has(key string) bool {
46+
c.mutex.Lock()
47+
defer c.mutex.Unlock()
48+
49+
expires, ok := c.entries[key]
50+
if !ok {
51+
return false
52+
}
53+
if !c.now().Before(expires) {
54+
delete(c.entries, key)
55+
return false
56+
}
57+
return true
58+
}
59+
60+
func (c *credentialCache) add(key string) {
61+
c.mutex.Lock()
62+
defer c.mutex.Unlock()
63+
64+
now := c.now()
65+
if len(c.entries) >= 4096 {
66+
for candidate, expires := range c.entries {
67+
if !now.Before(expires) {
68+
delete(c.entries, candidate)
69+
}
70+
}
71+
}
72+
c.entries[key] = now.Add(verifiedTTL)
73+
}
74+
75+
// decoyHash is a real hash of a password nobody knows. Checking a password
76+
// against it costs exactly what checking against an account's hash costs, so an
77+
// email that does not exist takes as long to reject as a wrong password does.
78+
var decoyHash = sync.OnceValue(func() string {
79+
buffer := make([]byte, 16)
80+
if _, err := rand.Read(buffer); err != nil {
81+
panic("auth: unable to read random bytes: " + err.Error())
82+
}
83+
hash, err := HashPassword(hex.EncodeToString(buffer))
84+
if err != nil {
85+
panic("auth: unable to derive the decoy hash: " + err.Error())
86+
}
87+
return hash
88+
})
89+
90+
// Authenticate is the one path every password check takes: the interactive
91+
// login, the npm login, and Basic credentials on a repository request. It is
92+
// throttled per client address and per account, takes the same time whether the
93+
// account exists or not, and remembers a verified pair for a while so a client
94+
// that presents Basic credentials on every request is not charged a key
95+
// derivation each time.
96+
func (r *Resolver) Authenticate(address, login, password string) (*models.User, error) {
97+
if ok, wait := r.guard.Allow(address, login); !ok {
98+
return nil, ThrottledError{RetryAfter: wait}
99+
}
100+
101+
user, err := r.store.UserByEmail(login)
102+
if err != nil || user.Disabled {
103+
VerifyPassword(password, decoyHash())
104+
r.guard.Fail(address, login)
105+
return nil, ErrInvalidCredentials
106+
}
107+
108+
key := credentialKey(login, password, user.PasswordHash)
109+
if r.verified.has(key) {
110+
return user, nil
111+
}
112+
if !VerifyPassword(password, user.PasswordHash) {
113+
r.guard.Fail(address, login)
114+
return nil, ErrInvalidCredentials
115+
}
116+
117+
r.verified.add(key)
118+
return user, nil
119+
}
120+
121+
// Guard exposes the throttle for checks that are not a login but still cost a
122+
// key derivation, such as confirming the current password before changing it.
123+
func (r *Resolver) Guard() *Guard { return r.guard }

‎internal/auth/identity.go‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,14 @@ func cutBearer(header string) (secret string, found bool) {
5050
}
5151

5252
type Resolver struct {
53-
store *store.Store
53+
store *store.Store
54+
guard *Guard
55+
verified *credentialCache
5456
}
5557

56-
func NewResolver(database *store.Store) *Resolver { return &Resolver{store: database} }
58+
func NewResolver(database *store.Store) *Resolver {
59+
return &Resolver{store: database, guard: NewGuard(), verified: newCredentialCache()}
60+
}
5761

5862
func (r *Resolver) fromToken(secret string) *models.User {
5963
user, tokenID, err := r.store.UserByTokenHash(SHA256Hex(secret))
@@ -64,9 +68,9 @@ func (r *Resolver) fromToken(secret string) *models.User {
6468
return user
6569
}
6670

67-
func (r *Resolver) fromPassword(login, password string) *models.User {
68-
user, err := r.store.UserByEmail(login)
69-
if err != nil || user.Disabled || !VerifyPassword(password, user.PasswordHash) {
71+
func (r *Resolver) fromPassword(address, login, password string) *models.User {
72+
user, err := r.Authenticate(address, strings.ToLower(login), password)
73+
if err != nil {
7074
return nil
7175
}
7276
return user
@@ -95,7 +99,7 @@ func (r *Resolver) Resolve(request *http.Request) *models.User {
9599
if strings.HasPrefix(secret, TokenPrefix) {
96100
return r.fromToken(secret)
97101
}
98-
return r.fromPassword(login, secret)
102+
return r.fromPassword(ClientAddress(request), login, secret)
99103
}
100104

101105
if cookie, err := request.Cookie(SessionCookie); err == nil {

0 commit comments

Comments
 (0)