Skip to content

Marketing skills for Claude Code and AI agents. CRO, copywriting, SEO, analytics, and growth engineering.

fix(customer-io): encode customer identifiers in request paths

Rudy Celekli5fd4f5259728
diff --git a/tests/clis/customerio-identifiers.test.cjs b/tests/clis/customerio-identifiers.test.cjs
new file mode 100644
index 0000000..9f5e234
--- /dev/null
+++ b/tests/clis/customerio-identifiers.test.cjs
@@ -0,0 +1,51 @@
+const { test } = require('node:test')
+const assert = require('node:assert/strict')
+const { spawnSync } = require('node:child_process')
+const path = require('node:path')
+
+function run(args, dryRun = false) {
+ const cli = path.resolve(__dirname, '../../tools/clis/customer-io.js')
+ const source = `global.fetch = async (url, options) => {
+ if (${dryRun}) throw new Error('Unexpected network request');
+ const request = { url: String(url), method: options.method, body: options.body ? JSON.parse(options.body) : null };
+ return { status: 200, text: async () => JSON.stringify(request) };
+ }; process.argv = ['node', ${JSON.stringify(cli)}, ...${JSON.stringify(args)}]; require(${JSON.stringify(cli)});`
+ const result = spawnSync(process.execPath, ['-e', source], { encoding: 'utf8', timeout: 5000, env: {
+ ...process.env, CUSTOMERIO_SITE_ID: 'fixture-site', CUSTOMERIO_API_KEY: 'fixture-track-key', CUSTOMERIO_APP_KEY: 'fixture-app-key'
+ } })
+ assert.equal(result.status, 0, result.stderr)
+ return JSON.parse(result.stdout)
+}
+
+const identifier = 'team/[email protected]?workspace=1#profile%20'
+const unicodeIdentifier = 'café/東京 +user\n#details'
+for (const [action, method, suffix, flags] of [
+ ['identify', 'PUT', '', ['--email', '[email protected]']],
+ ['get', 'GET', '/attributes', []],
+ ['delete', 'DELETE', '', []],
+ ['track-event', 'POST', '/events', ['--name', 'signed_up']],
+]) {
+ for (const selectedId of [identifier, unicodeIdentifier]) {
+ test(`customer ${action} encodes ${JSON.stringify(selectedId)} as one path segment`, () => {
+ const result = run(['customers', action, selectedId, ...flags])
+ const url = new URL(result.url)
+ const base = action === 'get' ? '/v1' : '/api/v1'
+ assert.equal(url.pathname, `${base}/customers/${encodeURIComponent(selectedId)}${suffix}`)
+ assert.equal(url.search, '')
+ assert.equal(url.hash, '')
+ assert.equal(result.method, method)
+ })
+ }
+}
+test('the --id form preserves literal percent sequences without pre-decoding', () => {
+ const result = run(['customers', 'delete', '--id', 'customer%2Fid'])
+ assert.equal(new URL(result.url).pathname, '/api/v1/customers/customer%252Fid')
+})
+test('ordinary customer identifiers remain compatible', () => {
+ assert.equal(run(['customers', 'get', 'customer_123']).url, 'https://api.customer.io/v1/customers/customer_123/attributes')
+})
+test('dry run uses the encoded identifier and masks Track auth', () => {
+ const result = run(['customers', 'identify', identifier, '--dry-run'], true)
+ assert.equal(new URL(result.url).pathname, `/api/v1/customers/${encodeURIComponent(identifier)}`)
+ assert.equal(result.headers.Authorization, '***')
+})
diff --git a/tools/clis/customer-io.js b/tools/clis/customer-io.js
index b72485f..3a59fb8 100755
--- a/tools/clis/customer-io.js
+++ b/tools/clis/customer-io.js
@@ -103,19 +103,19 @@ async function main() {
if (args['created-at']) body.created_at = parseInt(args['created-at'])
if (args.plan) body.plan = args.plan
if (args.data) Object.assign(body, JSON.parse(args.data))
- result = await trackApi('PUT', `/customers/${customerId}`, body)
+ result = await trackApi('PUT', `/customers/${encodeURIComponent(customerId)}`, body)
break
}
case 'get': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
- result = await appApi('GET', `/customers/${customerId}/attributes`)
+ result = await appApi('GET', `/customers/${encodeURIComponent(customerId)}/attributes`)
break
}
case 'delete': {
const customerId = rest[0] || args.id
if (!customerId) { result = { error: 'Customer ID required (positional arg or --id)' }; break }
- result = await trackApi('DELETE', `/customers/${customerId}`)
+ result = await trackApi('DELETE', `/customers/${encodeURIComponent(customerId)}`)
break
}
case 'track-event': {
@@ -124,7 +124,7 @@ async function main() {
if (!args.name) { result = { error: '--name required (event name)' }; break }
const body = { name: args.name }
if (args.data) body.data = JSON.parse(args.data)
- result = await trackApi('POST', `/customers/${customerId}/events`, body)
+ result = await trackApi('POST', `/customers/${encodeURIComponent(customerId)}/events`, body)
break
}
default: