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
2 changes: 1 addition & 1 deletion devbox.lock
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
},
"nodejs@22": {
"last_modified": "2026-03-27T11:17:38Z",
"plugin_version": "0.0.4",
"plugin_version": "0.0.2",
"resolved": "github:NixOS/nixpkgs/832efc09b4caf6b4569fbf9dc01bec3082a00611#nodejs_22",
"source": "devbox-search",
"version": "22.22.2",
Expand Down
44 changes: 43 additions & 1 deletion packages/core/src/__tests__/util.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { UserTraits } from '../types';
import { chunk, allSettled, deepCompare, getURL } from '../util';
import {
chunk,
allSettled,
deepCompare,
getURL,
validateApiHost,
} from '../util';

describe('#chunk', () => {
it('handles empty array', () => {
Expand Down Expand Up @@ -210,3 +216,39 @@ describe('getURL function', () => {
);
});
});

describe('validateApiHost', () => {
it('accepts a bare hostname', () => {
expect(validateApiHost('api.segment.io')).toBe(true);
});

it('accepts hostname with path (normal Segment format)', () => {
expect(validateApiHost('api.segment.io/v1')).toBe(true);
expect(validateApiHost('events.eu1.segmentapis.com')).toBe(true);
});

it('accepts hostname with port', () => {
expect(validateApiHost('api.segment.io:443/v1')).toBe(true);
});

it('rejects values with a scheme', () => {
expect(validateApiHost('https://api.segment.io/v1')).toBe(false);
expect(validateApiHost('http://api.segment.io/v1')).toBe(false);
});

it('rejects values with credentials', () => {
expect(validateApiHost('user:pass@api.segment.io')).toBe(false);
});

it('rejects values with a query string', () => {
expect(validateApiHost('attacker.com/collect?x=')).toBe(false);
});

it('rejects values with a fragment', () => {
expect(validateApiHost('attacker.com/path#fragment')).toBe(false);
});

it('rejects empty string', () => {
expect(validateApiHost('')).toBe(false);
});
});
11 changes: 8 additions & 3 deletions packages/core/src/plugins/SegmentDestination.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
SegmentEvent,
UpdateType,
} from '../types';
import { chunk, createPromise, getURL } from '../util';
import { chunk, createPromise, getURL, validateApiHost } from '../util';
import { uploadEvents } from '../api';
import type { SegmentClient } from '../analytics';
import { DestinationMetadataEnrichment } from './DestinationMetadataEnrichment';
Expand Down Expand Up @@ -446,8 +446,13 @@ export class SegmentDestination extends DestinationPlugin {
segmentSettings?.apiHost !== undefined &&
segmentSettings?.apiHost !== null
) {
//assign the api host from segment settings (domain/v1)
this.apiHost = `https://${segmentSettings.apiHost}/b`;
if (validateApiHost(segmentSettings.apiHost)) {
this.apiHost = `https://${segmentSettings.apiHost}/b`;
} else {
console.error(
`[Segment] Invalid apiHost "${segmentSettings.apiHost}" received from settings — ignoring, using default endpoint.`
);
}
}

// Read httpConfig: prefer integration-level settings from CDN, fall back to
Expand Down
22 changes: 22 additions & 0 deletions packages/core/src/plugins/__tests__/SegmentDestination.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,28 @@ describe('SegmentDestination', () => {
});
});

it('ignores a tampered apiHost containing a scheme or query string and falls back to default', async () => {
const events = [{ messageId: 'msg-1' }] as SegmentEvent[];

for (const badHost of [
'https://attacker.com/collect',
'http://attacker.com',
'attacker.com/collect?x=',
'user:pass@attacker.com',
]) {
const { plugin, sendEventsSpy } = createTestWith({
events,
settings: { apiKey: '', apiHost: badHost },
});
jest.spyOn(console, 'error').mockImplementation(jest.fn());
await plugin.flush();
expect(sendEventsSpy).toHaveBeenCalledWith(
expect.objectContaining({ url: defaultApiHost })
);
sendEventsSpy.mockClear();
}
});

it.each([
[false, false], // No proxy, No segment endpoint
[false, true], // No proxy, Yes segment endpoint
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,16 @@ export const createPromise = <T>(
};
};

// Accepts a bare host[/path] value (e.g. "api.segment.io/v1") as supplied by
// the settings CDN. Rejects anything that contains a scheme, credentials,
// query string, or fragment — all of which could redirect uploads to an
// attacker-controlled endpoint when interpolated into `https://${apiHost}/b`.
export function validateApiHost(apiHost: string): boolean {
return /^[a-zA-Z0-9][a-zA-Z0-9._-]*(:\d{2,5})?(\/[a-zA-Z0-9._\-/]*)?$/.test(
apiHost
);
}

export function getURL(host: string, path: string, allowInsecure = false) {
if (!host.startsWith('https://') && !host.startsWith('http://')) {
host = 'https://' + host;
Expand Down
Loading