Skip to content

Feature/api client flavor#135

Open
justinmakaila wants to merge 28 commits intomainfrom
feature/api-client-flavor
Open

Feature/api client flavor#135
justinmakaila wants to merge 28 commits intomainfrom
feature/api-client-flavor

Conversation

@justinmakaila
Copy link
Copy Markdown
Contributor

No description provided.

async function hashPassword(email: string, password: string) {
return crypto
.createHash("sha256")
.update(password + email.toLowerCase())

Check failure

Code scanning / CodeQL

Use of password hash with insufficient computational effort High

Password from
an access to password
is hashed insecurely.
Password from
an access to IRACING_AUTH_PASSWORD
is hashed insecurely.
Password from
an access to password
is hashed insecurely.

Copilot Autofix

AI 6 months ago

To fix this problem, replace the insecure use of crypto.createHash('sha256') for password hashing with a secure password hashing function, such as bcrypt. This entails updating the hashPassword function to use bcrypt.hashSync or its async variant. Given that you must not change behavior outside the provided snippet, and only the code in this file, you should implement the secure hash just within the hashPassword function. You will need to import bcrypt at the top. The code must generate a salt (or accept one as input), as recommended for bcrypt. You should ensure consistency with how hashes are generated and used later (e.g., replacing the call on line 46 to use the securely hashed password).

Updates include:

  • Add bcrypt import.
  • Update the hashPassword function to use bcrypt (asynchronously, since original use is with await).
  • (Optionally) Generate a salt within the function if not externally supplied.

Suggested changeset 2
apps/sync-car-assets-cli/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/apps/sync-car-assets-cli/src/index.ts b/apps/sync-car-assets-cli/src/index.ts
--- a/apps/sync-car-assets-cli/src/index.ts
+++ b/apps/sync-car-assets-cli/src/index.ts
@@ -1,6 +1,7 @@
 #!/usr/bin/env node
 
 import crypto from "node:crypto";
+import bcrypt from "bcrypt";
 import { Command } from "@commander-js/extra-typings";
 import { CarApi, Configuration } from "@iracing-data/api-client-fetch";
 import { syncCarAssets } from "@iracing-data/sync-car-assets";
@@ -11,10 +12,9 @@
 dotenv.config();
 
 async function hashPassword(email: string, password: string) {
-  return crypto
-    .createHash("sha256")
-    .update(password + email.toLowerCase())
-    .digest("base64");
+  const saltRounds = 12;
+  // Use bcrypt to securely hash password; include email in hash input if required for protocol.
+  return await bcrypt.hash(password + email.toLowerCase(), saltRounds);
 }
 
 const program = new Command("sync-iracing-car-assets")
EOF
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import crypto from "node:crypto";
import bcrypt from "bcrypt";
import { Command } from "@commander-js/extra-typings";
import { CarApi, Configuration } from "@iracing-data/api-client-fetch";
import { syncCarAssets } from "@iracing-data/sync-car-assets";
@@ -11,10 +12,9 @@
dotenv.config();

async function hashPassword(email: string, password: string) {
return crypto
.createHash("sha256")
.update(password + email.toLowerCase())
.digest("base64");
const saltRounds = 12;
// Use bcrypt to securely hash password; include email in hash input if required for protocol.
return await bcrypt.hash(password + email.toLowerCase(), saltRounds);
}

const program = new Command("sync-iracing-car-assets")
apps/sync-car-assets-cli/package.json
Outside changed files

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/apps/sync-car-assets-cli/package.json b/apps/sync-car-assets-cli/package.json
--- a/apps/sync-car-assets-cli/package.json
+++ b/apps/sync-car-assets-cli/package.json
@@ -18,7 +18,8 @@
     "@iracing-data/api-client-fetch": "workspace:*",
     "@iracing-data/sync-car-assets": "workspace:*",
     "commander": "^14.0.2",
-    "openapi-fetch": "^0.15.0"
+    "openapi-fetch": "^0.15.0",
+    "bcrypt": "^6.0.0"
   },
   "devDependencies": {
     "@commander-js/extra-typings": "^14.0.0",
EOF
@@ -18,7 +18,8 @@
"@iracing-data/api-client-fetch": "workspace:*",
"@iracing-data/sync-car-assets": "workspace:*",
"commander": "^14.0.2",
"openapi-fetch": "^0.15.0"
"openapi-fetch": "^0.15.0",
"bcrypt": "^6.0.0"
},
"devDependencies": {
"@commander-js/extra-typings": "^14.0.0",
This fix introduces these dependencies
Package Version Security advisories
bcrypt (npm) 6.0.0 None
Copilot is powered by AI and may make mistakes. Always verify output.
export async function hashPassword(email: string, password: string) {
return crypto
.createHash("sha256")
.update(password + email.toLowerCase())

Check failure

Code scanning / CodeQL

Use of password hash with insufficient computational effort High

Password from
an access to password
is hashed insecurely.
Password from
an access to IRACING_AUTH_PASSWORD
is hashed insecurely.
Password from
an access to password
is hashed insecurely.

Copilot Autofix

AI 6 months ago

To resolve this issue, we need to replace the use of the general-purpose SHA-256 hashing algorithm for password hashing with a computationally expensive password hashing scheme, such as bcrypt. This change should be done within the hashPassword function in apps/sync-track-assets-cli/src/index.ts (lines 16–21). We will use the bcrypt library, which is a widely accepted method for password hashing in Node.js applications. This will involve:

  • Installing and importing the bcrypt package (not shown in other parts, so we’ll just add the import line shown).
  • Modifying the hashPassword function to use bcrypt.hash() (the async version, since the function is already marked async).
  • Deciding on a salt factor (e.g., 12 is a good default).
  • Removing the manual concatenation with the email, unless required by the downstream API (if needed, can append to the password before passing to bcrypt).
  • Ensuring the rest of the code continues to treat the hash as it did previously (as a string).

All changes should be made only to apps/sync-track-assets-cli/src/index.ts, with sufficient context to clearly identify the changed lines (i.e., lines 16-21 must be replaced; also add the import for bcrypt).


Suggested changeset 1
apps/sync-track-assets-cli/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/apps/sync-track-assets-cli/src/index.ts b/apps/sync-track-assets-cli/src/index.ts
--- a/apps/sync-track-assets-cli/src/index.ts
+++ b/apps/sync-track-assets-cli/src/index.ts
@@ -1,6 +1,7 @@
 #!/usr/bin/env node
 
 import crypto from "node:crypto";
+import bcrypt from "bcrypt";
 import { Command } from "@commander-js/extra-typings";
 import { Configuration, TrackApi } from "@iracing-data/api-client-fetch";
 import { syncTrackAssets } from "@iracing-data/sync-track-assets";
@@ -14,10 +15,10 @@
  * Compute the Base64‑encoded SHA‑256 hash of (password + email.toLowerCase()).
  */
 export async function hashPassword(email: string, password: string) {
-  return crypto
-    .createHash("sha256")
-    .update(password + email.toLowerCase())
-    .digest("base64");
+  // It’s typical to use just the password, but if the API requires email+password, keep that
+  const saltRounds = 12;
+  const toHash = password + email.toLowerCase();
+  return await bcrypt.hash(toHash, saltRounds);
 }
 
 const program = new Command("sync-iracing-track-assets")
EOF
@@ -1,6 +1,7 @@
#!/usr/bin/env node

import crypto from "node:crypto";
import bcrypt from "bcrypt";
import { Command } from "@commander-js/extra-typings";
import { Configuration, TrackApi } from "@iracing-data/api-client-fetch";
import { syncTrackAssets } from "@iracing-data/sync-track-assets";
@@ -14,10 +15,10 @@
* Compute the Base64‑encoded SHA‑256 hash of (password + email.toLowerCase()).
*/
export async function hashPassword(email: string, password: string) {
return crypto
.createHash("sha256")
.update(password + email.toLowerCase())
.digest("base64");
// It’s typical to use just the password, but if the API requires email+password, keep that
const saltRounds = 12;
const toHash = password + email.toLowerCase();
return await bcrypt.hash(toHash, saltRounds);
}

const program = new Command("sync-iracing-track-assets")
Copilot is powered by AI and may make mistakes. Always verify output.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants