Skip to content

Infisical is the open-source platform for securing secrets, certificates, access, and AI Agents.

fix: resolved command encoding in git scanner

=86b10ac610e2
diff --git a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
index f4b4c03a99..ce14d354ea 100644
--- a/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
+++ b/backend/src/ee/services/secret-scanning-v2/bitbucket/bitbucket-secret-scanning-factory.ts
@@ -1,4 +1,5 @@
import { join } from "path";
+import RE2 from "re2";
import { scanContentAndGetFindings } from "@app/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-fns";
import { SecretMatch } from "@app/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-queue-types";
@@ -41,6 +42,8 @@ import {
TQueueBitbucketResourceDiffScan
} from "./bitbucket-secret-scanning-types";
+const GitCommitShaRegex = new RE2(/^[a-f0-9]{7,40}$/);
+
export const BitbucketSecretScanningFactory = () => {
const initialize: TSecretScanningFactoryInitialize<
TBitbucketDataSourceInput,
@@ -120,7 +123,7 @@ export const BitbucketSecretScanningFactory = () => {
try {
await request.delete(
- `${IntegrationUrls.BITBUCKET_API_URL}/2.0/workspaces/${config.workspaceSlug}/hooks/${webhookId}`,
+ `${IntegrationUrls.BITBUCKET_API_URL}/2.0/workspaces/${encodeURIComponent(config.workspaceSlug)}/hooks/${webhookId}`,
{
headers: {
Authorization: authHeader,
@@ -212,6 +215,10 @@ export const BitbucketSecretScanningFactory = () => {
for (const change of push.changes) {
for (const commit of change.commits) {
+ if (!GitCommitShaRegex.test(commit.hash)) {
+ throw new Error("Invalid commit hash");
+ }
+
// eslint-disable-next-line no-await-in-loop
const { data: diffstat } = await request.get<{
values: {
@@ -219,12 +226,15 @@ export const BitbucketSecretScanningFactory = () => {
new?: { path: string };
old?: { path: string };
}[];
- }>(`${IntegrationUrls.BITBUCKET_API_URL}/2.0/repositories/${repository.full_name}/diffstat/${commit.hash}`, {
- headers: {
- Authorization: authHeader,
- Accept: "application/json"
+ }>(
+ `${IntegrationUrls.BITBUCKET_API_URL}/2.0/repositories/${encodeURIComponent(repository.full_name)}/diffstat/${commit.hash}`,
+ {
+ headers: {
+ Authorization: authHeader,
+ Accept: "application/json"
+ }
}
- });
+ );
// eslint-disable-next-line no-continue
if (!diffstat.values) continue;
@@ -235,7 +245,7 @@ export const BitbucketSecretScanningFactory = () => {
// eslint-disable-next-line no-await-in-loop
const { data: patch } = await request.get<string>(
- `https://api.bitbucket.org/2.0/repositories/${repository.full_name}/diff/${commit.hash}`,
+ `https://api.bitbucket.org/2.0/repositories/${encodeURIComponent(repository.full_name)}/diff/${commit.hash}`,
{
params: {
path: filePath
diff --git a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
index 2cabb7466c..00a3b334e2 100644
--- a/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
+++ b/backend/src/ee/services/secret-scanning-v2/secret-scanning-v2-fns.ts
@@ -1,5 +1,5 @@
import { AxiosError } from "axios";
-import { exec } from "child_process";
+import { execFile } from "child_process";
import { join } from "path";
import picomatch from "picomatch";
import RE2 from "re2";
@@ -33,9 +33,13 @@ export const listSecretScanningDataSourceOptions = () => {
};
export const cloneRepository = async ({ cloneUrl, repoPath }: TCloneRepository): Promise<void> => {
- const command = `git clone ${cloneUrl} ${repoPath} --bare`;
+ // Validate that the constructed URL is structurally valid.
+ // This prevents malformed or tampered components from producing unexpected git behavior.
+ // eslint-disable-next-line no-new
+ new URL(cloneUrl);
+
return new Promise((resolve, reject) => {
- exec(command, (error) => {
+ execFile("git", ["clone", cloneUrl, repoPath, "--bare"], (error) => {
if (error) {
reject(error);
} else {
@@ -47,8 +51,11 @@ export const cloneRepository = async ({ cloneUrl, repoPath }: TCloneRepository):
export function scanDirectory(inputPath: string, outputPath: string, configPath?: string): Promise<void> {
return new Promise((resolve, reject) => {
- const command = `cd ${inputPath} && infisical scan --exit-code=77 -r "${outputPath}" ${configPath ? `-c ${configPath}` : ""}`;
- exec(command, (error) => {
+ const args = ["scan", "--exit-code=77", "-r", outputPath];
+ if (configPath) {
+ args.push("-c", configPath);
+ }
+ execFile("infisical", args, { cwd: inputPath }, (error) => {
if (error && error.code !== 77) {
reject(error);
} else {
@@ -60,8 +67,11 @@ export function scanDirectory(inputPath: string, outputPath: string, configPath?
export function scanFile(inputPath: string, configPath?: string): Promise<void> {
return new Promise((resolve, reject) => {
- const command = `infisical scan --exit-code=77 --source "${inputPath}" --no-git ${configPath ? `-c ${configPath}` : ""}`;
- exec(command, (error) => {
+ const args = ["scan", "--exit-code=77", "--source", inputPath, "--no-git"];
+ if (configPath) {
+ args.push("-c", configPath);
+ }
+ execFile("infisical", args, (error) => {
if (error && error.code === 77) {
reject(error);
} else {
diff --git a/backend/src/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-fns.ts b/backend/src/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-fns.ts
index ceb239adf2..94d699c7fd 100644
--- a/backend/src/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-fns.ts
+++ b/backend/src/ee/services/secret-scanning/secret-scanning-queue/secret-scanning-fns.ts
@@ -1,5 +1,5 @@
import { Octokit } from "@octokit/rest";
-import { exec } from "child_process";
+import { execFile } from "child_process";
import { mkdir, readFile, rm, writeFile } from "fs";
import { tmpdir } from "os";
import { join } from "path";
@@ -40,9 +40,12 @@ export async function cloneRepo(
repoPath: string
): Promise<void> {
const cloneUrl = `https://x-access-token:${installationAcccessToken}@github.com/${repositoryFullName}.git`;
- const command = `git clone ${cloneUrl} ${repoPath} --bare`;
+
+ // eslint-disable-next-line no-new
+ new URL(cloneUrl);
+
return new Promise((resolve, reject) => {
- exec(command, (error) => {
+ execFile("git", ["clone", cloneUrl, repoPath, "--bare"], (error) => {
if (error) {
reject(error);
} else {
@@ -54,8 +57,7 @@ export async function cloneRepo(
export function runInfisicalScanOnRepo(repoPath: string, outputPath: string): Promise<void> {
return new Promise((resolve, reject) => {
- const command = `cd ${repoPath} && infisical scan --exit-code=77 -r "${outputPath}"`;
- exec(command, (error) => {
+ execFile("infisical", ["scan", "--exit-code=77", "-r", outputPath], { cwd: repoPath }, (error) => {
if (error && error.code !== 77) {
reject(error);
} else {
@@ -67,8 +69,11 @@ export function runInfisicalScanOnRepo(repoPath: string, outputPath: string): Pr
export function runInfisicalScan(inputPath: string, outputPath: string, configPath?: string): Promise<void> {
return new Promise((resolve, reject) => {
- const command = `cat "${inputPath}" | infisical scan --exit-code=77 --pipe -r "${outputPath}" ${configPath ? `-c "${configPath}"` : ""}`;
- exec(command, (error) => {
+ const args = ["scan", "--exit-code=77", "--source", inputPath, "--no-git", "-r", outputPath];
+ if (configPath) {
+ args.push("-c", configPath);
+ }
+ execFile("infisical", args, (error) => {
if (error && error.code !== 77) {
reject(error);
} else {