Skip to content
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

fix(vault): fix default network id setting #222

Merged
merged 1 commit into from
Nov 6, 2024

Conversation

mrcnk
Copy link
Member

@mrcnk mrcnk commented Nov 6, 2024

Describe changes

Ticket or discussion link

Review checklist

  • Proper documentation added
  • Proper tests added

Screenshots

Copy link

deepsource-io bot commented Nov 6, 2024

Here's the code health analysis summary for commits 2f43e8d..7343baa. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource JavaScript LogoJavaScript✅ Success
❗ 1 occurence introduced
🎯 1 occurence resolved
View Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

set(
produce((state) => {
state.accounts[network][address] = {
state.accounts[networkId][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.

Copilot Autofix AI 2 months ago

To fix the prototype pollution issue, we need to ensure that the networkId parameter cannot be set to special property names like __proto__, constructor, or prototype. We can achieve this by adding a check to reject these values before using them as keys in the state.accounts object.

The best way to fix this without changing existing functionality is to add a validation step for networkId in each function where it is used. If networkId is one of the special property names, we can return early or throw an error.

Suggested changeset 1
packages/vault/src/account/accountStore.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/vault/src/account/accountStore.ts b/packages/vault/src/account/accountStore.ts
--- a/packages/vault/src/account/accountStore.ts
+++ b/packages/vault/src/account/accountStore.ts
@@ -21,2 +21,5 @@
   ensureAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -36,2 +39,5 @@
   setAccountInfo: (networkId, address, accountInfo) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     const { accounts } = get()
@@ -48,2 +54,5 @@
   setTransactions: (networkId, address, transactions) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     const { accounts } = get()
@@ -60,2 +69,5 @@
   addAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -72,2 +84,5 @@
   removeAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
EOF
@@ -21,2 +21,5 @@
ensureAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -36,2 +39,5 @@
setAccountInfo: (networkId, address, accountInfo) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
const { accounts } = get()
@@ -48,2 +54,5 @@
setTransactions: (networkId, address, transactions) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
const { accounts } = get()
@@ -60,2 +69,5 @@
addAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -72,2 +84,5 @@
removeAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
set(
produce((state) => {
state.accounts[network][address] = {
state.accounts[networkId][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.

Copilot Autofix AI 2 months ago

To fix the prototype pollution vulnerability, we need to ensure that networkId cannot be set to special property names like __proto__, constructor, or prototype. One way to achieve this is by adding a check to reject these values before using them as keys. Alternatively, we can use a Map object instead of a plain object to store the accounts, as Map does not have the same prototype pollution risks.

The best way to fix the problem without changing existing functionality is to add a check to reject special property names. This approach is straightforward and does not require significant changes to the existing code structure.

Suggested changeset 1
packages/vault/src/account/accountStore.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/vault/src/account/accountStore.ts b/packages/vault/src/account/accountStore.ts
--- a/packages/vault/src/account/accountStore.ts
+++ b/packages/vault/src/account/accountStore.ts
@@ -21,2 +21,5 @@
   ensureAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -48,2 +51,5 @@
   setTransactions: (networkId, address, transactions) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     const { accounts } = get()
@@ -60,2 +66,5 @@
   addAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -72,2 +81,5 @@
   removeAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
EOF
@@ -21,2 +21,5 @@
ensureAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -48,2 +51,5 @@
setTransactions: (networkId, address, transactions) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
const { accounts } = get()
@@ -60,2 +66,5 @@
addAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -72,2 +81,5 @@
removeAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
state.accounts[network][address] = {
if (!state.accounts?.[networkId]?.[address]) {
state.accounts[networkId] = state.accounts[networkId] || {}
state.accounts[networkId][address] = {

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.

Copilot Autofix AI 2 months ago

To fix the problem, we need to ensure that networkId cannot be used to manipulate the prototype of the object. One effective way to achieve this is by validating networkId to ensure it does not contain any dangerous values like __proto__, constructor, or prototype. Alternatively, we can use a Map object instead of a plain object to store the accounts, as Map is not vulnerable to prototype pollution.

The best way to fix the problem without changing existing functionality is to validate networkId before using it as a key. We will add a check to ensure networkId is not one of the dangerous values.

Suggested changeset 1
packages/vault/src/account/accountStore.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/vault/src/account/accountStore.ts b/packages/vault/src/account/accountStore.ts
--- a/packages/vault/src/account/accountStore.ts
+++ b/packages/vault/src/account/accountStore.ts
@@ -21,2 +21,5 @@
   ensureAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -36,2 +39,5 @@
   setAccountInfo: (networkId, address, accountInfo) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     const { accounts } = get()
@@ -48,2 +54,5 @@
   setTransactions: (networkId, address, transactions) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     const { accounts } = get()
@@ -60,2 +69,5 @@
   addAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
@@ -72,2 +84,5 @@
   removeAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      throw new Error('Invalid networkId');
+    }
     set(
EOF
@@ -21,2 +21,5 @@
ensureAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -36,2 +39,5 @@
setAccountInfo: (networkId, address, accountInfo) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
const { accounts } = get()
@@ -48,2 +54,5 @@
setTransactions: (networkId, address, transactions) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
const { accounts } = get()
@@ -60,2 +69,5 @@
addAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
@@ -72,2 +84,5 @@
removeAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
throw new Error('Invalid networkId');
}
set(
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
set(
produce((state) => {
delete state.accounts[network][address]
delete state.accounts[networkId][address]

Check warning

Code scanning / CodeQL

Prototype-polluting assignment Medium

This assignment may alter Object.prototype if a malicious '__proto__' string is injected from
library input
.

Copilot Autofix AI 2 months ago

To fix the prototype pollution vulnerability, we need to ensure that the networkId parameter cannot be used to modify Object.prototype. This can be achieved by validating the networkId and rejecting any values that could lead to prototype pollution, such as __proto__, constructor, or prototype.

The best way to fix this without changing existing functionality is to add a validation check for networkId before it is used as a key in the state.accounts object. If networkId is one of the restricted values, we should return early or handle it appropriately.

Suggested changeset 1
packages/vault/src/account/accountStore.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/vault/src/account/accountStore.ts b/packages/vault/src/account/accountStore.ts
--- a/packages/vault/src/account/accountStore.ts
+++ b/packages/vault/src/account/accountStore.ts
@@ -21,2 +21,5 @@
   ensureAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      return;
+    }
     set(
@@ -48,2 +51,5 @@
   setTransactions: (networkId, address, transactions) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      return;
+    }
     const { accounts } = get()
@@ -60,2 +66,5 @@
   addAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      return;
+    }
     set(
@@ -72,2 +81,5 @@
   removeAccount: (networkId, address) => {
+    if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
+      return;
+    }
     set(
EOF
@@ -21,2 +21,5 @@
ensureAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
return;
}
set(
@@ -48,2 +51,5 @@
setTransactions: (networkId, address, transactions) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
return;
}
const { accounts } = get()
@@ -60,2 +66,5 @@
addAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
return;
}
set(
@@ -72,2 +81,5 @@
removeAccount: (networkId, address) => {
if (networkId === '__proto__' || networkId === 'constructor' || networkId === 'prototype') {
return;
}
set(
Copilot is powered by AI and may make mistakes. Always verify output.
Positive Feedback
Negative Feedback

Provide additional feedback

Please help us improve GitHub Copilot by sharing more details about this comment.

Please select one or more of the options
@mrcnk mrcnk merged commit 2bc5d5c into main Nov 6, 2024
4 checks passed
@mrcnk mrcnk deleted the fix/patch-default-network-id-setting branch November 6, 2024 14:34
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.

1 participant