Skip to content
Merged
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
11 changes: 6 additions & 5 deletions packages/coding-agent/src/features/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,11 +442,12 @@ export function createStepExtension(options: StepExtensionOptions = {}): Extensi
pi.on("before_provider_headers", (event, ctx) => {
const model = ctx.model;
if (!model) return;
// x-step-client marks every request. The session/workspace/provider
// attribution follows only Step-owned providers (native step + product
// proxies), keyed by provider id, so it never egresses to a distinct
// third-party provider. Caveat: overriding STEP_BASE_URL under the
// "step" id points that provider at another host and attribution follows.
// x-step-client marks every request. The session id additionally rides
// every request to a Step first-party host (api.stepfun.com / .ai) as
// x-session-id, keyed by the request URL host regardless of provider id.
// The remaining workspace/goal/provider attribution is policy-gated
// (allowlist + highSensitivityFields), so it never egresses to a
// third-party endpoint.
applyStepTraceHeaders(
event.headers,
{
Expand Down
43 changes: 37 additions & 6 deletions packages/coding-agent/src/step/trace-headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,25 @@ const TRACE_HEADER_ENV_NAMES = [
*/
const STEP_TRACE_PRODUCT_PROVIDERS = new Set(["stepfunModelProxy", "neocodex"]);

/**
* Step's first-party hosts (domestic `.com` + oversea `.ai`). Requests to
* `https://api.stepfun.com/...` or `https://api.stepfun.ai/...` always carry
* the session id as `x-session-id`, regardless of the host trace policy — the
* session id is first-party attribution there, not third-party egress.
*/
export const STEP_FIRST_PARTY_TRACE_HOSTS: readonly string[] = ["api.stepfun.com", "api.stepfun.ai"];

/** Whether a request URL targets a Step first-party host over https. */
export function isStepFirstPartyTraceUrl(requestUrl: string | undefined): boolean {
if (!requestUrl) return false;
try {
const url = new URL(requestUrl);
return url.protocol === "https:" && STEP_FIRST_PARTY_TRACE_HOSTS.includes(url.hostname);
} catch {
return false;
}
}

/**
* Add Step attribution headers in place.
*
Expand All @@ -58,13 +77,17 @@ const STEP_TRACE_PRODUCT_PROVIDERS = new Set(["stepfunModelProxy", "neocodex"]);
* replaces a half). The server reads the pair to decide which client releases
* may use Step-specific capacity; the same identity is appended to the provider
* `User-Agent` as `name/version`.
* The high-sensitivity fields
* (session/workspace/goal/...) are sent only when the request URL matches
*
* Requests to a Step first-party host (`https://api.stepfun.com/...` or
* `https://api.stepfun.ai/...`) always carry the session id as `x-session-id`
* too — first-party attribution there, not third-party egress. Every other
* high-sensitivity field (workspace/goal/...) — and the session id on any
* non-first-party host — is sent only when the request URL matches
* `options.allowedBaseUrls` AND the field appears in
* `options.highSensitivityFields`; an omitted or empty field list sends none of
* them (fail closed), so the local cwd and session id never egress to a
* third-party model endpoint. Values are size-bounded and header-safe via
* {@link encodeStepTraceHeaderValue}.
* them (fail closed), so the local cwd never egresses to a third-party model
* endpoint. The session id keeps that single name on every path. Values are
* size-bounded and header-safe via {@link encodeStepTraceHeaderValue}.
*/
export function applyStepTraceHeaders(
headers: Record<string, string | null>,
Expand All @@ -75,6 +98,12 @@ export function applyStepTraceHeaders(
setHeader(headers, "x-step-client", options.clientType ?? identity.name);
setHeader(headers, "x-step-client-version", options.clientVersion ?? identity.version);

// Step's first-party host always receives the session id, independent of the
// host trace policy. Other fields (and the session id elsewhere) stay gated.
if (isStepFirstPartyTraceUrl(options.requestUrl)) {
setHeader(headers, "x-session-id", trace.sessionId);
}

const traceAllowed =
options.requestUrl === undefined ||
options.allowedBaseUrls === undefined ||
Expand All @@ -89,7 +118,9 @@ export function applyStepTraceHeaders(
const setIfAllowed = (field: string, value: string | undefined): void => {
if (allowed.has(field)) setHeader(headers, `x-step-${field}`, value);
};
setIfAllowed("session-id", trace.sessionId);
// The session id keeps one name on every path; the other attributed fields
// stay under the x-step-* prefix.
if (allowed.has("session-id")) setHeader(headers, "x-session-id", trace.sessionId);
setIfAllowed("goal-id", trace.goalId);
setIfAllowed("attempt-id", trace.attemptId);
setIfAllowed("harness-id", trace.harnessId);
Expand Down
7 changes: 4 additions & 3 deletions packages/coding-agent/test/step-extension.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,11 +846,12 @@ describe("Step extension", () => {
sessionManager: { getSessionId: () => "session-1" },
} as unknown as Pick<ExtensionContext, "model" | "cwd" | "sessionManager">);
// The native Step endpoint is a direct model API, not the cloud-trace
// collector. Keep session/workspace identifiers off custom or direct
// provider requests; only the low-sensitivity client markers are universal.
// collector: workspace identifiers stay off it, while the session id rides
// along as first-party attribution under its single vendor-neutral name.
expect(headers).toEqual({
"x-step-client": "stepcode-cli",
"x-step-client-version": STEPCODE_VERSION.value,
"x-session-id": "session-1",
});

const originalOrigin = process.env.STEPCODE_CLOUD_TRACE_ORIGIN;
Expand All @@ -869,7 +870,7 @@ describe("Step extension", () => {
expect(otherHeaders).toMatchObject({
"x-step-client": "stepcode-cli",
"x-step-client-version": STEPCODE_VERSION.value,
"x-step-session-id": "session-2",
"x-session-id": "session-2",
"x-step-workspace-id": "/workspace",
"x-step-provider-id": "stepfunModelProxy",
"x-step-model": "step-proxy",
Expand Down
4 changes: 2 additions & 2 deletions packages/coding-agent/test/step-model-queue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,15 @@ test("forwards the request's own headers to the control requests, except those d
headers: {
authorization: "Bearer sk-test",
"x-api-key": "k",
"x-step-session-id": "s1",
"x-session-id": "s1",
"content-type": "application/json",
"content-length": String(BODY.length),
},
});
const poll = h.calls[1];
assert.equal(poll?.headers.get("authorization"), "Bearer sk-test");
assert.equal(poll?.headers.get("x-api-key"), "k");
assert.equal(poll?.headers.get("x-step-session-id"), "s1");
assert.equal(poll?.headers.get("x-session-id"), "s1");
assert.equal(poll?.headers.get("accept"), "application/json");
assert.equal(poll?.headers.has("content-type"), false);
assert.equal(poll?.headers.has("content-length"), false);
Expand Down
73 changes: 71 additions & 2 deletions packages/coding-agent/test/step-trace-headers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, it } from "vitest";
import {
applyStepTraceHeaders,
encodeStepTraceHeaderValue,
isStepFirstPartyTraceUrl,
isStepTraceRequestAllowed,
matchesStepTraceBaseUrl,
resolveStepTraceAllowlist,
Expand Down Expand Up @@ -66,7 +67,7 @@ describe("Step trace headers", () => {
const headers: Record<string, string | null> = {
"X-Step-Client": "stale",
"X-Step-Client-Version": "0.0.1",
"x-step-session-id": "stale-session",
"x-session-id": "stale-session",
accept: "application/json",
};
applyStepTraceHeaders(headers, trace, {
Expand All @@ -76,14 +77,82 @@ describe("Step trace headers", () => {
allowedBaseUrls: ["https://allowed.example/v1"],
});

// Not a first-party host and outside the allowlist: the caller's own
// `x-session-id` is left alone, and no attribution field is added.
expect(headers).toEqual({
"x-step-client": "stepcode-desktop",
"x-step-client-version": "1.4.0",
"x-step-session-id": "stale-session",
"x-session-id": "stale-session",
accept: "application/json",
});
});

it("sends the session id as x-session-id on Step's first-party hosts", () => {
for (const requestUrl of [
"https://api.stepfun.com/step_plan/v1/chat/completions",
"https://api.stepfun.com/v1/messages",
"https://api.stepfun.ai/v1/responses",
]) {
const headers: Record<string, string | null> = {};
// No allowlist and no field policy: the first-party host still gets the
// session id, and nothing else beyond the client pair.
applyStepTraceHeaders(headers, trace, { requestUrl, allowedBaseUrls: [] });
expect(headers).toEqual({
"x-step-client": "stepcode-cli",
"x-step-client-version": STEPCODE_VERSION.value,
"x-session-id": "session-1",
});
}
});

it("keeps the session id off every other host", () => {
const headers: Record<string, string | null> = {};
applyStepTraceHeaders(headers, trace, {
requestUrl: "https://api.stepfun.com.example/v1",
allowedBaseUrls: [],
});
expect(headers).toEqual({
"x-step-client": "stepcode-cli",
"x-step-client-version": STEPCODE_VERSION.value,
});
});

it("omits x-session-id on a first-party host when the session id is absent", () => {
const headers: Record<string, string | null> = {};
applyStepTraceHeaders(headers, {}, { requestUrl: "https://api.stepfun.com/step_plan/v1", allowedBaseUrls: [] });
expect(headers).toEqual({
"x-step-client": "stepcode-cli",
"x-step-client-version": STEPCODE_VERSION.value,
});
});

it("recognizes only https first-party hosts", () => {
expect(isStepFirstPartyTraceUrl("https://api.stepfun.com/step_plan/v1")).toBe(true);
expect(isStepFirstPartyTraceUrl("https://API.Stepfun.Com/v1")).toBe(true);
expect(isStepFirstPartyTraceUrl("https://api.stepfun.ai/v1")).toBe(true);
expect(isStepFirstPartyTraceUrl("http://api.stepfun.com/v1")).toBe(false);
expect(isStepFirstPartyTraceUrl("https://api.stepfun.com.evil/v1")).toBe(false);
expect(isStepFirstPartyTraceUrl("https://api.stepfun.com@evil.example/v1")).toBe(false);
expect(isStepFirstPartyTraceUrl("https://other.stepfun.com/v1")).toBe(false);
expect(isStepFirstPartyTraceUrl(undefined)).toBe(false);
});

it("does not duplicate the session id when the first-party host is also allowlisted", () => {
const headers: Record<string, string | null> = {};
applyStepTraceHeaders(headers, trace, {
requestUrl: "https://api.stepfun.com/step_plan/v1",
allowedBaseUrls: ["https://api.stepfun.com/step_plan/v1"],
highSensitivityFields: ["session-id", "workspace-id"],
});
expect(headers["x-step-client"]).toBe("stepcode-cli");
expect(headers["x-step-client-version"]).toBe(STEPCODE_VERSION.value);
// One name for the session id on every path: the first-party rule and the
// allowlist both naming it must not emit a second header.
expect(headers["x-session-id"]).toBe("session-1");
expect(Object.keys(headers).filter((name) => name.includes("session"))).toEqual(["x-session-id"]);
expect(decode(headers["x-step-workspace-id"] as string)).toBe("/tmp/工作区");
});

it("treats an explicitly empty allowlist as untrusted", () => {
const headers: Record<string, string | null> = {};
applyStepTraceHeaders(headers, trace, {
Expand Down