Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#java# 规范 不安全的对象绑定 增补修订建议 #20

Open
k4n5ha0 opened this issue May 24, 2021 · 5 comments
Open

#java# 规范 不安全的对象绑定 增补修订建议 #20

k4n5ha0 opened this issue May 24, 2021 · 5 comments

Comments

@k4n5ha0
Copy link

k4n5ha0 commented May 24, 2021

对用户输入数据绑定到对象时如不做限制,可能造成攻击者恶意覆盖用户数据

脆弱代码:

@javax.persistence.Entity
class UserEntity {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;

	private String username;

	private String password;

	private Long role;
}
@Controller
class UserController {

	@PutMapping("/user/")
	@ResponseStatus(value = HttpStatus.OK)
	public void update(UserEntity user) {

	// 攻击者可以构造恶意user对象,将id字段构造为管理员id,将password字段构造为弱密码
	// 如果鉴权不完整,接口读取恶意user对象的id字段后会覆盖管理员的password字段成为弱密码
	userService.save(user); 
	}
}

解决方案:

  • setAllowedFields白名单
@Controller
class UserController {

	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request){

		// 对允许绑定的字段设置白名单,阻止其他所有字段
		binder.setAllowedFields(["role"]); 
	}
}
  • setDisallowedFields黑名单
@Controller
class UserController {

	@InitBinder
	public void initBinder(WebDataBinder binder, WebRequest request){

		// 对不允许绑定的字段设置黑名单,允许其他所有字段
		binder.setDisallowedFields(["username","password"]); 
	}
}
@coffeehb
Copy link

熊猫师傅666

@maniacs1
Copy link

师傅太强了,学习!

@Frank5337
Copy link

师傅强

@goexc
Copy link

goexc commented Feb 17, 2023 via email

@honguangli
Copy link

honguangli commented Feb 17, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants