| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package sumweb |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"errors" |
| 10 |
"fmt" |
| 11 |
"path" |
| 12 |
"strings" |
| 13 |
"sync" |
| 14 |
"sync/atomic" |
| 15 |
|
| 16 |
"golang.org/x/exp/sumdb/internal/note" |
| 17 |
"golang.org/x/exp/sumdb/internal/tlog" |
| 18 |
) |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
type Client interface { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
ReadRemote(path string) ([]byte, error) |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
ReadConfig(file string) ([]byte, error) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
WriteConfig(file string, old, new []byte) error |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
ReadCache(file string) ([]byte, error) |
| 56 |
|
| 57 |
|
| 58 |
WriteCache(file string, data []byte) |
| 59 |
|
| 60 |
|
| 61 |
Log(msg string) |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
SecurityError(msg string) |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
var ErrWriteConflict = errors.New("write conflict") |
| 72 |
|
| 73 |
|
| 74 |
var ErrSecurity = errors.New("security error: misbehaving server") |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
type Conn struct { |
| 79 |
client Client |
| 80 |
|
| 81 |
didLookup uint32 |
| 82 |
|
| 83 |
|
| 84 |
initOnce sync.Once |
| 85 |
initErr error |
| 86 |
name string |
| 87 |
verifiers note.Verifiers |
| 88 |
tileReader tileReader |
| 89 |
tileHeight int |
| 90 |
nosumdb string |
| 91 |
|
| 92 |
record parCache |
| 93 |
tileCache parCache |
| 94 |
|
| 95 |
latestMu sync.Mutex |
| 96 |
latest tlog.Tree |
| 97 |
latestMsg []byte |
| 98 |
|
| 99 |
tileSavedMu sync.Mutex |
| 100 |
tileSaved map[tlog.Tile]bool |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
func NewConn(client Client) *Conn { |
| 105 |
return &Conn{ |
| 106 |
client: client, |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
func (c *Conn) init() error { |
| 113 |
c.initOnce.Do(c.initWork) |
| 114 |
return c.initErr |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
func (c *Conn) initWork() { |
| 119 |
defer func() { |
| 120 |
if c.initErr != nil { |
| 121 |
c.initErr = fmt.Errorf("initializing sumweb.Conn: %v", c.initErr) |
| 122 |
} |
| 123 |
}() |
| 124 |
|
| 125 |
c.tileReader.c = c |
| 126 |
if c.tileHeight == 0 { |
| 127 |
c.tileHeight = 8 |
| 128 |
} |
| 129 |
c.tileSaved = make(map[tlog.Tile]bool) |
| 130 |
|
| 131 |
vkey, err := c.client.ReadConfig("key") |
| 132 |
if err != nil { |
| 133 |
c.initErr = err |
| 134 |
return |
| 135 |
} |
| 136 |
verifier, err := note.NewVerifier(strings.TrimSpace(string(vkey))) |
| 137 |
if err != nil { |
| 138 |
c.initErr = err |
| 139 |
return |
| 140 |
} |
| 141 |
c.verifiers = note.VerifierList(verifier) |
| 142 |
c.name = verifier.Name() |
| 143 |
|
| 144 |
data, err := c.client.ReadConfig(c.name + "/latest") |
| 145 |
if err != nil { |
| 146 |
c.initErr = err |
| 147 |
return |
| 148 |
} |
| 149 |
if err := c.mergeLatest(data); err != nil { |
| 150 |
c.initErr = err |
| 151 |
return |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
func (c *Conn) SetTileHeight(height int) { |
| 159 |
if atomic.LoadUint32(&c.didLookup) != 0 { |
| 160 |
panic("SetTileHeight used after Lookup") |
| 161 |
} |
| 162 |
if c.tileHeight != 0 { |
| 163 |
panic("multiple calls to SetTileHeight") |
| 164 |
} |
| 165 |
c.tileHeight = height |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
func (c *Conn) SetGONOSUMDB(list string) { |
| 173 |
if atomic.LoadUint32(&c.didLookup) != 0 { |
| 174 |
panic("SetGONOSUMDB used after Lookup") |
| 175 |
} |
| 176 |
if c.nosumdb != "" { |
| 177 |
panic("multiple calls to SetGONOSUMDB") |
| 178 |
} |
| 179 |
c.nosumdb = list |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
var ErrGONOSUMDB = errors.New("skipped (listed in GONOSUMDB)") |
| 186 |
|
| 187 |
func (c *Conn) skip(target string) bool { |
| 188 |
return globsMatchPath(c.nosumdb, target) |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
func globsMatchPath(globs, target string) bool { |
| 196 |
for globs != "" { |
| 197 |
|
| 198 |
var glob string |
| 199 |
if i := strings.Index(globs, ","); i >= 0 { |
| 200 |
glob, globs = globs[:i], globs[i+1:] |
| 201 |
} else { |
| 202 |
glob, globs = globs, "" |
| 203 |
} |
| 204 |
if glob == "" { |
| 205 |
continue |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
n := strings.Count(glob, "/") |
| 212 |
prefix := target |
| 213 |
|
| 214 |
for i := 0; i < len(target); i++ { |
| 215 |
if target[i] == '/' { |
| 216 |
if n == 0 { |
| 217 |
prefix = target[:i] |
| 218 |
break |
| 219 |
} |
| 220 |
n-- |
| 221 |
} |
| 222 |
} |
| 223 |
if n > 0 { |
| 224 |
|
| 225 |
continue |
| 226 |
} |
| 227 |
matched, _ := path.Match(glob, prefix) |
| 228 |
if matched { |
| 229 |
return true |
| 230 |
} |
| 231 |
} |
| 232 |
return false |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
func (c *Conn) Lookup(path, vers string) (lines []string, err error) { |
| 239 |
atomic.StoreUint32(&c.didLookup, 1) |
| 240 |
|
| 241 |
if c.skip(path) { |
| 242 |
return nil, ErrGONOSUMDB |
| 243 |
} |
| 244 |
|
| 245 |
defer func() { |
| 246 |
if err != nil { |
| 247 |
err = fmt.Errorf("%s@%s: %v", path, vers, err) |
| 248 |
} |
| 249 |
}() |
| 250 |
|
| 251 |
if err := c.init(); err != nil { |
| 252 |
return nil, err |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
epath, err := encodePath(path) |
| 257 |
if err != nil { |
| 258 |
return nil, err |
| 259 |
} |
| 260 |
evers, err := encodeVersion(strings.TrimSuffix(vers, "/go.mod")) |
| 261 |
if err != nil { |
| 262 |
return nil, err |
| 263 |
} |
| 264 |
file := c.name + "/lookup/" + epath + "@" + evers |
| 265 |
remotePath := "/lookup/" + epath + "@" + evers |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
type cached struct { |
| 273 |
data []byte |
| 274 |
err error |
| 275 |
} |
| 276 |
result := c.record.Do(file, func() interface{} { |
| 277 |
|
| 278 |
writeCache := false |
| 279 |
data, err := c.client.ReadCache(file) |
| 280 |
if err != nil { |
| 281 |
data, err = c.client.ReadRemote(remotePath) |
| 282 |
if err != nil { |
| 283 |
return cached{nil, err} |
| 284 |
} |
| 285 |
writeCache = true |
| 286 |
} |
| 287 |
|
| 288 |
|
| 289 |
id, text, treeMsg, err := tlog.ParseRecord(data) |
| 290 |
if err != nil { |
| 291 |
return cached{nil, err} |
| 292 |
} |
| 293 |
if err := c.mergeLatest(treeMsg); err != nil { |
| 294 |
return cached{nil, err} |
| 295 |
} |
| 296 |
if err := c.checkRecord(id, text); err != nil { |
| 297 |
return cached{nil, err} |
| 298 |
} |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
if writeCache { |
| 303 |
c.client.WriteCache(file, data) |
| 304 |
} |
| 305 |
|
| 306 |
return cached{data, nil} |
| 307 |
}).(cached) |
| 308 |
if result.err != nil { |
| 309 |
return nil, result.err |
| 310 |
} |
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
prefix := path + " " + vers + " " |
| 315 |
var hashes []string |
| 316 |
for _, line := range strings.Split(string(result.data), "\n") { |
| 317 |
if strings.HasPrefix(line, prefix) { |
| 318 |
hashes = append(hashes, line) |
| 319 |
} |
| 320 |
} |
| 321 |
return hashes, nil |
| 322 |
} |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
func (c *Conn) mergeLatest(msg []byte) error { |
| 334 |
|
| 335 |
when, err := c.mergeLatestMem(msg) |
| 336 |
if err != nil { |
| 337 |
return err |
| 338 |
} |
| 339 |
if when != msgFuture { |
| 340 |
|
| 341 |
|
| 342 |
return nil |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
for { |
| 350 |
msg, err := c.client.ReadConfig(c.name + "/latest") |
| 351 |
if err != nil { |
| 352 |
return err |
| 353 |
} |
| 354 |
when, err := c.mergeLatestMem(msg) |
| 355 |
if err != nil { |
| 356 |
return err |
| 357 |
} |
| 358 |
if when != msgPast { |
| 359 |
|
| 360 |
|
| 361 |
return nil |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
c.latestMu.Lock() |
| 366 |
latestMsg := c.latestMsg |
| 367 |
c.latestMu.Unlock() |
| 368 |
if err := c.client.WriteConfig(c.name+"/latest", msg, latestMsg); err != ErrWriteConflict { |
| 369 |
|
| 370 |
return err |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
const ( |
| 376 |
msgPast = 1 + iota |
| 377 |
msgNow |
| 378 |
msgFuture |
| 379 |
) |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
func (c *Conn) mergeLatestMem(msg []byte) (when int, err error) { |
| 390 |
if len(msg) == 0 { |
| 391 |
|
| 392 |
c.latestMu.Lock() |
| 393 |
latest := c.latest |
| 394 |
c.latestMu.Unlock() |
| 395 |
if latest.N == 0 { |
| 396 |
return msgNow, nil |
| 397 |
} |
| 398 |
return msgPast, nil |
| 399 |
} |
| 400 |
|
| 401 |
note, err := note.Open(msg, c.verifiers) |
| 402 |
if err != nil { |
| 403 |
return 0, fmt.Errorf("reading tree note: %v\nnote:\n%s", err, msg) |
| 404 |
} |
| 405 |
tree, err := tlog.ParseTree([]byte(note.Text)) |
| 406 |
if err != nil { |
| 407 |
return 0, fmt.Errorf("reading tree: %v\ntree:\n%s", err, note.Text) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
c.latestMu.Lock() |
| 414 |
latest := c.latest |
| 415 |
latestMsg := c.latestMsg |
| 416 |
c.latestMu.Unlock() |
| 417 |
|
| 418 |
for { |
| 419 |
|
| 420 |
if tree.N <= latest.N { |
| 421 |
if err := c.checkTrees(tree, msg, latest, latestMsg); err != nil { |
| 422 |
return 0, err |
| 423 |
} |
| 424 |
if tree.N < latest.N { |
| 425 |
return msgPast, nil |
| 426 |
} |
| 427 |
return msgNow, nil |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
if err := c.checkTrees(latest, latestMsg, tree, msg); err != nil { |
| 432 |
return 0, err |
| 433 |
} |
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
c.latestMu.Lock() |
| 438 |
installed := false |
| 439 |
if c.latest == latest { |
| 440 |
installed = true |
| 441 |
c.latest = tree |
| 442 |
c.latestMsg = msg |
| 443 |
} else { |
| 444 |
latest = c.latest |
| 445 |
latestMsg = c.latestMsg |
| 446 |
} |
| 447 |
c.latestMu.Unlock() |
| 448 |
|
| 449 |
if installed { |
| 450 |
return msgFuture, nil |
| 451 |
} |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
func (c *Conn) checkTrees(older tlog.Tree, olderNote []byte, newer tlog.Tree, newerNote []byte) error { |
| 460 |
thr := tlog.TileHashReader(newer, &c.tileReader) |
| 461 |
h, err := tlog.TreeHash(older.N, thr) |
| 462 |
if err != nil { |
| 463 |
if older.N == newer.N { |
| 464 |
return fmt.Errorf("checking tree#%d: %v", older.N, err) |
| 465 |
} |
| 466 |
return fmt.Errorf("checking tree#%d against tree#%d: %v", older.N, newer.N, err) |
| 467 |
} |
| 468 |
if h == older.Hash { |
| 469 |
return nil |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
var buf bytes.Buffer |
| 475 |
fmt.Fprintf(&buf, "SECURITY ERROR\n") |
| 476 |
fmt.Fprintf(&buf, "go.sum database server misbehavior detected!\n\n") |
| 477 |
indent := func(b []byte) []byte { |
| 478 |
return bytes.Replace(b, []byte("\n"), []byte("\n\t"), -1) |
| 479 |
} |
| 480 |
fmt.Fprintf(&buf, "old database:\n\t%s\n", indent(olderNote)) |
| 481 |
fmt.Fprintf(&buf, "new database:\n\t%s\n", indent(newerNote)) |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
fmt.Fprintf(&buf, "proof of misbehavior:\n\t%v", h) |
| 494 |
if p, err := tlog.ProveTree(newer.N, older.N, thr); err != nil { |
| 495 |
fmt.Fprintf(&buf, "\tinternal error: %v\n", err) |
| 496 |
} else if err := tlog.CheckTree(p, newer.N, newer.Hash, older.N, h); err != nil { |
| 497 |
fmt.Fprintf(&buf, "\tinternal error: generated inconsistent proof\n") |
| 498 |
} else { |
| 499 |
for _, h := range p { |
| 500 |
fmt.Fprintf(&buf, "\n\t%v", h) |
| 501 |
} |
| 502 |
} |
| 503 |
c.client.SecurityError(buf.String()) |
| 504 |
return ErrSecurity |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
func (c *Conn) checkRecord(id int64, data []byte) error { |
| 509 |
c.latestMu.Lock() |
| 510 |
latest := c.latest |
| 511 |
c.latestMu.Unlock() |
| 512 |
|
| 513 |
if id >= latest.N { |
| 514 |
return fmt.Errorf("cannot validate record %d in tree of size %d", id, latest.N) |
| 515 |
} |
| 516 |
hashes, err := tlog.TileHashReader(latest, &c.tileReader).ReadHashes([]int64{tlog.StoredHashIndex(0, id)}) |
| 517 |
if err != nil { |
| 518 |
return err |
| 519 |
} |
| 520 |
if hashes[0] == tlog.RecordHash(data) { |
| 521 |
return nil |
| 522 |
} |
| 523 |
return fmt.Errorf("cannot authenticate record data in server response") |
| 524 |
} |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
type tileReader struct { |
| 530 |
c *Conn |
| 531 |
} |
| 532 |
|
| 533 |
func (r *tileReader) Height() int { |
| 534 |
return r.c.tileHeight |
| 535 |
} |
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
func (r *tileReader) ReadTiles(tiles []tlog.Tile) ([][]byte, error) { |
| 540 |
|
| 541 |
data := make([][]byte, len(tiles)) |
| 542 |
errs := make([]error, len(tiles)) |
| 543 |
var wg sync.WaitGroup |
| 544 |
for i, tile := range tiles { |
| 545 |
wg.Add(1) |
| 546 |
go func(i int, tile tlog.Tile) { |
| 547 |
defer wg.Done() |
| 548 |
data[i], errs[i] = r.c.readTile(tile) |
| 549 |
}(i, tile) |
| 550 |
} |
| 551 |
wg.Wait() |
| 552 |
|
| 553 |
for _, err := range errs { |
| 554 |
if err != nil { |
| 555 |
return nil, err |
| 556 |
} |
| 557 |
} |
| 558 |
|
| 559 |
return data, nil |
| 560 |
} |
| 561 |
|
| 562 |
|
| 563 |
func (c *Conn) tileCacheKey(tile tlog.Tile) string { |
| 564 |
return c.name + "/" + tile.Path() |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
func (c *Conn) tileRemotePath(tile tlog.Tile) string { |
| 569 |
return "/" + tile.Path() |
| 570 |
} |
| 571 |
|
| 572 |
|
| 573 |
func (c *Conn) readTile(tile tlog.Tile) ([]byte, error) { |
| 574 |
type cached struct { |
| 575 |
data []byte |
| 576 |
err error |
| 577 |
} |
| 578 |
|
| 579 |
result := c.tileCache.Do(tile, func() interface{} { |
| 580 |
|
| 581 |
data, err := c.client.ReadCache(c.tileCacheKey(tile)) |
| 582 |
if err == nil { |
| 583 |
c.markTileSaved(tile) |
| 584 |
return cached{data, nil} |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
full := tile |
| 591 |
full.W = 1 << uint(tile.H) |
| 592 |
if tile != full { |
| 593 |
data, err := c.client.ReadCache(c.tileCacheKey(full)) |
| 594 |
if err == nil { |
| 595 |
c.markTileSaved(tile) |
| 596 |
return cached{data[:len(data)/full.W*tile.W], nil} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
data, err = c.client.ReadRemote(c.tileRemotePath(tile)) |
| 602 |
if err == nil { |
| 603 |
return cached{data, nil} |
| 604 |
} |
| 605 |
|
| 606 |
|
| 607 |
|
| 608 |
|
| 609 |
|
| 610 |
if tile != full { |
| 611 |
data, err := c.client.ReadRemote(c.tileRemotePath(full)) |
| 612 |
if err == nil { |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
return cached{data[:len(data)/full.W*tile.W], nil} |
| 619 |
} |
| 620 |
} |
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
return cached{nil, err} |
| 625 |
}).(cached) |
| 626 |
|
| 627 |
return result.data, result.err |
| 628 |
} |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
func (c *Conn) markTileSaved(tile tlog.Tile) { |
| 633 |
c.tileSavedMu.Lock() |
| 634 |
c.tileSaved[tile] = true |
| 635 |
c.tileSavedMu.Unlock() |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
func (r *tileReader) SaveTiles(tiles []tlog.Tile, data [][]byte) { |
| 640 |
c := r.c |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
save := make([]bool, len(tiles)) |
| 645 |
c.tileSavedMu.Lock() |
| 646 |
for i, tile := range tiles { |
| 647 |
if !c.tileSaved[tile] { |
| 648 |
save[i] = true |
| 649 |
c.tileSaved[tile] = true |
| 650 |
} |
| 651 |
} |
| 652 |
c.tileSavedMu.Unlock() |
| 653 |
|
| 654 |
for i, tile := range tiles { |
| 655 |
if save[i] { |
| 656 |
|
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
c.client.WriteCache(c.name+"/"+tile.Path(), data[i]) |
| 661 |
} |
| 662 |
} |
| 663 |
} |