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
6 changes: 6 additions & 0 deletions .changeset/sweet-apples-camp.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@rock-js/tools': patch
'rock': patch
---

fix: properly take env value into account; hash sensitive data
30 changes: 27 additions & 3 deletions packages/cli/src/lib/plugins/fingerprint.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { createHash } from 'node:crypto';
import { performance } from 'node:perf_hooks';
import type { PluginApi } from '@rock-js/config';
import type { FingerprintSources } from '@rock-js/tools';
import type { FingerprintInputHash, FingerprintSources } from '@rock-js/tools';
import {
color,
intro,
Expand All @@ -12,6 +13,25 @@ import {
spinner,
} from '@rock-js/tools';

const hashValue = (value: string) =>
`[HASHED:${createHash('sha256').update(value).digest('hex').substring(0, 8)}]`;

/**
* Redacts sensitive environment variables from fingerprint sources by hashing their values
*/
function redactSensitiveSources(sources: FingerprintInputHash[]) {
return sources.map((source) => {
if (source.key === 'json:env' && 'json' in source) {
const env = source.json as Record<string, string>;
const redactedEnv = Object.fromEntries(
Object.entries(env).map(([key, value]) => [key, hashValue(value)]),
);
return { ...source, json: redactedEnv };
}
return source;
});
}

type NativeFingerprintCommandOptions = {
platform: 'ios' | 'android';
raw?: boolean;
Expand Down Expand Up @@ -39,7 +59,9 @@ export async function nativeFingerprintCommand(
JSON.stringify(
{
hash: fingerprint.hash,
sources: fingerprint.inputs.filter((source) => source.hash != null),
sources: redactSensitiveSources(
fingerprint.inputs.filter((source) => source.hash != null),
),
},
null,
2,
Expand Down Expand Up @@ -69,7 +91,9 @@ export async function nativeFingerprintCommand(
logger.debug(
'Sources:',
JSON.stringify(
fingerprint.inputs.filter((source) => source.hash != null),
redactSensitiveSources(
fingerprint.inputs.filter((source) => source.hash != null),
),
null,
2,
),
Expand Down
12 changes: 11 additions & 1 deletion packages/tools/src/lib/fingerprint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
getDefaultIgnorePaths,
getPlatformDirIgnorePaths,
} from './ignorePaths.js';
export type { FingerprintInputHash } from 'fs-fingerprint';

export type FingerprintSources = {
extraSources: string[];
Expand Down Expand Up @@ -61,6 +62,15 @@
throw new Error('No platforms found in autolinking project config');
}

let env = undefined;

if (options.env.length > 0) {
env = options.env.reduce((acc: Record<string, string>, key: string) => {
acc[key] = process.env[key] ?? '';
return acc;
}, {});
}

const fingerprint = await calculateFingerprint(projectRoot, {
ignoreFilePath: '.gitignore',
include: [
Expand All @@ -79,7 +89,7 @@
key: 'reactNativeVersion',
json: { version: getReactNativeVersion(projectRoot) },
},
...(options.env.length > 0 ? [{ key: 'env', json: options.env }] : []),
...(env ? [{ key: 'env', json: env }] : []),
],
exclude: [
...getDefaultIgnorePaths(),
Expand All @@ -106,7 +116,7 @@
return filePath.replace(/\\/g, '/');
}

function parseAutolinkingSources(config: any): Record<string, any> {

Check warning on line 119 in packages/tools/src/lib/fingerprint/index.ts

View workflow job for this annotation

GitHub Actions / Validate

Unexpected any. Specify a different type

Check warning on line 119 in packages/tools/src/lib/fingerprint/index.ts

View workflow job for this annotation

GitHub Actions / Validate

Unexpected any. Specify a different type
const { root } = config;
for (const [depName, depData] of Object.entries<any>(config.dependencies)) {
try {
Expand Down
Loading