@gen3/workspaces 0.12.71 → 0.12.72

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.
@@ -1,6 +1,8 @@
1
1
  import { GEN3_API, GEN3_WORKSPACE_API, GEN3_FENCE_SERVICE, fetchFence, GEN3_COMMONS_NAME } from '@gen3/core';
2
2
  import nodePath from 'path';
3
3
  import fs from 'fs';
4
+ import http from 'http';
5
+ import https from 'https';
4
6
  import { getCookie } from 'cookies-next';
5
7
  import { GEN3_FENCE_API } from '@gen3/core/server';
6
8
  import { getAccessToken } from '@gen3/frontend/server';
@@ -11,6 +13,134 @@ process.env.GEN3_KERNEL_API || `${GEN3_API}/lw-workspace/proxy/jeg-panel/api`;
11
13
  process.env.JEG_GATEWAY_API || `${GEN3_API}/lw-workspace/proxy/jeg-panel/api`;
12
14
  const JEG_SERVICE_API = 'http://jupyter-enterprise-gateway.jupyter-pods.svc.cluster.local:8888';
13
15
 
16
+ /**
17
+ * Shared HTTP proxy utility for server-side proxy handlers.
18
+ * Streams request body through and forwards response status + body.
19
+ *
20
+ * Also exports two token helpers used by the proxy factories:
21
+ * - extractTokenFallback: minimal cookie/header extraction used when the host
22
+ * app has not injected its own getToken (standalone / test use).
23
+ * - decodeJwtClaims: base64url decode of the JWT payload for REMOTE_USER header.
24
+ * Crypto verification is Ambassador's job — we only need the sub/username claim.
25
+ */ /**
26
+ * Minimal JWT extraction fallback — used only when the host app does not inject
27
+ * its own getToken. Prefers Authorization: Bearer, then access_token cookie.
28
+ * In gen3-vectis production this is never reached — getAccessToken is always wired.
29
+ */ function extractTokenFallback(req) {
30
+ const auth = req.headers['authorization'];
31
+ if (typeof auth === 'string' && auth.startsWith('Bearer ')) {
32
+ return auth.slice(7).trim() || null;
33
+ }
34
+ const cookie = req.headers['cookie'] ?? '';
35
+ const m = cookie.match(/(?:^|;\s*)access_token=([^;]+)/);
36
+ return m ? decodeURIComponent(m[1]).trim() || null : null;
37
+ }
38
+ /**
39
+ * Decode JWT payload claims without cryptographic verification.
40
+ * Ambassador has already verified the RS256 signature — we just need the sub claim
41
+ * to set the REMOTE_USER header for per-user isolation enforcement downstream.
42
+ */ // Maximum JWT string length accepted before base64 decoding.
43
+ // GA4GH RAS Passport JWTs embed multiple signed Visa JWTs as claims, so the
44
+ // outer passport can easily exceed 8 KiB (50 visas × ~2 KB each ≈ 136 KB encoded).
45
+ // 512 KiB covers the most complex real-world passports while still guarding against
46
+ // crafted JWTs designed to exhaust server memory. Ambassador has already verified
47
+ // the RS256 signature — this cap only defends the local base64/JSON decode step.
48
+ const MAX_JWT_LENGTH = 524288; // 512 KiB
49
+ function decodeJwtClaims(jwt) {
50
+ if (jwt.length > MAX_JWT_LENGTH) return null;
51
+ try {
52
+ const parts = jwt.split('.');
53
+ if (parts.length < 2) return null;
54
+ const padded = parts[1].replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(parts[1].length / 4) * 4, '=');
55
+ return JSON.parse(Buffer.from(padded, 'base64').toString('utf8'));
56
+ } catch {
57
+ return null;
58
+ }
59
+ }
60
+ async function createProxyRequest({ req, res, targetUrl, method, extraHeaders = {}, forwardBody = false }) {
61
+ const headers = {
62
+ ...extraHeaders
63
+ };
64
+ if (forwardBody && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
65
+ headers['Content-Type'] ??= 'application/json';
66
+ }
67
+ let body;
68
+ if (forwardBody && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
69
+ let buf;
70
+ try {
71
+ buf = await bufferBody(req);
72
+ } catch (err) {
73
+ const statusCode = err.statusCode ?? 400;
74
+ const msg = err instanceof Error ? err.message : 'Bad request';
75
+ res.status(statusCode).json({
76
+ error: msg
77
+ });
78
+ return;
79
+ }
80
+ // Node.js Buffer is a Uint8Array subclass; cast to silence DOM BodyInit mismatch
81
+ body = buf;
82
+ if (buf.length > 0) {
83
+ headers['Content-Length'] = String(buf.length);
84
+ }
85
+ }
86
+ let upRes;
87
+ try {
88
+ upRes = await fetch(targetUrl, {
89
+ method,
90
+ headers,
91
+ body,
92
+ signal: AbortSignal.timeout(30_000)
93
+ });
94
+ } catch (err) {
95
+ const msg = err instanceof Error ? err.message : String(err);
96
+ res.status(502).json({
97
+ error: `Upstream unreachable: ${msg}`
98
+ });
99
+ return;
100
+ }
101
+ // Forward status and content-type
102
+ const contentType = upRes.headers.get('content-type') || 'application/json';
103
+ res.setHeader('Content-Type', contentType);
104
+ if (upRes.status === 204 || upRes.status === 304) {
105
+ res.status(upRes.status).end();
106
+ return;
107
+ }
108
+ // Do not forward raw upstream error bodies — they may contain stack traces or
109
+ // internal hostnames. Log them server-side (CloudWatch) and return a generic message.
110
+ if (upRes.status >= 500) {
111
+ const errBody = await upRes.text().catch(()=>'');
112
+ console.error('[proxy] upstream %d from %s: %s', upRes.status, targetUrl, errBody);
113
+ res.status(502).json({
114
+ error: 'Upstream service error.'
115
+ });
116
+ return;
117
+ }
118
+ const responseBody = await upRes.text();
119
+ res.status(upRes.status).send(responseBody);
120
+ }
121
+ // Maximum request body size accepted by the proxy (1 MiB).
122
+ // Prevents memory exhaustion from oversized payloads before any business logic runs.
123
+ const MAX_BODY_BYTES = 1 * 1024 * 1024;
124
+ function bufferBody(req) {
125
+ return new Promise((resolve, reject)=>{
126
+ const chunks = [];
127
+ let total = 0;
128
+ req.on('data', (chunk)=>{
129
+ total += chunk.length;
130
+ if (total > MAX_BODY_BYTES) {
131
+ req.destroy();
132
+ reject(Object.assign(new Error('Request body too large'), {
133
+ statusCode: 413
134
+ }));
135
+ return;
136
+ }
137
+ chunks.push(chunk);
138
+ });
139
+ req.on('end', ()=>resolve(Buffer.concat(chunks)));
140
+ req.on('error', reject);
141
+ });
142
+ }
143
+
14
144
  const MAX_FILE_SIZE_BYTES = 67108864; // 64 * 1024 * 1024
