@analogjs/vite-plugin-nitro 2.6.4-beta.2 → 2.6.4-beta.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@analogjs/vite-plugin-nitro",
3
- "version": "2.6.4-beta.2",
3
+ "version": "2.6.4-beta.3",
4
4
  "description": "A Vite plugin for adding a nitro API server",
5
5
  "type": "module",
6
6
  "author": "Brandon Roberts <robertsbt@gmail.com>",
@@ -1,3 +1,3 @@
1
1
  export declare const ssrRenderer = "\nimport { eventHandler, getResponseHeader } from 'h3';\n// @ts-ignore\nimport renderer from '#analog/ssr';\n// @ts-ignore\nimport template from '#analog/index';\n\nexport default eventHandler(async (event) => {\n const noSSR = getResponseHeader(event, 'x-analog-no-ssr');\n\n if (noSSR === 'true') {\n return template;\n }\n\n const html = await renderer(event.node.req.url, template, {\n req: event.node.req,\n res: event.node.res,\n });\n\n return html;\n});";
2
2
  export declare const clientRenderer = "\nimport { eventHandler } from 'h3';\n\n// @ts-ignore\nimport template from '#analog/index';\n\nexport default eventHandler(async () => {\n return template;\n});\n";
3
- export declare const apiMiddleware = "\nimport { eventHandler, proxyRequest } from 'h3';\nimport { useRuntimeConfig } from '#imports';\n\nexport default eventHandler(async (event) => {\n const prefix = useRuntimeConfig().prefix;\n const apiPrefix = `${prefix}/${useRuntimeConfig().apiPrefix}`;\n\n if (event.node.req.url?.startsWith(apiPrefix)) {\n const reqUrl = event.node.req.url?.replace(apiPrefix, '');\n\n if (\n event.node.req.method === 'GET' &&\n // in the case of XML routes, we want to proxy the request so that nitro gets the correct headers\n // and can render the XML correctly as a static asset\n !event.node.req.url?.endsWith('.xml')\n ) {\n return $fetch(reqUrl, { headers: event.node.req.headers });\n }\n\n return proxyRequest(event, reqUrl, {\n // @ts-ignore\n fetch: $fetch.native,\n });\n }\n});";
3
+ export declare const apiMiddleware = "\nimport { createError, eventHandler, proxyRequest } from 'h3';\nimport { useRuntimeConfig } from '#imports';\n\nexport default eventHandler(async (event) => {\n const prefix = useRuntimeConfig().prefix;\n const apiPrefix = `${prefix}/${useRuntimeConfig().apiPrefix}`;\n\n const url = event.node.req.url || '';\n\n // only match the prefix on a path boundary, otherwise a URL such as\n // '/apihttp://internal-host' would pass startsWith() and be forwarded verbatim\n if (\n url === apiPrefix ||\n url.startsWith(`${apiPrefix}/`) ||\n url.startsWith(`${apiPrefix}?`)\n ) {\n let reqUrl = url.slice(apiPrefix.length);\n if (reqUrl === '' || reqUrl.startsWith('?')) {\n reqUrl = `/${reqUrl}`;\n }\n\n // reject absolute and protocol-relative targets to prevent SSRF\n if (!reqUrl.startsWith('/') || reqUrl.startsWith('//')) {\n throw createError({ statusCode: 400, statusMessage: 'Invalid API route' });\n }\n\n if (\n event.node.req.method === 'GET' &&\n // in the case of XML routes, we want to proxy the request so that nitro gets the correct headers\n // and can render the XML correctly as a static asset\n !event.node.req.url?.endsWith('.xml')\n ) {\n return $fetch(reqUrl, { headers: event.node.req.headers });\n }\n\n return proxyRequest(event, reqUrl, {\n // @ts-ignore\n fetch: $fetch.native,\n });\n }\n});";
@@ -30,15 +30,31 @@ export default eventHandler(async () => {
30
30
  });
31
31
  `;
32
32
  export const apiMiddleware = `
33
- import { eventHandler, proxyRequest } from 'h3';
33
+ import { createError, eventHandler, proxyRequest } from 'h3';
34
34
  import { useRuntimeConfig } from '#imports';
35
35
 
36
36
  export default eventHandler(async (event) => {
37
37
  const prefix = useRuntimeConfig().prefix;
38
38
  const apiPrefix = \`\${prefix}/\${useRuntimeConfig().apiPrefix}\`;
39
39
 
40
- if (event.node.req.url?.startsWith(apiPrefix)) {
41
- const reqUrl = event.node.req.url?.replace(apiPrefix, '');
40
+ const url = event.node.req.url || '';
41
+
42
+ // only match the prefix on a path boundary, otherwise a URL such as
43
+ // '/apihttp://internal-host' would pass startsWith() and be forwarded verbatim
44
+ if (
45
+ url === apiPrefix ||
46
+ url.startsWith(\`\${apiPrefix}/\`) ||
47
+ url.startsWith(\`\${apiPrefix}?\`)
48
+ ) {
49
+ let reqUrl = url.slice(apiPrefix.length);
50
+ if (reqUrl === '' || reqUrl.startsWith('?')) {
51
+ reqUrl = \`/\${reqUrl}\`;
52
+ }
53
+
54
+ // reject absolute and protocol-relative targets to prevent SSRF
55
+ if (!reqUrl.startsWith('/') || reqUrl.startsWith('//')) {
56
+ throw createError({ statusCode: 400, statusMessage: 'Invalid API route' });
57
+ }
42
58
 
43
59
  if (
44
60
  event.node.req.method === 'GET' &&
@@ -1 +1 @@
1
- {"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/renderers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;IAoBvB,CAAC;AAEL,MAAM,CAAC,MAAM,cAAc,GAAG;;;;;;;;;CAS7B,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;IAyBzB,CAAC"}
1
+ {"version":3,"file":"renderers.js","sourceRoot":"","sources":["../../../../../../packages/vite-plugin-nitro/src/lib/utils/renderers.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,WAAW,GAAG;;;;;;;;;;;;;;;;;;;;IAoBvB,CAAC;AAEL,MAAM,CAAC,MAAM,cAAc,GAAG;;;;;;;;;CAS7B,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;IAyCzB,CAAC"}