The goal of the challenge was to buy a flag. However, our balance starts from 0.
We could see that when selling the flag, the relevant code does not validate that the flag price is positive.
@PostMapping("/sell")
public String sellFlag(@Valid Flag flag, BindingResult bindingResult, Model model, Principal principal) {
Flag flagExists = flagService.findByName(flag.getName());
if (flagExists != null) {
bindingResult.rejectValue("name", "error.user",
"There is already a flag with the name provided");
}
if (principal != null) {
User user = userService.findByUsername(principal.getName());
model.addAttribute("current_user", user);
flag.setSeller(user);
flagService.saveFlag(flag);
}
return "redirect:flag/" + flag.getSlug();
}
We could thus sell a flag with a negative price. In the buyFlag function, this negative price is subtracted from buyerBalance, increasing the buyer's total balance.