Skip to content

feat(sw): retain signed envelope + derive appVersion in loader + server-requested manifest upload - #113

Open
a-d-j-i wants to merge 6 commits into
test/sw-telemetry-executorfrom
feat/sw-signed-envelope
Open

a-d-j-i wants to merge 6 commits into
test/sw-telemetry-executorfrom
feat/sw-signed-envelope

Conversation

@a-d-j-i

@a-d-j-i a-d-j-i commented Nov 18, 2026

Copy link
Copy Markdown
Collaborator

Store:

  • addLatest({appVersion, signedEnvelope, manifest}) takes caller-computed appVersion, drops the return value (caller has both inputs)
  • new getSignedEnvelope(appVersion) for server-requested re-POST
  • deleted createSyntheticAppVersion (moved to loader as deriveAppVersion)

Loader:

  • deriveAppVersion(pay) hashes the raw signed payload, not the SW-normalized manifest, so a server with the same envelope can compute the same id
  • normalizeManifestData runs in the caller; store never reshapes input

Event stream:

  • POST response header x-dappfence-request-manifest signals which appVersion the server wants a copy of; uploadManifestIfRequested re-POSTs the matching signedEnvelope best-effort (errors swallowed, doesn't affect primary POST)

Also: new HEADER_REQUEST_MANIFEST constant.

@a-d-j-i
a-d-j-i added this pull request to stack #112 November 18, 2026 18:25
@netlify

netlify Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

✅ Deploy Preview for dappfence-templates ready!

Name Link
🔨 Latest commit 4a31802
🔍 Latest deploy log https://app.netlify.com/projects/dappfence-templates/deploys/6b130cfe718f7694e85934b8
😎 Deploy Preview https://deploy-preview-113--dappfence-templates.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@a-d-j-i
a-d-j-i force-pushed the feat/sw-signed-envelope branch from 3b5814b to 279bf6d Compare November 18, 2026 18:39
@a-d-j-i
a-d-j-i force-pushed the feat/sw-signed-envelope branch 2 times, most recently from ea46361 to bc759f5 Compare December 4, 2026 13:27
@a-d-j-i
a-d-j-i requested a review from a team as a code owner December 4, 2026 13:27
…er-requested manifest upload

Store:
- addLatest({appVersion, signedEnvelope, manifest}) takes caller-computed
  appVersion, drops the return value (caller has both inputs)
- new getSignedEnvelope(appVersion) for server-requested re-POST
- deleted createSyntheticAppVersion (moved to loader as deriveAppVersion)

Loader:
- deriveAppVersion(pay) hashes the raw signed payload, not the SW-normalized
  manifest, so a server with the same envelope can compute the same id
- normalizeManifestData runs in the caller; store never reshapes input

Event stream:
- POST response header x-dappfence-request-manifest signals which appVersion
  the server wants a copy of; uploadManifestIfRequested re-POSTs the matching
  signedEnvelope best-effort (errors swallowed, doesn't affect primary POST)

Also: new HEADER_REQUEST_MANIFEST constant.
The SW logger buffers entries as {ts, level, args, count?} and log-stream was
shipping them verbatim as envelope.logs. The Worker's sanitizer expects
{ts, level, message, source?, context?, occurrenceCount?} and reads none of
the SW's fields, so every log row arrived with an empty message column in
df.mjs tail and lost its dedup count.

Add formatArgs(args) and toWireEntry(entry) helpers in log-stream.js and map
the drained batch through toWireEntry before buffering for POST. formatArgs
flattens the args array to a single string (Errors render as "Name: message";
objects JSON.stringify with String() fallback for circular refs), joins with
a space, and caps at 1024 chars with a trailing "..." marker — mirroring
MAX_LOG_MESSAGE_BYTES on the Worker so we never ship bytes the server would
discard.

deriveAppVersion collapses to calculateHash(encode(JSON.stringify(pay))):
same sha256-<base64> shape the codebase already uses for file hashes and SRI
attributes, so appVersion is directly comparable with other hashes without
reformatting. Drops the 96-bit truncation that earlier commits on this branch
emitted (manifest-<16 base64 chars>) — full SHA-256 is cheap (~26 extra chars
per envelope) and removes a birthday-collision concern on a content-addressed
identity.
…ants

Problem: the stringly-typed fields in a signed manifest — `mode`,
`pathRules[].type`, `contentRules[].action.type`, `signatureType`,
`logsMinLevel` were authored as ad-hoc string literals in build
configs and dropped silently if misspelled: the loader's permissive
normalizer would warn+fallback at runtime, but the signer happily
produced the broken manifest and operators had no way to catch the
typo before deploy.

This change:

1. Extracts the stringly-typed values into frozen enums in
   `@dappfence/core/constants` (`MODE`, `PATH_RULE_TYPE`,
   `CONTENT_ACTION_TYPE`, `SIGNATURE_TYPE`, `LOG_LEVEL`), each with a
   matching JSDoc `@typedef`. These are the single source of truth
   for both the signer and the SW loader.
2. Adds `@dappfence/manifest-tools/validate` — a strict
   `assertValidManifestPayload` that throws on any unknown enum value,
   malformed sha256 hash, malformed URL, or type mismatch. Exposed via
   `./validate` subpath export so build tools can call it directly.
3. Wires `signManifest` to call the validator by default (with a
   `skipValidation: true` escape for tests that exercise malformed
   payloads on purpose).
Wire the shared constants into the SW's manifest handling so string
literals can't drift, and extend signManifest to honor the
declared signatureType.
Mirrors the manifest-tools build-time validator on the SW side, but
in permissive mode — the SW cannot throw at runtime without bricking
every DF-protected site, so unknown values warn + drop + fall back
instead of hard-failing.

- `resolveMode` falls back to REPORTING on unknown/missing mode
  (log-only is the safer default when the integrator ships a typo).
- `filterKnownPathRules` / `filterKnownContentRules` drop rules whose
  `type` / `action.type` isn't in the enum set, warning per drop.
- `logsMinLevel` validation now uses Object.values(LOG_LEVEL) instead
  of an inline literal array — single source of truth with the enum.
- Switch the "Resolved manifest from cache" line from logger.debug
  to logger.local.log; this one fires on every fetch during steady
  state and shouldn't flow through telemetry.
@a-d-j-i
a-d-j-i force-pushed the feat/sw-signed-envelope branch from bc759f5 to 32d21ad Compare December 4, 2026 18:39
The mid-test `getPendingLogs()` drain caused the second `log('a')` to
start a fresh buffer entry, so `pending[0].ts` was a *new* `Date.now()`
call separate from the captured `firstTs`. The assertion
`pending[0].ts <= firstTs` only held when both calls landed in the
same millisecond — flaky on CI under load (1 ms off).

Drop the drain, log four times back-to-back, and keep the single
`before <= ts` bound. The assertion no longer depends on sub-ms
timing. Update count to 4 to match the new call count.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant