-
Notifications
You must be signed in to change notification settings - Fork 0
fix(clearfolio): require explicit artifact origin trust #504
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
9f7de29
ce4e7c5
462f659
865fec5
e4d2f5f
1595046
82ff679
6657767
2954f1a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| # Clearfolio artifact-origin trust boundary | ||
|
|
||
| ## Decision | ||
|
|
||
| ScopeWeave treats every artifact link returned by Clearfolio as untrusted provider data. The configured `CLEARFOLIO_URL` origin is the default artifact trust boundary. A production operator may add reviewed CDN or object-storage origins through `CLEARFOLIO_ARTIFACT_ORIGINS`, but each entry must be an origin only: HTTPS scheme, host, and optional non-default port, with no credentials, path, query, fragment, or empty comma-separated entry. | ||
|
|
||
| This slice is deliberately narrower than the complete Clearfolio production-adapter program in issue #489. It does not claim that provider DNS/IP authorization, artifact content validation, retention, or all operational acceptance work is complete. It closes the redirect-origin and token-confusion boundary on top of the provider-response controls owned by the parent PR. | ||
|
|
||
| ## Why exact origins | ||
|
|
||
| RFC 6454 defines an origin around scheme, host, and port. Comparing canonical URL origins therefore keeps `https://cdn.example`, `https://cdn.example:8443`, and HTTP variants in distinct trust domains instead of relying on string-prefix matching. The WHATWG URL Standard supplies the parser and serialization semantics used by Node's `URL` implementation, including explicit username/password and fragment components. | ||
|
|
||
| The adapter uses a positive allowlist rather than accepting any syntactically valid HTTPS URL. OWASP's SSRF guidance recommends allowlisting known destinations and disabling or tightly validating redirects when the intended service set is known. Although ScopeWeave is redirecting a browser to a provider-selected artifact rather than issuing a second server-side fetch, the same positive-trust principle prevents an untrusted provider response from turning the application into an arbitrary external redirector. | ||
|
|
||
| ## Runtime contract | ||
|
|
||
| 1. `CLEARFOLIO_ARTIFACT_ORIGINS` is optional. When absent, only the validated Clearfolio provider origin is trusted. | ||
| 2. When present, the value is a comma-separated list of canonicalizable HTTPS origins. Whitespace around entries is ignored; empty entries are rejected. | ||
| 3. Any malformed entry, HTTP entry, URL credential, path, query, or fragment produces `ClearfolioConfigurationError` with stable code `clearfolio_artifact_origins_invalid` before the artifact-link provider request is sent. | ||
| 4. Provider-returned artifact URLs must still satisfy the existing HTTP/HTTPS and downgrade rules, must contain no credentials or fragment, and must resolve to the provider origin or an explicitly configured artifact origin. | ||
| 5. A same-origin `artifactToken` may be translated into the trusted Clearfolio viewer route. A token on an approved cross-origin artifact URL remains on that returned URL; ScopeWeave never transplants it into the provider-origin viewer. | ||
| 6. Exact origin comparison includes the effective port. Approving `https://cdn.example:8443` does not approve `https://cdn.example`. | ||
|
|
||
| ## Operator action | ||
|
|
||
| If Clearfolio returns artifacts from a separate reviewed CDN or object-storage service, configure only that service origin, for example: | ||
|
|
||
| ```text | ||
| CLEARFOLIO_ARTIFACT_ORIGINS=https://artifacts.example.com,https://archive.example.com:8443 | ||
| ``` | ||
|
|
||
| Do not place signed paths, object keys, tokens, credentials, query strings, or fragments in this setting. If no cross-origin artifact service is required, leave the variable unset; the provider origin remains the least-privilege default. | ||
|
|
||
| ## Verification contract | ||
|
|
||
| `tests/unit/clearfolio-artifact-origin.test.mjs` exercises the production adapter with real `URL` parsing and a bounded mocked provider response. It proves: | ||
|
|
||
| - cross-origin HTTPS artifacts fail by default; | ||
| - same-origin relative artifacts continue to resolve against the provider; | ||
| - an explicitly approved origin succeeds only for the same scheme/host/port identity; | ||
| - approved cross-origin `artifactToken` values remain on the approved origin; | ||
| - credentials and fragments are rejected on provider and approved origins; | ||
| - malformed, HTTP, credentialed, path-, query-, fragment-bearing, and empty-entry configuration fails before any provider transport. | ||
|
|
||
| The test is registered in both the normal unit suite and the production coverage cases so changes to this boundary cannot silently bypass repository coverage evidence. | ||
|
|
||
| ## Failure and rollback | ||
|
|
||
| Configuration failure is fail-closed and limited to the artifact-link capability; it does not broaden trust or silently fall back to arbitrary URLs. Rollback removes the additional-origin feature and returns to provider-origin-only artifact redirects. Do not roll back by permitting arbitrary HTTPS destinations or by moving cross-origin tokens into a trusted same-origin viewer URL. | ||
|
|
||
| ## References | ||
|
|
||
| Barth, A. (2011). *The web origin concept* (RFC 6454). Internet Engineering Task Force. https://doi.org/10.17487/RFC6454 | ||
|
|
||
| Open Worldwide Application Security Project. (n.d.). *Server side request forgery prevention cheat sheet*. OWASP Cheat Sheet Series. Retrieved August 15, 2026, from https://cheatsheetseries.owasp.org/cheatsheets/Server_Side_Request_Forgery_Prevention_Cheat_Sheet.html | ||
|
|
||
| WHATWG. (2026). *URL standard*. https://url.spec.whatwg.org/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| import test from 'node:test'; | ||
| import assert from 'node:assert/strict'; | ||
|
|
||
| const HMAC_SECRET = 'clearfolio-shared-secret-32-bytes!!'; | ||
| const originalFetch = globalThis.fetch; | ||
| let importSequence = 0; | ||
| let fetchCalls = 0; | ||
| let artifactPayload = { artifactUrl: 'https://clearfolio.example/file.pdf' }; | ||
|
|
||
| globalThis.fetch = async () => { | ||
| fetchCalls += 1; | ||
| return new Response(JSON.stringify(artifactPayload), { | ||
| status: 200, | ||
| headers: { 'content-type': 'application/json; charset=utf-8' }, | ||
| }); | ||
| }; | ||
|
|
||
| async function loadAdapter(artifactOrigins) { | ||
| process.env.CLEARFOLIO_URL = 'https://clearfolio.example'; | ||
| process.env.CLEARFOLIO_HMAC_SECRET = HMAC_SECRET; | ||
| delete process.env.SCOPEWEAVE_DEV; | ||
| if (artifactOrigins === undefined) delete process.env.CLEARFOLIO_ARTIFACT_ORIGINS; | ||
| else process.env.CLEARFOLIO_ARTIFACT_ORIGINS = artifactOrigins; | ||
| importSequence += 1; | ||
| return import(`../../server/clearfolio.mjs?artifact-origin-policy=${importSequence}`); | ||
| } | ||
|
|
||
| async function resolveArtifact(link, artifactOrigins) { | ||
| artifactPayload = { artifactUrl: link }; | ||
| const { artifactUrl } = await loadAdapter(artifactOrigins); | ||
| return artifactUrl(4, 5, 'job-1'); | ||
| } | ||
|
|
||
| test.after(() => { | ||
| globalThis.fetch = originalFetch; | ||
| delete process.env.CLEARFOLIO_URL; | ||
| delete process.env.CLEARFOLIO_HMAC_SECRET; | ||
| delete process.env.CLEARFOLIO_ARTIFACT_ORIGINS; | ||
| delete process.env.SCOPEWEAVE_DEV; | ||
| }); | ||
|
|
||
| test('artifact URLs default to the configured Clearfolio origin', async () => { | ||
| await assert.rejects( | ||
| () => resolveArtifact('https://cdn.example/file.pdf', undefined), | ||
| /clearfolio artifact-link response invalid/, | ||
| ); | ||
| assert.equal( | ||
| await resolveArtifact('/signed/file.pdf', undefined), | ||
| 'https://clearfolio.example/signed/file.pdf', | ||
| ); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Default-deny currently covers only an absolute Add those three cases next to this test. The successor on |
||
|
|
||
| test('explicit HTTPS artifact origins are exact scheme-host-port allowlist entries', async () => { | ||
| assert.equal( | ||
| await resolveArtifact('https://cdn.example/file.pdf', ' https://cdn.example '), | ||
| 'https://cdn.example/file.pdf', | ||
| ); | ||
| assert.equal( | ||
| await resolveArtifact( | ||
| 'https://cdn.example/file.pdf?artifactToken=remote%20token', | ||
| 'https://cdn.example', | ||
| ), | ||
| 'https://cdn.example/file.pdf?artifactToken=remote%20token', | ||
| 'an allowlisted cross-origin token remains bound to the returned origin', | ||
| ); | ||
| await assert.rejects( | ||
| () => resolveArtifact('https://cdn.example/file.pdf', 'https://cdn.example:8443'), | ||
| /clearfolio artifact-link response invalid/, | ||
| 'an allowlist entry with a different port is a different origin', | ||
| ); | ||
| }); | ||
|
|
||
| test('artifact links reject credentials and fragments even on trusted origins', async () => { | ||
| for (const link of [ | ||
| 'https://user:password@clearfolio.example/file.pdf', | ||
| 'https://clearfolio.example/file.pdf#secret-fragment', | ||
| 'https://user:password@cdn.example/file.pdf', | ||
| 'https://cdn.example/file.pdf#secret-fragment', | ||
| ]) { | ||
| await assert.rejects( | ||
| () => resolveArtifact(link, 'https://cdn.example'), | ||
| /clearfolio artifact-link response invalid/, | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test('artifact origin configuration fails closed before provider transport', async () => { | ||
| const invalidConfigurations = [ | ||
| 'http://cdn.example', | ||
| 'https://user:password@cdn.example', | ||
| 'https://cdn.example/path', | ||
| 'https://cdn.example?query=1', | ||
| 'https://cdn.example#fragment', | ||
| 'not a URL', | ||
| 'https://cdn.example,', | ||
| ]; | ||
|
|
||
| for (const artifactOrigins of invalidConfigurations) { | ||
| const before = fetchCalls; | ||
| artifactPayload = { artifactUrl: 'https://cdn.example/file.pdf' }; | ||
| const { artifactUrl } = await loadAdapter(artifactOrigins); | ||
| await assert.rejects( | ||
| () => artifactUrl(4, 5, 'job-1'), | ||
| (error) => { | ||
| assert.equal(error.code, 'clearfolio_artifact_origins_invalid'); | ||
| return true; | ||
| }, | ||
| ); | ||
| assert.equal(fetchCalls, before, `invalid allowlist ${artifactOrigins} never reaches provider transport`); | ||
| } | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After WHATWG normalization this accepts
https://cdn.example#,https://:@cdn.example,https://cdn.example/foo/.., andhttps://cdn.example:443even though the doctoring record says “origin only.” Compare the trimmed entry tourl.originor${url.origin}/before adding it to the set, and add empty/whitespace env values to the fail-closed-before-transport list. Platforms that inject an emptyCLEARFOLIO_ARTIFACT_ORIGINSalready fail closed; the suite should prove it.