@guren/plugin-cloudflare 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -341,8 +341,13 @@ var R2Driver = class {
341
341
  if (!this.presign) throw new Error("R2Driver.temporaryUrl() cannot sign URLs through the R2 binding. Either configure \"presign: { accountId, bucket, accessKeyId, secretAccessKey }\" (an R2 API token, used with the S3-compatible endpoint) or serve private files through a signed app route.");
342
342
  const expiresIn = Math.floor((expiration.getTime() - Date.now()) / 1e3);
343
343
  if (expiresIn > MAX_PRESIGN_SECONDS) throw new Error(`R2Driver.temporaryUrl(): R2 presigned URLs may be valid for at most 7 days (requested ${expiresIn}s).`);
344
+ const bucketUrl = `https://${this.presign.accountId}.r2.cloudflarestorage.com/${this.presign.bucket}`;
345
+ const url = `${bucketUrl}/${encodeKey(this.key(path))}`;
346
+ const signedPath = new URL(url).pathname;
347
+ const rootPath = trimTrailingSlashes(new URL(`${bucketUrl}/${encodeKey(this.prefix)}`).pathname);
348
+ if (signedPath !== rootPath && !signedPath.startsWith(`${rootPath}/`)) throw new Error(`R2Driver.temporaryUrl(): path escapes the storage root: "${path}"`);
344
349
  return presignGetUrl({
345
- url: `https://${this.presign.accountId}.r2.cloudflarestorage.com/${this.presign.bucket}/${encodeKey(this.key(path))}`,
350
+ url,
346
351
  accessKeyId: this.presign.accessKeyId,
347
352
  secretAccessKey: this.presign.secretAccessKey,
348
353
  region: "auto",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@guren/plugin-cloudflare",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
4
4
  "description": "Deploy a Guren application to Cloudflare Workers, with D1 as the database.",
5
5
  "keywords": [
6
6
  "guren",
@@ -39,6 +39,14 @@ import {
39
39
  */
40
40
  const LOGIN_PATH = '/login'
41
41
 
42
+ /**
43
+ * The guard the consenting user is read from: the session guard `auth.useModel()`
44
+ * registers. Never an unqualified `this.auth.id()`, which answers with an API
45
+ * token's user for a request carrying `Authorization: Bearer`, and a grant minted
46
+ * for a token outlives revoking it. Change this if your session guard has another name.
47
+ */
48
+ const SESSION_GUARD = 'web'
49
+
42
50
  /**
43
51
  * What a client that sent no `scope` is offered. RFC 6749 §3.3 requires a default
44
52
  * or a refusal, and the MCP clients measured (Claude's connector, MCP Inspector)
@@ -55,7 +63,7 @@ interface WorkerEnvWithProvider {
55
63
  export default class McpOAuthController extends Controller {
56
64
  /** GET /oauth/authorize — render the consent screen. */
57
65
  async show(): Promise<Response> {
58
- const userId = await this.auth.id()
66
+ const userId = await this.auth.guard(SESSION_GUARD).id()
59
67
  if (userId === null || userId === undefined) {
60
68
  return this.redirect(this.loginUrl())
61
69
  }
@@ -69,6 +77,8 @@ export default class McpOAuthController extends Controller {
69
77
 
70
78
  return this.view(McpOAuthConsentPage, {
71
79
  clientName: client?.clientName ?? parsed.clientId,
80
+ // From the request, never `client.redirectUris`: a client may register several.
81
+ redirectTarget: redirectTargetOf(parsed.redirectUri),
72
82
  query: new URL(this.ctx.req.url).searchParams.toString(),
73
83
  tools: this.offeredTools(parsed.scope),
74
84
  csrfToken: getCsrfToken(this.ctx),
@@ -77,7 +87,7 @@ export default class McpOAuthController extends Controller {
77
87
 
78
88
  /** POST /oauth/authorize — record the decision and hand back to the provider. */
79
89
  async approve(): Promise<Response> {
80
- const userId = await this.auth.id()
90
+ const userId = await this.auth.guard(SESSION_GUARD).id()
81
91
  if (userId === null || userId === undefined) {
82
92
  return this.redirect(this.loginUrl())
83
93
  }
@@ -222,6 +232,21 @@ export default class McpOAuthController extends Controller {
222
232
  }
223
233
  }
224
234
 
235
+ /**
236
+ * The redirect URI as `protocol//host`, or the scheme alone if it has no host,
237
+ * read by the `new URL` parse `completeAuthorization` redirects with, so userinfo
238
+ * like `https://claude.ai@attacker.example` shows the host the code really goes to.
239
+ * One that will not parse is shown as it came; the page escapes it.
240
+ */
241
+ function redirectTargetOf(redirectUri: string): string {
242
+ try {
243
+ const { protocol, host } = new URL(redirectUri)
244
+ return host ? `${protocol}//${host}` : protocol
245
+ } catch {
246
+ return redirectUri
247
+ }
248
+ }
249
+
225
250
  /** One value from a parsed body field that may legally hold several. */
226
251
  function single(value: unknown): string {
227
252
  return typeof value === 'string' ? value : ''
@@ -26,6 +26,8 @@ export const QUERY_FIELD = 'authorize_query'
26
26
  export interface McpOAuthConsentPageProps {
27
27
  /** The client's own registered name, or its id when it registered none. */
28
28
  clientName: string
29
+ /** Where approving sends the authorization code: this request's redirect URI, reduced to scheme and host. */
30
+ redirectTarget: string
29
31
  /** The authorize request's query string, handed back on submit. */
30
32
  query: string
31
33
  /** The tools this request may grant — already intersected with what it asked for. */
@@ -44,6 +46,8 @@ const STYLES = `
44
46
  .desc { display: block; margin: .25rem 0 0 1.75rem; opacity: .8; }
45
47
  .badge { border: 1px solid currentColor; border-radius: .5rem; font-size: .75rem; margin-left: .5rem; padding: 0 .4rem; }
46
48
  .empty { opacity: .8; }
49
+ .target { border: 1px solid rgba(128,128,128,.35); border-radius: .5rem; margin: 1rem 0; padding: .75rem; }
50
+ .host { display: block; font-family: ui-monospace, monospace; font-weight: 600; overflow-wrap: anywhere; }
47
51
  code { font-family: ui-monospace, monospace; font-size: .9em; }
48
52
  button { font: inherit; padding: .5rem 1rem; }
49
53
  `
@@ -94,8 +98,15 @@ const NothingToGrant: FC = () => (
94
98
  </>
95
99
  )
96
100
 
101
+ /**
102
+ * Registration at `/oauth/register` is open, so the name is whatever the caller
103
+ * chose and the redirect target is the only part of this page it cannot fake. The
104
+ * target stays text, never an `href`. An app that creates its own clients with
105
+ * `createClient()` and closes registration removes the label and its sentence by hand.
106
+ */
97
107
  export const McpOAuthConsentPage: FC<McpOAuthConsentPageProps> = ({
98
108
  clientName,
109
+ redirectTarget,
99
110
  query,
100
111
  tools,
101
112
  csrfToken,
@@ -109,8 +120,17 @@ export const McpOAuthConsentPage: FC<McpOAuthConsentPageProps> = ({
109
120
  </head>
110
121
  <body>
111
122
  <h1>
112
- <strong>{clientName}</strong> is asking to use this application's tools
123
+ A client calling itself <strong>{clientName}</strong>{' '}
124
+ <span class="badge">unverified</span> is asking to use this application's tools
113
125
  </h1>
126
+ <p>
127
+ This application has not verified that name, and anyone can register a client under any
128
+ name. Go by where the access goes, not by what the client calls itself.
129
+ </p>
130
+ <p class="target">
131
+ Approving hands access to whoever controls{' '}
132
+ <span class="host">{redirectTarget}</span>
133
+ </p>
114
134
  <p>
115
135
  Approving lets it act as you, through the tools you select. Uncheck anything you would
116
136
  rather it could not do.