Repository navigation
feat(azure): keep az's network traffic on the emulator with an egress guard #94
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
Open
DrisDary
wants to merge
1
commit into
main
Choose a base branch
from
feat/azure-egress-guard
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,5 +7,16 @@ | |
| - Bicep templates need the `bicep` binary on your `PATH` (for example `brew install bicep` or `winget install Microsoft.Bicep`). | ||
| - Relative file paths in commands resolve against `LOCALSTACK_AZ_WORKDIR`, by default the directory the MCP client starts the server in. | ||
| - `rest` calls the emulator: give it a relative URL. An absolute URL must name a local host; a `management.azure.com` URL is turned into a relative one. | ||
| - Only `rest` URLs are checked: an endpoint given in a command, such as `--blob-endpoint` or a connection string, is used as it is. | ||
| - `az` reaches only the emulator: see [What `az` can reach](#what-az-can-reach). | ||
| - The Docker image does not include the Azure CLI yet: run the server with npx or from source to use the Azure tool. | ||
|
|
||
| ## What `az` can reach | ||
|
|
||
| The agent writes the commands, and a command can name any endpoint: `--blob-endpoint`, a connection string, a `rest` URL. `az` would send that endpoint the file the command reads, with a key from the command. So every `az` run goes through an egress guard, a proxy on `127.0.0.1` that the tool starts for that run and points `HTTPS_PROXY` and `HTTP_PROXY` at. | ||
|
Member
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. it took me a while to understand this, I finally got what it does. It's not blocking, but the explanation to could be simplified or re-written for clarity. |
||
|
|
||
| - It relays only to the emulator: `localhost.localstack.cloud` and every name under it, `localhost`, `127.0.0.1` and `::1`. | ||
| - It relays only to the emulator's ports: `LOCALSTACK_PORT`, 443 and the external-service range (`EXTERNAL_SERVICE_PORTS_START` to `_END`, by default 4510 to 4560). Other local services, such as a Docker API on `localhost:2375`, stay out of reach. | ||
| - It sends those names to `127.0.0.1` itself, so no DNS answer decides where a request goes, and routers that block LocalStack's DNS answers do not matter. | ||
| - Every other request is refused, and the tool's answer names the host. | ||
|
|
||
| It covers what honours `HTTPS_PROXY` and `HTTP_PROXY`: `az` itself, the Azure SDKs and MSAL, and Bicep. It does not cover programs that open connections of their own, such as the Docker daemon behind `az acr login`, or a database client behind `flexible-server connect`. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| import http from "http"; | ||
| import net, { type AddressInfo } from "net"; | ||
| import { startEgressGuard, type EgressGuard } from "./egress-guard"; | ||
|
|
||
| // A local echo server stands in for the emulator; the guard relays to it on its port only. | ||
| let echo: net.Server; | ||
| let origin: http.Server; | ||
| let emulatorPort: number; | ||
| let httpPort: number; | ||
| let guard: EgressGuard; | ||
| const dialled: Array<[number, string]> = []; | ||
|
|
||
| const listen = (server: net.Server) => | ||
| new Promise<number>((resolve) => | ||
| server.listen(0, "127.0.0.1", () => resolve((server.address() as AddressInfo).port)) | ||
| ); | ||
| const guardPort = () => Number(new URL(guard.env.HTTPS_PROXY).port); | ||
|
|
||
| /** Sends CONNECT through the guard; resolves with its status and the open socket. */ | ||
| function connectThrough(authority: string): Promise<{ status: number; socket: net.Socket }> { | ||
| return new Promise((resolve, reject) => { | ||
| const socket = net.connect(guardPort(), "127.0.0.1"); | ||
| socket.once("error", reject); | ||
| let head = ""; | ||
| const onData = (chunk: Buffer) => { | ||
| head += chunk.toString("latin1"); | ||
| if (!head.includes("\r\n\r\n")) return; | ||
| socket.off("data", onData); | ||
| resolve({ status: Number(head.split(" ")[1]), socket }); | ||
| }; | ||
| socket.on("data", onData); | ||
| socket.write(`CONNECT ${authority} HTTP/1.1\r\nHost: ${authority}\r\n\r\n`); | ||
| }); | ||
| } | ||
|
|
||
| /** Sends a plain-HTTP proxy request (absolute URI) through the guard. */ | ||
| function getThrough(url: string): Promise<{ status: number; body: string }> { | ||
| return new Promise((resolve, reject) => { | ||
| const req = http.request({ host: "127.0.0.1", port: guardPort(), path: url }, (res) => { | ||
| let body = ""; | ||
| res.on("data", (c) => (body += c)); | ||
| res.on("end", () => resolve({ status: res.statusCode ?? 0, body })); | ||
| }); | ||
| req.on("error", reject); | ||
| req.end(); | ||
| }); | ||
| } | ||
|
|
||
| beforeAll(async () => { | ||
| echo = net.createServer((socket) => socket.pipe(socket)); | ||
| emulatorPort = await listen(echo); | ||
| origin = http.createServer((req, res) => res.end(`host=${req.headers.host} path=${req.url}`)); | ||
| httpPort = await listen(origin); | ||
| }); | ||
| afterAll(() => { | ||
| echo.close(); | ||
| origin.close(); | ||
| }); | ||
| beforeEach(async () => { | ||
| dialled.length = 0; | ||
| guard = await startEgressGuard({ | ||
| ports: [emulatorPort, httpPort], | ||
| connect: (port, host) => { | ||
| dialled.push([port, host]); | ||
| return net.connect({ port, host, allowHalfOpen: true }); | ||
| }, | ||
| }); | ||
| }); | ||
| afterEach(() => guard.close()); | ||
|
|
||
| test("points az's HTTPS_PROXY and HTTP_PROXY at itself", () => { | ||
| expect(guard.env.HTTPS_PROXY).toMatch(/^http:\/\/127\.0\.0\.1:\d+$/); | ||
| expect(guard.env.HTTP_PROXY).toBe(guard.env.HTTPS_PROXY); | ||
| }); | ||
|
|
||
| test("tunnels to the emulator's names on loopback, without DNS", async () => { | ||
| for (const host of ["azure.localhost.localstack.cloud", "LOCALHOST", "127.1", "[::1]"]) { | ||
| const { status, socket } = await connectThrough(`${host}:${emulatorPort}`); | ||
| if (host === "[::1]") { | ||
| // Relayed to ::1, where the echo server does not listen: the relay was attempted. | ||
| expect([200, 502]).toContain(status); | ||
| socket.destroy(); | ||
| continue; | ||
| } | ||
| expect(status).toBe(200); | ||
| const echoed = new Promise<string>((resolve) => socket.once("data", (d) => resolve(String(d)))); | ||
| socket.write("ping"); | ||
| expect(await echoed).toBe("ping"); | ||
| socket.destroy(); | ||
| } | ||
| expect(dialled.map(([, host]) => host)).toEqual(["127.0.0.1", "127.0.0.1", "127.0.0.1", "::1"]); | ||
| expect(guard.refused()).toEqual([]); | ||
| }); | ||
|
|
||
| test("refuses other hosts, and emulator names on other ports, and records them", async () => { | ||
| const refused = [ | ||
| "storage.evil.example:443", | ||
| "10.0.0.5:" + emulatorPort, | ||
| "localhost.localstack.cloud.evil.example:443", | ||
| "localhost:2375", | ||
| ]; | ||
| for (const authority of refused) { | ||
| const { status, socket } = await connectThrough(authority); | ||
| expect(status).toBe(403); | ||
| socket.destroy(); | ||
| } | ||
| expect(guard.refused()).toEqual([ | ||
| "storage.evil.example", | ||
| "10.0.0.5", | ||
| "localhost.localstack.cloud.evil.example", | ||
| "localhost:2375", | ||
| ]); | ||
| expect(dialled).toEqual([]); | ||
| }); | ||
|
|
||
| test("plain HTTP: forwards an absolute URI to an emulator name, refuses anything else", async () => { | ||
| const ok = await getThrough(`http://acct.blob.localhost.localstack.cloud:${httpPort}/c/b?x=1`); | ||
| expect(ok).toEqual({ | ||
| status: 200, | ||
| body: `host=acct.blob.localhost.localstack.cloud:${httpPort} path=/c/b?x=1`, | ||
| }); | ||
| expect((await getThrough("http://evil.example/upload")).status).toBe(403); | ||
| expect((await getThrough("/not-absolute")).status).toBe(400); | ||
| expect(guard.refused()).toEqual(["evil.example"]); | ||
| }); | ||
|
|
||
| test("a malformed CONNECT gets 400; close() ends an open tunnel", async () => { | ||
| expect((await connectThrough("no-port-here")).status).toBe(400); | ||
| const { status, socket } = await connectThrough(`localhost:${emulatorPort}`); | ||
| expect(status).toBe(200); | ||
| const closed = new Promise((resolve) => socket.once("close", resolve)); | ||
| await guard.close(); | ||
| await closed; | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
what's 'rest' in this context?