-
-
Notifications
You must be signed in to change notification settings - Fork 56
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
[chore]: port to official @iobroker/eslint-config
config
#2898
Conversation
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
import { maybeCallback, maybeCallbackWithError, maybeCallbackWithRedisError } from '@/lib/common/maybeCallback.js'; | ||
|
||
// ============================================================ | ||
// maybeCallbackWithError => Callback | ||
|
||
async () => { | ||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -29,7 +28,7 @@ | |||
maybeCallbackWithError(cb, 'this is an error', 'why', 'are', 'you', 'calling', 'me', 'with', 'arguments'); | |||
}; | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -52,14 +51,14 @@ | |||
maybeCallbackWithError(cb, 'this is an error', 'why', 'are', 'you', 'calling', 'me', 'with', 'arguments'); | |||
}; | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
// maybeCallbackWithError, callback only takes an argument that's not compatible with Error | ||
const cb = (err?: number): void => {}; | ||
// @ts-expect-error | ||
maybeCallbackWithError(cb, null); | ||
}; | ||
|
||
async () => { | ||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -96,7 +95,7 @@ | |||
// ============================================================ | |||
// maybeCallbackWithError => Promise | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -199,14 +198,14 @@ | |||
maybeCallbackWithRedisError(cb, 'this is an error', 'why', 'are', 'you', 'calling', 'me', 'with', 'arguments'); | |||
}; | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
// maybeCallbackWithRedisError, callback only takes an argument that's not compatible with Error | ||
const cb = (err?: number): void => {}; | ||
// @ts-expect-error | ||
maybeCallbackWithRedisError(cb, null); | ||
}; | ||
|
||
async () => { | ||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -243,7 +242,7 @@ | |||
// ============================================================ | |||
// maybeCallback => Callback | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -255,7 +254,7 @@ | |||
maybeCallback(cb, 'why', 'are', 'you', 'calling', 'me', 'with', 'arguments'); | |||
}; | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
@@ -277,7 +276,7 @@ | |||
// ============================================================ | |||
// maybeCallback => Promise | |||
|
|||
async () => { | |||
() => { |
Check warning
Code scanning / CodeQL
Expression has no effect Warning
a9eb8b5
to
8c00e6e
Compare
@@ -1286,13 +1286,13 @@ | |||
async _enumerateAdapterStateObjects(knownObjIDs: string[], adapter: string, instance?: number): Promise<void> { | |||
const adapterRegex = new RegExp(`^${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`); | |||
const sysAdapterRegex = new RegExp( | |||
`^system\\.adapter\\.${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.` | |||
`^system\\.adapter\\.${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`, |
Check failure
Code scanning / CodeQL
Regular expression injection High
command-line argument
@@ -1354,7 +1354,7 @@ | |||
private async _enumerateAdapterDocs(knownObjIDs: string[], adapter: string, instance?: number): Promise<void> { | |||
const adapterRegex = new RegExp(`^${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`); | |||
const sysAdapterRegex = new RegExp( | |||
`^system\\.adapter\\.${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.` | |||
`^system\\.adapter\\.${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`, |
Check failure
Code scanning / CodeQL
Regular expression injection High
command-line argument
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 6 months ago
To fix the problem, we need to sanitize the adapter
variable before using it to construct the regular expression. The best way to do this is by using the _.escapeRegExp
function from the lodash library, which escapes special characters in the string, making it safe to use in a regular expression.
- Import the
lodash
library. - Use the
_.escapeRegExp
function to sanitize theadapter
variable before constructing the regular expression.
-
Copy modified line R27 -
Copy modified lines R1350-R1351 -
Copy modified line R1353
@@ -26,2 +26,3 @@ | ||
import { createRequire } from 'node:module'; | ||
import _ from 'lodash'; | ||
|
||
@@ -1348,5 +1349,6 @@ | ||
private async _enumerateAdapterDocs(knownObjIDs: string[], adapter: string, instance?: number): Promise<void> { | ||
const adapterRegex = new RegExp(`^${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`); | ||
const safeAdapter = _.escapeRegExp(adapter); | ||
const adapterRegex = new RegExp(`^${safeAdapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`); | ||
const sysAdapterRegex = new RegExp( | ||
`^system\\.adapter\\.${adapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`, | ||
`^system\\.adapter\\.${safeAdapter}${instance !== undefined ? `\\.${instance}` : ''}\\.`, | ||
); |
-
Copy modified lines R22-R23
@@ -21,3 +21,4 @@ | ||
"semver": "^7.5.2", | ||
"yargs": "^17.6.2" | ||
"yargs": "^17.6.2", | ||
"lodash": "^4.17.21" | ||
}, |
Package | Version | Security advisories |
lodash (npm) | 4.17.21 | None |
@@ -40,7 +40,7 @@ | |||
export class Upload { | |||
private readonly states: StatesRedisClient; | |||
private readonly objects: ObjectsRedisClient; | |||
private readonly regApp = new RegExp('/' + tools.appName.replace(/\./g, '\\.') + '\\.', 'i'); | |||
private readonly regApp = new RegExp(`/${tools.appName.replace(/\./g, '\\.')}\\.`, 'i'); |
Check failure
Code scanning / CodeQL
Incomplete string escaping or encoding High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 6 months ago
To fix the problem, we need to ensure that all backslashes in the tools.appName
string are properly escaped before replacing periods with escaped periods. This can be achieved by first replacing all backslashes with double backslashes and then replacing periods with escaped periods. This ensures that the resulting string is safe to use in a regular expression.
- Modify the
tools.appName.replace
call to first escape backslashes. - Ensure that the regular expression is constructed correctly with the properly escaped string.
-
Copy modified line R43
@@ -42,3 +42,3 @@ | ||
private readonly objects: ObjectsRedisClient; | ||
private readonly regApp = new RegExp(`/${tools.appName.replace(/\./g, '\\.')}\\.`, 'i'); | ||
private readonly regApp = new RegExp(`/${tools.appName.replace(/\\/g, '\\\\').replace(/\./g, '\\.')}\\.`, 'i'); | ||
private callbackId = 1; |
function _emit_(id, obj) { | ||
result.rows.push({ id: id, value: obj }); | ||
} | ||
|
||
const f = eval('(' + func.map.replace(/emit/g, '_emit_') + ')'); | ||
const f = eval(`(${func.map.replace(/emit/g, '_emit_')})`); |
Check failure
Code scanning / CodeQL
Code injection Critical
user-provided value
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 6 months ago
To fix the code injection vulnerability, we should avoid using eval
to execute user-provided data. Instead, we can use the Function
constructor to create a new function in a safer manner. This approach allows us to control the scope and avoid executing arbitrary code.
- Replace the
eval
function with theFunction
constructor. - Ensure that the new function is created in a controlled environment to prevent code injection.
- Update the
_applyView
method to use theFunction
constructor.
-
Copy modified line R953
@@ -952,3 +952,3 @@ | ||
|
||
const f = eval(`(${func.map.replace(/emit/g, '_emit_')})`); | ||
const f = new Function('_emit_', `return (${func.map.replace(/emit/g, '_emit_')});`)(_emit_); | ||
|
Implementation details
We now have a centralized eslint config at
@iobroker/eslint-config
Adding it to controller was a first step for checking the required rules, there might be changes later on but if they differ to much we will make a separate export for controller and adapters.
Some rules are a bit stricter than before but it now also uses more TS knowledge and can e.g. auto remove unnecessary non-null assertions, unnecessary type casts etc.