Skip to content

Do some silly changes to introduce vulns in a few projects #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion .github/workflows/codeql-monorepo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ jobs:
project: ${{ fromJson(needs.changes.outputs.projects).projects }}
steps:
- name: Analyze code
uses: advanced-security/monorepo-code-scanning-action/scan@main
uses: advanced-security/monorepo-code-scanning-action/scan@annotate-sarif
# If you have a custom analysis workflow defined at .github/workflows/custom-codeql-analysis.yml, then set this to 'true' so that it is run.
# custom-analysis: true

Expand Down
7 changes: 7 additions & 0 deletions packages/babel-cli/src/babel/dir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
fs.writeFileSync(filePath, data);
}

function insecurePassword(): string {
// BAD: the random suffix is not cryptographically secure
const suffix = Math.random();
const password = "myPassword" + suffix;

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix

AI 4 months ago

To fix the problem, we need to replace the use of Math.random() with a cryptographically secure random number generator. In Node.js, we can use the crypto module's randomBytes method to generate secure random values. We will convert these bytes to a number and use it as the suffix for the password.

  • Import the crypto module.
  • Replace the Math.random() call with a secure random number generation using crypto.randomBytes.
  • Ensure the generated random number is in the desired range.
Suggested changeset 1
packages/babel-cli/src/babel/dir.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/babel-cli/src/babel/dir.ts b/packages/babel-cli/src/babel/dir.ts
--- a/packages/babel-cli/src/babel/dir.ts
+++ b/packages/babel-cli/src/babel/dir.ts
@@ -3,3 +3,3 @@
 import fs from "fs";
-
+import crypto from "crypto";
 import * as util from "./util.ts";
@@ -21,4 +21,4 @@
 function insecurePassword(): string {
-  // BAD: the random suffix is not cryptographically secure
-  const suffix = Math.random();
+  // GOOD: the random suffix is cryptographically secure
+  const suffix = crypto.randomBytes(4).readUInt32BE(0);
   const password = "myPassword" + suffix;
EOF
@@ -3,3 +3,3 @@
import fs from "fs";

import crypto from "crypto";
import * as util from "./util.ts";
@@ -21,4 +21,4 @@
function insecurePassword(): string {
// BAD: the random suffix is not cryptographically secure
const suffix = Math.random();
// GOOD: the random suffix is cryptographically secure
const suffix = crypto.randomBytes(4).readUInt32BE(0);
const password = "myPassword" + suffix;
Copilot is powered by AI and may make mistakes. Always verify output.
return password;
}

export default async function ({
cliOptions,
babelOptions,
Expand Down
7 changes: 7 additions & 0 deletions packages/babel-helpers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@
}
}

function insecurePassword(): string {
// BAD: the random suffix is not cryptographically secure
const suffix = Math.random();
const password = "myPassword" + suffix;

Check failure

Code scanning / CodeQL

Insecure randomness High

This uses a cryptographically insecure random number generated at
Math.random()
in a security context.

Copilot Autofix

AI 4 months ago

To fix the problem, we need to replace the use of Math.random() with a cryptographically secure random number generator. In a Node.js environment, we can use the crypto module's randomBytes method to generate a secure random number. This method provides cryptographically strong pseudo-random data.

We will:

  1. Import the crypto module.
  2. Replace the Math.random() call with a call to crypto.randomBytes.
  3. Convert the random bytes to a number or string that can be appended to the password.
Suggested changeset 1
packages/babel-helpers/src/index.ts

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/packages/babel-helpers/src/index.ts b/packages/babel-helpers/src/index.ts
--- a/packages/babel-helpers/src/index.ts
+++ b/packages/babel-helpers/src/index.ts
@@ -4,2 +4,3 @@
 import type { HelperMetadata } from "./helpers-generated.ts";
+import { randomBytes } from "crypto";
 
@@ -27,4 +28,4 @@
 function insecurePassword(): string {
-  // BAD: the random suffix is not cryptographically secure
-  const suffix = Math.random();
+  // GOOD: the random suffix is cryptographically secure
+  const suffix = randomBytes(4).toString('hex');
   const password = "myPassword" + suffix;
EOF
@@ -4,2 +4,3 @@
import type { HelperMetadata } from "./helpers-generated.ts";
import { randomBytes } from "crypto";

@@ -27,4 +28,4 @@
function insecurePassword(): string {
// BAD: the random suffix is not cryptographically secure
const suffix = Math.random();
// GOOD: the random suffix is cryptographically secure
const suffix = randomBytes(4).toString('hex');
const password = "myPassword" + suffix;
Copilot is powered by AI and may make mistakes. Always verify output.
return password;
}

type AdjustAst = (
ast: t.Program,
exportName: string,
Expand Down
Loading