0

I recently ran static code analysis in Visual Studio on a solution and saw a line get flagged for CA2104. Mutable object marked as ReadOnly. I understand why it doesn't make sense to have a mutable type set as ReadOnly, since the properties on the object can change, but I don't understand why this is considered a security problem.

This seems more like a data integrity/code quality problem. The only security related thing that comes to mind is if the user was able to somehow change the properties, they could potentially make the object behave in a different manner, but wouldn't this be true for any mutable object? What am I missing?

Edit: I see that this was marked as a possible duplicate. I read through the linked question, but I do not feel that this answers my question. I understand what the CA2104 warning is saying, however I don't understand why this is categorized as a security issue. This is what I am trying to understand.

Dave
  • 2,473
  • 2
  • 30
  • 55
  • Possible duplicate of [Do not declare read only mutable reference types - why not?](https://stackoverflow.com/questions/2370268/do-not-declare-read-only-mutable-reference-types-why-not) – Rubens Farias Mar 20 '18 at 21:29

1 Answers1

0

Having read the documentation on this. I believe that the warning is raised because although the property is marked read-only. There's nothing preventing the user from changing the properties on the read-only property.

Where user would be the consumer of the assembly/library containing the parent type.

Since the property is read-only it's probably been set because the author doesn't want the property to be changed/re-assigned but there's nothing to prevent any of the properties on the child object from being changed. Which is counter to what read-only means. Hence the warning

I suppose it's a tenuous link to security but still valid if the library author wants to protect the exposed object without allowing it to be modified but that's not what is actually happening.

phuzi
  • 12,078
  • 3
  • 26
  • 50