host: early publication checks one read of the cached import (#1429)
Astra R3 P2-3, Fable R3 P3-2: importBoundTo checked usability through one read and the account and app through a second, so a swap between them could combine checks from two entries, and the second read allowed 1 MiB. It is now one domains.ReadImportedCert call (one no-follow, non-blocking open, Fstat regular, 64 KiB cap, key, name and dates from those bytes), with account and app checked on the binding it returned.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 file changed,
+8
−28
internal/host/edgesfile_import.go+8 −28
diff --git a/internal/host/edgesfile_import.go b/internal/host/edgesfile_import.go| index de291d493..e3d8e6551 100644 |
| --- a/internal/host/edgesfile_import.go |
| +++ b/internal/host/edgesfile_import.go |
| @@ -1,9 +1,6 @@ |
| package host |
| import ( |
| - "io" |
| - "os" |
| - "path/filepath" |
| "time" |
| "github.com/carlosframework/platform/internal/domains" |
| @@ -14,32 +11,15 @@ import ( |
| // alias row's account AND its app: the edges-file writer's test for |
| // publishing an alias before its DNS points here. |
| // |
| -// domains.UsableImportedCert checks the file (a regular <alias>+import, not |
| -// a symlink), the key, the name, the dates and the account; the app is |
| -// checked here from the same entry's binding. The second read is opened |
| -// only if it is still the file that check examined, so a swap between the |
| -// two reads publishes nothing (the next pass decides again). |
| +// One read: domains.ReadImportedCert opens <acmeDir>/<alias>+import once |
| +// (no symlink followed, no blocking on a FIFO, a regular file of at most 64 |
| +// KiB) and checks the key, the name and the dates on the bytes it read; the |
| +// account and the app are checked here on the binding that same read |
| +// returned, never on a second read of the file. |
| func importBoundTo(acmeDir, alias, account, app string, now time.Time) bool { |
| - if app == "" || !domains.UsableImportedCert(acmeDir, alias, account, now) { |
| + if account == "" || app == "" { |
| return false |
| } |
| - p := filepath.Join(acmeDir, domains.ImportName(alias)) |
| - before, err := os.Lstat(p) |
| - if err != nil || !before.Mode().IsRegular() { |
| - return false |
| - } |
| - f, err := os.Open(p) |
| - if err != nil { |
| - return false |
| - } |
| - defer f.Close() |
| - if opened, err := f.Stat(); err != nil || !os.SameFile(before, opened) { |
| - return false |
| - } |
| - raw, err := io.ReadAll(io.LimitReader(f, 1<<20)) |
| - if err != nil { |
| - return false |
| - } |
| - b, _, err := domains.ParseImport(raw) |
| - return err == nil && b.Account == account && b.App == app |
| + b, _, ok := domains.ReadImportedCert(acmeDir, alias, now) |
| + return ok && b.Account == account && b.App == app |
| } |