Skip to content

Commit c904ec9

Browse files
authored
fix: properly take env value into account; hash sensitive data (#591)
* fix: properly take env value into account; hash sensitive data * changeset
1 parent 288bd5f commit c904ec9

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

.changeset/sweet-apples-camp.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@rock-js/tools': patch
3+
'rock': patch
4+
---
5+
6+
fix: properly take env value into account; hash sensitive data

packages/cli/src/lib/plugins/fingerprint.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1+
import { createHash } from 'node:crypto';
12
import { performance } from 'node:perf_hooks';
23
import type { PluginApi } from '@rock-js/config';
3-
import type { FingerprintSources } from '@rock-js/tools';
4+
import type { FingerprintInputHash, FingerprintSources } from '@rock-js/tools';
45
import {
56
color,
67
intro,
@@ -12,6 +13,25 @@ import {
1213
spinner,
1314
} from '@rock-js/tools';
1415

16+
const hashValue = (value: string) =>
17+
`[HASHED:${createHash('sha256').update(value).digest('hex').substring(0, 8)}]`;
18+
19+
/**
20+
* Redacts sensitive environment variables from fingerprint sources by hashing their values
21+
*/
22+
function redactSensitiveSources(sources: FingerprintInputHash[]) {
23+
return sources.map((source) => {
24+
if (source.key === 'json:env' && 'json' in source) {
25+
const env = source.json as Record<string, string>;
26+
const redactedEnv = Object.fromEntries(
27+
Object.entries(env).map(([key, value]) => [key, hashValue(value)]),
28+
);
29+
return { ...source, json: redactedEnv };
30+
}
31+
return source;
32+
});
33+
}
34+
1535
type NativeFingerprintCommandOptions = {
1636
platform: 'ios' | 'android';
1737
raw?: boolean;
@@ -39,7 +59,9 @@ export async function nativeFingerprintCommand(
3959
JSON.stringify(
4060
{
4161
hash: fingerprint.hash,
42-
sources: fingerprint.inputs.filter((source) => source.hash != null),
62+
sources: redactSensitiveSources(
63+
fingerprint.inputs.filter((source) => source.hash != null),
64+
),
4365
},
4466
null,
4567
2,
@@ -69,7 +91,9 @@ export async function nativeFingerprintCommand(
6991
logger.debug(
7092
'Sources:',
7193
JSON.stringify(
72-
fingerprint.inputs.filter((source) => source.hash != null),
94+
redactSensitiveSources(
95+
fingerprint.inputs.filter((source) => source.hash != null),
96+
),
7397
null,
7498
2,
7599
),

packages/tools/src/lib/fingerprint/index.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
getDefaultIgnorePaths,
88
getPlatformDirIgnorePaths,
99
} from './ignorePaths.js';
10+
export type { FingerprintInputHash } from 'fs-fingerprint';
1011

1112
export type FingerprintSources = {
1213
extraSources: string[];
@@ -61,6 +62,15 @@ export async function nativeFingerprint(
6162
throw new Error('No platforms found in autolinking project config');
6263
}
6364

65+
let env = undefined;
66+
67+
if (options.env.length > 0) {
68+
env = options.env.reduce((acc: Record<string, string>, key: string) => {
69+
acc[key] = process.env[key] ?? '';
70+
return acc;
71+
}, {});
72+
}
73+
6474
const fingerprint = await calculateFingerprint(projectRoot, {
6575
ignoreFilePath: '.gitignore',
6676
include: [
@@ -79,7 +89,7 @@ export async function nativeFingerprint(
7989
key: 'reactNativeVersion',
8090
json: { version: getReactNativeVersion(projectRoot) },
8191
},
82-
...(options.env.length > 0 ? [{ key: 'env', json: options.env }] : []),
92+
...(env ? [{ key: 'env', json: env }] : []),
8393
],
8494
exclude: [
8595
...getDefaultIgnorePaths(),

0 commit comments

Comments
 (0)