Adding security tests and fixing security bugs

#697