Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

309
Visualizações
How to properly handle ESLint 'detect-object-injection'

In our angular project the ESLint shout:

"Detects variable[key] as a left- or right-hand assignment operand."

It is detect-object-injection rule. And says that:

//Bad:
variable[key] = value;

And as a proof provide the following link with description.

I'm not going to start to argue that this rule is sucks.

But I'm interested how to fix it. The link about has the solution:

The most direct fix here is going to be to avoid the use of user input in property name fields. This isn't reasonable in all circumstances, however, and there should be a way to safely use core language features.

Another option is to create a whitelist of allowed property names, and filter each user input through a helper function to check before allowing it to be used. This is a great option in situations where you know specifically what property names to allow.

In cases where you don't have a strictly defined data model ( which isn't ideal, but there are cases where it has to be so ) then using the same method as above, but with a blacklist of disallowed properties instead is a valid choice.

So the first option "do not use it" is not acceptable as my key is dynamic and the bracket notation is the only way to access the property.

The second solution is also is not suite for me as I don't have and cannot create a whitelist of allowed property names.

So the last option which I have is to use a blacklist of disallowed properties. But the question is: where to take this blacklist and how to apply it? It doesn't have an example and I'm interested how to properly implement it in Angular.

Thanks in advance!

UPDATE: As my question is not totally clear, I'll explain one more time.

The code I have is:

someObject[someDynamicKey] = 'someValue';

The problem that the eslint treat this line as bad. Because of setting the object property by Square Bracket Notation. EsLint says that it's dangerous. So I ask how should we do if the is no possibility to access the object property with dot notation? Examples? Thanks!

Explanation why Square Bracket Notation is Dangers according to ESLint.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

It looks like you are using an object as a key/value store, which is usually associated with a Map.

Use a map instead since it won't be touching object properties and the keys will not collide with an object's native properties.

const variable = new Map();
...

variable.set(key, value);
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda