1
votes

Problem :

Entity id decorated @PrimaryGeneratedColumn() is overwritable on put request.

Expected behaviour :

The id decorated with @PrimaryGeneratedColumn() shall not update.

Steps to reproduce :

  1. Create an entry with this post request :
curl --location --request POST 'https://q6zre.sse.codesandbox.io/tests' \
--header 'Content-Type: application/json' \
--data-raw '{"name": "acme"}'
  1. Copy the id from the response, add it in the put request url, and add a random wishedId in the body :
curl --location --request PUT 'https://q6zre.sse.codesandbox.io/tests/{id}' \
--header 'Content-Type: application/json' \
--data-raw '{"id": "{wishedId}"}'

The entry is updated with the wishedId.

Working example : https://codesandbox.io/s/github/desirelabs/nest-typeorm

Is there any way to prevent this ? Thanks

1

1 Answers

0
votes

I would say this is less of a TypeORM problem and more of an architecture problem. You should consider adding an Anti-corruption Layer or DDD: Anti-corruption Layer, etc. to your architecture. The pattern comes from Domain Driven Design, and the basic idea is that your REST layer should not be directly influenced by your Database layer, and there should be mapping going on in between the two to help enforce business requirements. You are using TypeORM to do that mapping, and I'm suggesting you should write your own mapping to defend your data integrity from the outside world.