15
145
  // ---------- constants ----------
16
146
  const ALLOWED_TIERS = new Set([
@@ -36,6 +166,14 @@ const MIME_TYPES = {
36
166
  '.ipynb': 'application/json; charset=utf-8'
37
167
  };
38
168
  const DEFAULT_FULL_THEMES_URL = '/workspace-api/workspace-assets/remote/build/themes';
169
+ /**
170
+ * First path segments that identify server extension REST APIs (not static assets).
171
+ * Requests whose first segment matches one of these are proxied to
172
+ * `serverExtensionProxyPath` when that option is configured for the remote tier.
173
+ */ const SERVER_EXTENSION_PREFIXES = new Set([
174
+ 'module',
175
+ 'server-proxy'
176
+ ]);
39
177
  const REMOTE_DISABLED_EXTENSIONS = [
40
178
  '@jupyterlite/services-extension:config-section-manager',
41
179
  '@jupyterlite/services-extension:default-drive',
@@ -176,6 +314,98 @@ function injectBranding(html, branding) {
176
314
  });
177
315
  return replaceTitle(result, branding);
178
316
  }
317
+ // ---------- server-extension proxy ----------
318
+ function readBody$2(req) {
319
+ return new Promise((resolve, reject)=>{
320
+ const chunks = [];
321
+ req.on('data', (chunk)=>chunks.push(chunk));
322
+ req.on('end', ()=>resolve(Buffer.concat(chunks)));
323
+ req.on('error', reject);
324
+ });
325
+ }
326
+ async function proxyServerExtension(req, res, targetUrl, getToken) {
327
+ const forwardHeaders = {};
328
+ for (const [key, value] of Object.entries(req.headers)){
329
+ if (!value) continue;
330
+ const lower = key.toLowerCase();
331
+ if (lower === 'host' || lower === 'content-length' || lower === 'transfer-encoding') continue;
332
+ forwardHeaders[key] = Array.isArray(value) ? value.join(',') : value;
333
+ }
334
+ // Inject identity headers so workspace-proxy auth passes.
335
+ // Direct server-side calls bypass nginx (which normally injects X-Gen3-User-ID /
336
+ // REMOTE_USER from the validated access_token), so we must add them explicitly.
337
+ // only add in production mode
338
+ // TODO: refactor this to handle development mode better
339
+ if (process.env.NODE_ENV === 'production') {
340
+ const jwt = getToken ? getToken(req) : extractTokenFallback(req);
341
+ if (jwt) {
342
+ const claims = decodeJwtClaims(jwt);
343
+ const rawUsername = claims?.sub ?? claims?.preferred_username ?? claims?.username;
344
+ if (typeof rawUsername === 'string') {
345
+ const safeUsername = rawUsername.replace(/[\r\n]/g, '');
346
+ const safeJwt = jwt.replace(/[\r\n]/g, '');
347
+ forwardHeaders['Authorization'] = `Bearer ${safeJwt}`;
348
+ forwardHeaders['REMOTE_USER'] = safeUsername;
349
+ forwardHeaders['X-Gen3-User-ID'] = safeUsername;
350
+ }
351
+ }
352
+ }
353
+ const method = (req.method || 'GET').toUpperCase();
354
+ let body;
355
+ if (![
356
+ 'GET',
357
+ 'HEAD'
358
+ ].includes(method)) {
359
+ try {
360
+ body = await readBody$2(req);
361
+ } catch {
362
+ res.status(500).end('Failed to read request body');
363
+ return;
364
+ }
365
+ if (body?.length) {
366
+ forwardHeaders['content-length'] = String(body.length);
367
+ }
368
+ }
369
+ console.log('[workspace-assets] proxying extension API to', targetUrl);
370
+ await new Promise((resolve)=>{
371
+ const parsed = new URL(targetUrl);
372
+ const transport = parsed.protocol === 'https:' ? https : http;
373
+ const reqOptions = {
374
+ method,
375
+ hostname: parsed.hostname,
376
+ port: parsed.port || (parsed.protocol === 'https:' ? 443 : 80),
377
+ path: parsed.pathname + parsed.search,
378
+ headers: forwardHeaders
379
+ };
380
+ const proxyReq = transport.request(reqOptions, (proxyRes)=>{
381
+ res.status(proxyRes.statusCode ?? 502);
382
+ const skip = new Set([
383
+ 'transfer-encoding',
384
+ 'content-length'
385
+ ]);
386
+ for (const [key, value] of Object.entries(proxyRes.headers)){
387
+ if (!skip.has(key.toLowerCase()) && value !== undefined) {
388
+ res.setHeader(key, value);
389
+ }
390
+ }
391
+ proxyRes.pipe(res, {
392
+ end: true
393
+ });
394
+ proxyRes.on('end', resolve);
395
+ });
396
+ proxyReq.on('error', (err)=>{
397
+ console.error('[workspace-assets] extension proxy failed for', targetUrl, err.message);
398
+ if (!res.headersSent) {
399
+ res.status(502).end('Extension proxy unreachable');
400
+ }
401
+ resolve();
402
+ });
403
+ if (body?.length) {
404
+ proxyReq.write(body);
405
+ }
406
+ proxyReq.end();
407
+ });
408
+ }
179
409
  // ---------- factory ----------
180
410
  function createWorkspaceAssetsHandler(options) {
181
411
  const gatewayBaseUrl = options?.gatewayBaseUrl ?? process.env.JUPYTER_GATEWAY_BASE_URL ?? '/api/workspace/gateway/';
@@ -191,6 +421,9 @@ function createWorkspaceAssetsHandler(options) {
191
421
  faviconUrl: options.faviconUrl,
192
422
  pageTitle: options.pageTitle
193
423
  } : undefined;
424
+ const serverExtensionProxyPath = options?.serverExtensionProxyPath;
425
+ const serverExtensionProxyBaseUrl = options?.serverExtensionProxyBaseUrl;
426
+ const getToken = options?.getToken;
194
427
  const remoteInjectionConfig = {
195
428
  gatewayBaseUrl,
196
429
  disabledExtensions,
@@ -249,6 +482,21 @@ function createWorkspaceAssetsHandler(options) {
249
482
  }
250
483
  safeSegments.push(nodePath.basename(seg));
251
484
  }
485
+ // Proxy server extension API calls (e.g. jupyterlmod /module/*, jupyter-server-proxy
486
+ // /server-proxy/*) to the configured upstream rather than attempting a static file
487
+ // lookup, which would always 404.
488
+ if (tier === 'remote' && serverExtensionProxyPath && safeSegments.length > 0 && SERVER_EXTENSION_PREFIXES.has(safeSegments[0])) {
489
+ const extensionPath = safeSegments.join('/');
490
+ const rawQuery = req.url?.split('?')[1];
491
+ const qs = rawQuery ? `?${rawQuery}` : '';
492
+ const proxyBase = serverExtensionProxyBaseUrl ?? (()=>{
493
+ const { proto, host } = resolveOrigin(req);
494
+ return `${proto}://${host}`;
495
+ })();
496
+ const targetUrl = `${proxyBase}${serverExtensionProxyPath}/${extensionPath}${qs}`;
497
+ console.log('[workspace-assets] proxying extension API to', targetUrl);
498
+ return proxyServerExtension(req, res, targetUrl, getToken);
499
+ }
252
500
  const allowedFiles = allowedFilesByTier[tier];
253
501
  const candidatePath = nodePath.resolve(tierRoot, ...safeSegments);
254
502
  const candidateIndex = nodePath.join(candidatePath, 'index.html');
@@ -402,134 +650,6 @@ function withJupyterWorkspaces(nextConfig, options) {
402
650
  };
403
651
  }
404
652
 
405
- /**
406
- * Shared HTTP proxy utility for server-side proxy handlers.
407
- * Streams request body through and forwards response status + body.
408
- *
409
- * Also exports two token helpers used by the proxy factories:
410
- * - extractTokenFallback: minimal cookie/header extraction used when the host
411
- * app has not injected its own getToken (standalone / test use).
412
- * - decodeJwtClaims: base64url decode of the JWT payload for REMOTE_USER header.
413
- * Crypto verification is Ambassador's job — we only need the sub/username claim.
414
- */ /**
415
- * Minimal JWT extraction fallback — used only when the host app does not inject
416
- * its own getToken. Prefers Authorization: Bearer, then access_token cookie.
417
- * In gen3-vectis production this is never reached — getAccessToken is always wired.
418
- */ function extractTokenFallback(req) {
419
- const auth = req.headers['authorization'];
420
- if (typeof auth === 'string' && auth.startsWith('Bearer ')) {
421
- return auth.slice(7).trim() || null;
422
- }
423
- const cookie = req.headers['cookie'] ?? '';
424
- const m = cookie.match(/(?:^|;\s*)access_token=([^;]+)/);
425
- return m ? decodeURIComponent(m[1]).trim() || null : null;
426
- }
427
- /**
428
- * Decode JWT payload claims without cryptographic verification.
429
- * Ambassador has already verified the RS256 signature — we just need the sub claim
430
- * to set the REMOTE_USER header for per-user isolation enforcement downstream.
431
- */ // Maximum JWT string length accepted before base64 decoding.
432
- // GA4GH RAS Passport JWTs embed multiple signed Visa JWTs as claims, so the
433
- // outer passport can easily exceed 8 KiB (50 visas × ~2 KB each ≈ 136 KB encoded).
434
- // 512 KiB covers the most complex real-world passports while still guarding against
435
- // crafted JWTs designed to exhaust server memory. Ambassador has already verified
436
- // the RS256 signature — this cap only defends the local base64/JSON decode step.
437
- const MAX_JWT_LENGTH = 524288; // 512 KiB
438
- function decodeJwtClaims(jwt) {
439
- if (jwt.length > MAX_JWT_LENGTH) return null;
440
- try {
441
- const parts = jwt.split('.');
442
- if (parts.length < 2) return null;
443
- const padded = parts[1].replace(/-/g, '+').replace(/_/g, '/').padEnd(Math.ceil(parts[1].length / 4) * 4, '=');
444
- return JSON.parse(Buffer.from(padded, 'base64').toString('utf8'));
445
- } catch {
446
- return null;
447
- }
448
- }
449
- async function createProxyRequest({ req, res, targetUrl, method, extraHeaders = {}, forwardBody = false }) {
450
- const headers = {
451
- ...extraHeaders
452
- };
453
- if (forwardBody && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
454
- headers['Content-Type'] ??= 'application/json';
455
- }
456
- let body;
457
- if (forwardBody && (method === 'POST' || method === 'PUT' || method === 'PATCH')) {
458
- let buf;
459
- try {
460
- buf = await bufferBody(req);
461
- } catch (err) {
462
- const statusCode = err.statusCode ?? 400;
463
- const msg = err instanceof Error ? err.message : 'Bad request';
464
- res.status(statusCode).json({
465
- error: msg
466
- });
467
- return;
468
- }
469
- // Node.js Buffer is a Uint8Array subclass; cast to silence DOM BodyInit mismatch
470
- body = buf;
471
- if (buf.length > 0) {
472
- headers['Content-Length'] = String(buf.length);
473
- }
474
- }
475
- let upRes;
476
- try {
477
- upRes = await fetch(targetUrl, {
478
- method,
479
- headers,
480
- body,
481
- signal: AbortSignal.timeout(30_000)
482
- });
483
- } catch (err) {
484
- const msg = err instanceof Error ? err.message : String(err);
485
- res.status(502).json({
486
- error: `Upstream unreachable: ${msg}`
487
- });
488
- return;
489
- }
490
- // Forward status and content-type
491
- const contentType = upRes.headers.get('content-type') || 'application/json';
492
- res.setHeader('Content-Type', contentType);
493
- if (upRes.status === 204 || upRes.status === 304) {
494
- res.status(upRes.status).end();
495
- return;
496
- }
497
- // Do not forward raw upstream error bodies — they may contain stack traces or
498
- // internal hostnames. Log them server-side (CloudWatch) and return a generic message.
499
- if (upRes.status >= 500) {
500
- const errBody = await upRes.text().catch(()=>'');
501
- console.error('[proxy] upstream %d from %s: %s', upRes.status, targetUrl, errBody);
502
- res.status(502).json({
503
- error: 'Upstream service error.'
504
- });
505
- return;
506
- }
507
- const responseBody = await upRes.text();
508
- res.status(upRes.status).send(responseBody);
509
- }
510
- // Maximum request body size accepted by the proxy (1 MiB).
511
- // Prevents memory exhaustion from oversized payloads before any business logic runs.
512
- const MAX_BODY_BYTES = 1 * 1024 * 1024;
513
- function bufferBody(req) {
514
- return new Promise((resolve, reject)=>{
515
- const chunks = [];
516
- let total = 0;
517
- req.on('data', (chunk)=>{
518
- total += chunk.length;
519
- if (total > MAX_BODY_BYTES) {
520
- req.destroy();
521
- reject(Object.assign(new Error('Request body too large'), {
522
- statusCode: 413
523
- }));
524
- return;
525
- }
526
- chunks.push(chunk);
527
- });
528
- req.on('end', ()=>resolve(Buffer.concat(chunks)));
529
- req.on('error', reject);
530
- });
531
- }
532
-
533
653
  /* ------------------------------------------------------------------ */ /* Default billing sink — structured CloudWatch-compatible JSON log */ /* ------------------------------------------------------------------ */ function defaultBillingSink(event) {
534
654
  // CloudWatch Logs Insights picks up structured JSON automatically
535
655
  process.stdout.write(JSON.stringify({
@@ -1057,7 +1177,16 @@ const DEFAULT_OPTIONS = {
1057
1177
  // launches/channels to the container; GPU kernel launches are gated (403)
1058
1178
  // with a message to use the Kernel Panel.
1059
1179
  remoteKernelsPath: '/lw-workspace/proxy/jeg-proxy',
1060
- // The remote tier attaches to a real kernel gateway, so JupyterLite's
1180
+ // Proxy server extension REST calls (jupyterlmod /module/*, jupyter-server-proxy
1181
+ // /server-proxy/*) to jeg-proxy. These calls originate from JupyterLab extensions
1182
+ // in the remote build and resolve relative to the JupyterLite base URL, which points
1183
+ // at this static-file handler — without a proxy they always 404.
1184
+ serverExtensionProxyPath: '/lw-workspace/proxy',
1185
+ // Optional internal k8s service base URL for the extension proxy. When set,
1186
+ // proxy requests target this address directly instead of routing through the
1187
+ // ingress (avoiding self-signed-cert issues). Readable from the JSON config
1188
+ // file or the WORKSPACE_EXTENSION_PROXY_BASE_URL env var.
1189
+ serverExtensionProxyBaseUrl: 'http://workspace-proxy-service:8080',
1061
1190
  // in-browser Pyodide kernel must not be offered alongside it.
1062
1191
  additionalDisabledExtensions: [
1063
1192
  '@jupyterlite/pyodide-kernel-extension:kernel'
@@ -1071,7 +1200,9 @@ const STRING_OPTIONS = [
1071
1200
  'faviconUrl',
1072
1201
  'pageTitle',
1073
1202
  'remoteKernelsPath',
1074
- 'fullThemesUrl'
1203
+ 'fullThemesUrl',
1204
+ 'serverExtensionProxyPath',
1205
+ 'serverExtensionProxyBaseUrl'
1075
1206
  ];
1076
1207
  function configFilePath() {
1077
1208
  return nodePath.resolve(process.cwd(), CONFIG_ROOT, GEN3_COMMONS_NAME, CONFIG_FILENAME);