The question is, can anyone access every field in my classes, this is unsafe, right?
Not everyone can. Only code with sufficient permissions is trusted code. Insufficient code is limited. On the other hand, if a person who wants to use reflection has his own assembly, they can run trusted code on their own machine. This is not a new attack vector, although as if they have their own code, they can also modify it to make the field public in the first place.
Basically, if the code runs on their machine, you should expect them to be able to do something with it. Do not rely on access modifiers to keep something secret.
So, how can I make my application safe, which means how I forbid anyone other than me from changing important status values inside my classes?
If a hostile user runs your code on their own, you pretty much can't. You can make it harder for them, but this is an arms race that is not fun.
Thus, one option in some cases is not to let anyone else run your code - post it on the Internet in an environment that you have blocked. Of course, this is not suitable in all cases.
If you must allow users to run the code itself, you need to weigh the disadvantages associated with their unauthorized costs, which make this intervention difficult to do. We cannot help you in this balancing action - we have no idea what your application is, or which of them are related (reputational, financial, etc.).
source share