Код контроллера:
Код: Выделить всё
@PostMapping("/createDetails")
public ResponseEntity createDetails(@Valid @RequestBody MyEntityDTO myEntityDTO) {
log.debug("Inside createDetails method, DTO - {}", myEntityDTO);
Integer userId = userService.getLoggedInUserId();
myEntityDTO.setUserId(userId);
myEntityService.saveEntity(myEntityDTO.toEntity());
return ResponseEntity.ok("Entity Created Successfully");
}
Код: Выделить всё
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import lombok.NonNull;
import java.util.List;
import java.util.stream.Collectors;
@Data
public class MyEntityDTO {
private Integer entityId;
private String entityName;
private Integer entityNumber;
private String status;
private List subEntities;
@JsonIgnore
public MyEntity toEntity(@NonNull Integer userId) {
MyEntity myEntity = new MyEntity();
myEntity.setEntityId(this.entityId);
myEntity.setEntityName(this.entityName != null ? this.entityName : "UNASSIGNED");
myEntity.setEntityNumber(this.entityNumber);
myEntity.setStatus(this.status);
myEntity.setUserId(userId);
MyEntityRelation relation = new MyEntityRelation();
relation.setStatus("ACTIVE");
relation.setSubEntities(this.subEntities != null ? this.subEntities.stream()
.map(MySubEntityDTO::toEntity)
.collect(Collectors.toList()) : null);
relation.setMyEntity(myEntity);
myEntity.setRelations(relation);
return myEntity;
}
}
Код: Выделить всё
import javax.persistence.*;
import java.util.List;
@Entity
public class MyEntity {
@Id
private Integer entityId;
private String entityName;
private Integer entityNumber;
private String status;
private Integer userId;
@OneToMany(mappedBy = "myEntity", cascade = CascadeType.ALL)
private List relations;
// Getters, Setters, and Named Queries...
}
Уязвимость возникает из-за того, что прямая привязка пользовательских входных данных к DTO и преобразование их в сущности может позволить злоумышленнику вводить неожиданные значения, обходить проверку или манипулировать конфиденциальными полями, такими как статус или идентификатор пользователя
Что я уже пробовал:
- Я добавил @ Действительно для DTO и проверки на уровне поля. аннотации.
- Я игнорировал конфиденциальные поля, такие как userId, с помощью @JsonIgnore.
Подробнее здесь: https://stackoverflow.com/questions/792 ... ty-mapping