Locked ships unlocking glitch
After a bit of looking into it, I think I found the cause of locked ships unlocking themselves. A player can only have one locked ship at a time, however when a player is added as pilot of a ship that locks itself, it deactivates the lock for the ship currently locked for the player.
This makes it so if I locked my cargo ship in space and one of my settlement members pilots a ship with me as a pilot, the ship when it locks itself the cargo ship I am trying to protect will deactivate it's lock, therefore making it raid-able.
To put it simply, starship computers should be fixed so that they can't be unlocked via someone else's ship locking itself.