It is common to hear that strings are immutable and this improves security. I understand the idea that since strings are final, their contents cannot be changed later. But even if the content could be changed, i think it is still safe as the code is written by developer instead of attacker. Or else in practice, how does this attack are being done actually?
I saw an example online that indicated an attacker could bypass security if strings were mutable. I don't get it. The below code is written by service provider. This is the part attacker can never touch. Whether strings are mutable or not attackers can never modify their values, right?
public class FileInputStream
{
private String filename;
public FileInputStream(String filename)
{
if (!allowedToReadFile(filename))
throw new SecurityException();
this.filename = filename;
}
...
}