Skip to content
Draft
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
25 changes: 21 additions & 4 deletions packages/app/src/cli/services/app/env/pull.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('env pull', () => {
"Created ${filePath}:

SHOPIFY_API_KEY=api-key
SHOPIFY_API_SECRET=api-secret
SHOPIFY_API_SECRET=***
SCOPES=my-scope
"
`)
Expand All @@ -66,15 +66,15 @@ describe('env pull', () => {
"Updated ${filePath} to be:

SHOPIFY_API_KEY=api-key
SHOPIFY_API_SECRET=api-secret
SHOPIFY_API_SECRET=***
SCOPES=my-scope

Here's what changed:

- SHOPIFY_API_KEY=ABC
- SHOPIFY_API_SECRET=XYZ
- SHOPIFY_API_SECRET=***
+ SHOPIFY_API_KEY=api-key
+ SHOPIFY_API_SECRET=api-secret
+ SHOPIFY_API_SECRET=***
SCOPES=my-scope
"
`)
Expand All @@ -98,6 +98,23 @@ describe('env pull', () => {
`)
})
})

test('masks secrets even in common lines of the diff', async () => {
await file.inTemporaryDirectory(async (tmpDir: string) => {
// Given
const filePath = resolvePath(tmpDir, '.env')
// SHOPIFY_API_SECRET is the same in both versions, but other fields change
file.writeFileSync(filePath, 'SHOPIFY_API_KEY=ABC\nSHOPIFY_API_SECRET=api-secret\nSCOPES=old-scope')
vi.spyOn(file, 'writeFile')

// When
const result = await pullEnv({app, remoteApp, organization: ORG1, envFile: filePath})

// Then
expect(unstyled(stringifyMessage(result))).toContain('SHOPIFY_API_SECRET=***')
expect(unstyled(stringifyMessage(result))).not.toContain('SHOPIFY_API_SECRET=api-secret')
})
})
})

function mockApp(): AppInterface {
Expand Down
27 changes: 23 additions & 4 deletions packages/app/src/cli/services/app/env/pull.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {logMetadataForLoadedContext} from '../../context.js'

import {Organization, OrganizationApp} from '../../../models/organization.js'
import {patchEnvFile} from '@shopify/cli-kit/node/dot-env'
import {diffLines} from 'diff'
import {diffLines, Change} from 'diff'
import {fileExists, readFile, writeFile} from '@shopify/cli-kit/node/fs'
import {OutputMessage, outputContent, outputToken} from '@shopify/cli-kit/node/output'

Expand Down Expand Up @@ -36,11 +36,11 @@ export async function pullEnv({app, remoteApp, organization, envFile}: PullEnvOp
const diff = diffLines(envFileContent ?? '', updatedEnvFileContent)
return outputContent`Updated ${outputToken.path(envFile)} to be:

${updatedEnvFileContent}
${maskSecret(updatedEnvFileContent)}

Here's what changed:

${outputToken.linesDiff(diff)}
${outputToken.linesDiff(maskDiff(diff))}
`
}
} else {
Expand All @@ -50,7 +50,26 @@ ${outputToken.linesDiff(diff)}

return outputContent`Created ${outputToken.path(envFile)}:

${newEnvFileContent}
${maskSecret(newEnvFileContent)}
`
}
}

function maskSecret(envFileContent: string): string {
return envFileContent.replace(/SHOPIFY_API_SECRET=.*/g, (match) => {
const index = match.indexOf('=')
if (index === -1) return match
const key = match.substring(0, index)
const value = match.substring(index + 1)
return `${key}=${outputToken.mask(value).output()}`
})
}

function maskDiff(diff: Change[]): Change[] {
return diff.map((part) => {
return {
...part,
value: maskSecret(part.value),
}
})
}
2 changes: 1 addition & 1 deletion packages/app/src/cli/services/app/env/show.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('env show', () => {
expect(unstyled(stringifyMessage(result))).toMatchInlineSnapshot(`
"
SHOPIFY_API_KEY=api-key
SHOPIFY_API_SECRET=api-secret
SHOPIFY_API_SECRET=***
SCOPES=my-scope
"
`)
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/cli/services/app/env/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export async function outputEnv(
} else {
return outputContent`
${outputToken.green('SHOPIFY_API_KEY')}=${remoteApp.apiKey}
${outputToken.green('SHOPIFY_API_SECRET')}=${remoteApp.apiSecretKeys[0]?.secret ?? ''}
${outputToken.green('SHOPIFY_API_SECRET')}=${outputToken.mask(remoteApp.apiSecretKeys[0]?.secret ?? '')}
${outputToken.green('SCOPES')}=${getAppScopes(app.configuration)}
`
}
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/cli/services/info.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('info', () => {
expect(unstyled(stringifyMessage(result))).toMatchInlineSnapshot(`
"
SHOPIFY_API_KEY=api-key
SHOPIFY_API_SECRET=api-secret
SHOPIFY_API_SECRET=***
SCOPES=my-scope
"
`)
Expand Down
8 changes: 8 additions & 0 deletions packages/cli-kit/src/private/node/content-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,11 @@ export class ItalicContentToken extends ContentToken<OutputMessage> {
return colors.italic(stringifyMessage(this.value))
}
}

export class MaskContentToken extends ContentToken<string> {
output(): string {
if (this.value.length === 0) return ''
if (this.value.length <= 20) return '***'
return `${this.value.substring(0, 10)}***`
}
}
4 changes: 4 additions & 0 deletions packages/cli-kit/src/public/node/output.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
JsonContentToken,
LinesDiffContentToken,
LinkContentToken,
MaskContentToken,
PathContentToken,
RawContentToken,
SubHeadingContentToken,
Expand Down Expand Up @@ -92,6 +93,9 @@ export const outputToken = {
linesDiff(value: Change[]): LinesDiffContentToken {
return new LinesDiffContentToken(value)
},
mask(value: string): MaskContentToken {
return new MaskContentToken(value)
},
}

/**
Expand Down
Loading