Engineers have a code of ethics. If you were to knowingly build an unsafe bridge then you would be liable for the damage and injuries it caused if/when it failed. This is true even if your boss told you to do it. You not only have a moral/ethical obligation to not build that bridge, but also a legal one.
As we write software we need to keep these things also in mind. We are writing things that affect peoples lives. That could kill people. That affect the environment. We need to hold ourselves to the same moral obligations as other engineers. We have a duty to prevent harming people when we write our software.
If you have no stand to refuse to do the work then it is your duty to report such actions. First up the chain, and if that is rejected, then to the media. It is also your duty to report it if you have refused and you see it still happening. There are whistleblower rights in place to protect you.