Just have two separate types, e.g. UnsafeString and regular String, and some kind of `convert` function that takes a validation function as an argument. You'd get compile-time checking that way.
People don't tend to use such things in practice though, and you would also have to ban a portion of most language's standard libraries to enforce it in practice (because they already return regular strings for inputs).
We use something similar where we have a BadNumber class in our code (python). Any operation with another number will also create a BadNumber. It allows us to make sure that these tainted numbers are always obvious.
Though I don't have experience with it, maybe one reason it isn't used is because of false positives?
For efficiency reasons, Perl takes a conservative view of whether data is tainted. If an expression contains tainted data, any subexpression may be considered tainted, even if the value of the subexpression is not itself affected by the tainted data.
Has anyone used this? Is the runtime overhead always there, or only when you turn the taint mode on? It seems like it would have to occupy some extra space in the string objects all the time? (Although I guess if it's literally a single bit, it can come for "free" because of padding)
-----
FWIW here are some references on static taint analysis:
https://cacm.acm.org/magazines/2019/8/238344-scaling-static-...
cites https://www.usenix.org/legacy/event/sec06/tech/full_papers/x...
Perhaps not much on purpose, but it kicks in automatically if the Perl script is setuid. So you'll find questions about it where people are struggling with it.
It's probably sharing the canonical link. That's the "correct" behaviour as defined by browsers. Definitely not ideal in your example, though.
[1] https://api.rubyonrails.org/classes/ActiveRecord/Base.html#c...
[2] http://www.jkfill.com/2012/03/10/preventing-mass-assignment-...
https://nim-lang.org/docs/manual_experimental.html#taint-mod...
Perl's "taint"[1] capability is pretty interesting in this space. Do other languages have something similar?
[1] https://perldoc.perl.org/perlsec.html#Taint-mode
"You may not use data derived from outside your program to affect something else outside your program--at least, not by accident. All command line arguments, environment variables, locale information (see perllocale), results of certain system calls (readdir(), readlink(), the variable of shmread(), the messages returned by msgrcv(), the password, gcos and shell fields returned by the getpwxxx() calls), and all file input are marked as "tainted"."
Unrelated rant: Sometime recently mobile chrome omits any part of a url after a # when you copy/share the url. Grrr.