Skip to content
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

feat: env leak detection #11203

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .changeset/forty-crabs-judge.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
'astro': patch
---

Adds an optional middleware for usage with `astro:env`

If you're using `astro:env`, you can now use a middleware to detect server envrionment variables leaks on the client:

```ts
// src/middleware.js

import { leakDetectionMiddleware } from 'astro/env/middleware'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should make this astro:env/middleware instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes things a bit more complicated. There's no codegen involved for the middleware code so I'm a bit reluctant to move it to a virtual import. We were also doing this for astro:env/setup and ended up moving it to astro/env/setup

import { sequence, defineMiddleware } from 'astro:middleware'

const userMiddleware = defineMiddleware((_, next) => {
return next()
})

export const onRequest = sequence(
// It's important to use use it first to be able to catch the returned response last
leakDetectionMiddleware(),
userMiddleware
);
```

An error will be thrown instead of rendering the page if a leak is detected.

You can pass 2 options:

1. `filterContentType`: filters what response content type should trigger the check. Defaults to the content type starting with `text/` or `application/json`
2. `excludeKeys`: by default, all server environment variables are checked. However, you may have variables whose value is really likely to end up on the client but not because it leaked (eg. `test`). In this case, you can exclude those keys.

```ts
import { leakDetectionMiddleware } from 'astro/env/middleware'

export const onRequest = leakDetectionMiddleware({
// Do not filter json response
filterContentType: (contentType) => contentType.startsWith('text/'),
excludeKeys: ['PORT']
})
```
3 changes: 2 additions & 1 deletion packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"default": "./dist/core/index.js"
},
"./env": "./env.d.ts",
"./env/runtime": "./dist/env/runtime.js",
"./env/runtime": "./dist/env/runtime/index.js",
"./env/middleware": "./dist/env/runtime/middleware.js",
"./env/setup": "./dist/env/setup.js",
"./types": "./types.d.ts",
"./client": "./client.d.ts",
Expand Down
3 changes: 1 addition & 2 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2138,7 +2138,6 @@ export interface AstroUserConfig {
* @name experimental.env.schema
* @kind h4
* @type {EnvSchema}
* @default `undefined`
* @version 4.10.0
* @description
*
Expand All @@ -2160,7 +2159,7 @@ export interface AstroUserConfig {
* })
* ```
*/
schema?: EnvSchema;
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
schema: EnvSchema;
};
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/astro/src/core/base-pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import type {
SSRManifest,
SSRResult,
} from '../@types/astro.js';
import { setGetEnv } from '../env/runtime.js';
import { setGetEnv } from '../env/runtime/index.js';
import { createI18nMiddleware } from '../i18n/middleware.js';
import { AstroError } from './errors/errors.js';
import { AstroErrorData } from './errors/index.js';
Expand Down
6 changes: 5 additions & 1 deletion packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ export const ASTRO_CONFIG_DEFAULTS = {
clientPrerender: false,
globalRoutePriority: false,
rewriting: false,
env: {
// Make TS happy
schema: {},
},
},
} satisfies AstroUserConfig & { server: { open: boolean } };

Expand Down Expand Up @@ -525,7 +529,7 @@ export const AstroConfigSchema = z.object({
rewriting: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.experimental.rewriting),
env: z
.object({
schema: EnvSchema.optional(),
schema: EnvSchema,
})
.strict()
.optional(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AstroError, AstroErrorData } from '../core/errors/index.js';
export { validateEnvVariable } from './validators.js';
import { AstroError, AstroErrorData } from '../../core/errors/index.js';
export { validateEnvVariable } from '../validators.js';

export type GetEnv = (key: string) => string | undefined;

Expand Down
48 changes: 48 additions & 0 deletions packages/astro/src/env/runtime/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { MiddlewareHandler } from '../../@types/astro.js';

interface MiddewareOptions {
/**
* Filters what response content type should trigger the check. Defaults to the content type
* starting with `/text` or `application/json`
*/
filterContentType?: (contentType: string) => boolean;
/**
* By default, all server environment variables are checked. However, you may have variables
* whose value is really likely to end up on the client but not because it leaked (eg. `test`).
* In this case, you can exclude those keys.
*/
// @ts-ignore
excludeKeys?: Array<keyof Omit<typeof import('astro:env/server'), 'getSecret'>>;
}

/**
* This middleware will throw if a response with the specified content type contains a server
* environment variable.
*/
export function leakDetectionMiddleware({
filterContentType = (v) => v.startsWith('text/') || v.startsWith('application/json'),
excludeKeys = [],
}: MiddewareOptions = {}): MiddlewareHandler {
return async (_, next) => {
const response = await next();

const contentType = response.headers.get('Content-Type');
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved
if (contentType && filterContentType(contentType)) {
const content = await response.clone().text();
const { getSecret, ...secrets }: Record<string, string | undefined> = await import(
// @ts-ignore
'astro:env/server'
);
for (const [key, value] of Object.entries(secrets)) {
if (excludeKeys.includes(key) || value === undefined) {
continue;
}
if (content.includes(value)) {
throw new Error(`[astro:env] \`${key}\` leaked client-side.`);
}
}
}

return response;
};
}
2 changes: 1 addition & 1 deletion packages/astro/src/env/setup.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { setGetEnv, type GetEnv } from './runtime.js';
export { setGetEnv, type GetEnv } from './runtime/index.js';
2 changes: 1 addition & 1 deletion packages/astro/src/env/vite-plugin-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function astroEnv({
if (!settings.config.experimental.env) {
return;
}
const schema = settings.config.experimental.env.schema ?? {};
const schema = settings.config.experimental.env?.schema ?? {};

let templates: { client: string; server: string; internal: string } | null = null;

Expand Down
21 changes: 21 additions & 0 deletions packages/astro/test/env-leak-detection.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from 'node:assert/strict';
import { describe, it } from 'node:test';
import { loadFixture } from './test-utils.js';

describe('astro:env leak detection', () => {
it('should fail if a secret is sent to the client', async () => {
const fixture = await loadFixture({
root: './fixtures/astro-env-leak-detection/',
});

let error;
try {
await fixture.build();
} catch (err) {
error = err;
}

assert.equal(error instanceof Error, true);
assert.equal(error.message.includes('leaked client-side'), true);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig, envField } from 'astro/config';

// https://astro.build/config
export default defineConfig({
experimental: {
env: {
schema: {
FOO: envField.string({
context: 'server',
access: 'secret',
default: 'this is a secret'
}),
},
}
}
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "@test/astro-env-leak-detection",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { leakDetectionMiddleware } from 'astro/env/middleware'

export const onRequest = leakDetectionMiddleware()
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
import { FOO } from "astro:env/server"
---
<p>{FOO}</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "astro/tsconfigs/base"
}
7 changes: 6 additions & 1 deletion pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading