CVE-2022-31690:spring-security-oauth2-client 中的許可權提升漏洞

工程 | Steve Riesenberg | 2022 年 10 月 31 日 | ...

Spring Security 5.6.95.7.52022 年 10 月 31 日 釋出,其中包含針對 CVE-2022-31690 的修復,該漏洞影響 spring-security-oauth2-client 中授權範圍的對映。 建議使用者儘快更新。

影響

已應用緩解措施的使用者應注意以下影響

當授權伺服器 (AS) 使用空或缺失的 scope 引數響應 OAuth2 訪問令牌響應時,沒有授權範圍對映到主體(當前使用者)。

如果您受到此漏洞的影響,當 AS 不返回範圍時,使用者將不會被授予任何以 SCOPE_ 開頭的許可權。 只有特殊許可權 ROLE_USER 授予主體。

注意

從 6.0 開始,特殊許可權已更改為 OAUTH2_USER(或 OIDC_USER)。 有關更多資訊,請參見 6.0 參考文件中的 使用 GrantedAuthoritiesMapper

如果您的應用程式需要其他許可權,則應註冊一個 GrantedAuthoritiesMapper @Bean 以提供所需的許可權,如以下示例所示

@Configuration
@EnableWebSecurity
public class OAuth2LoginSecurityConfig {

	@Bean
	public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
		http
			.authorizeHttpRequests((authorize) -> authorize
				.mvcMatchers(HttpMethod.GET, "/messages").hasAuthority("SCOPE_read")
				// ...
				.anyRequest().authenticated()
			)
			.oauth2Login(Customizer.withDefaults());
		return http.build();
	}

	@Bean
	public GrantedAuthoritiesMapper userAuthoritiesMapper() {
		return (authorities) -> {
			if (!authorities.isEmpty() && authorities.stream()
					.map(GrantedAuthority::getAuthority)
					.anyMatch(authority -> authority.startsWith("SCOPE_"))) {
				// AS returned scopes that are mapped to SCOPE_ by Spring Security
				return authorities;
			}

			// AS returned no scopes, either because none were granted or because requested == granted
			// See https://www.rfc-editor.org/rfc/rfc6749#section-5.1 and your AS documentation
			// You can access the ID Token or UserInfo attributes to map authorities based on the user:

			Set<GrantedAuthority> grantedAuthorities = new HashSet<>();
			authorities.forEach(authority -> {
				if (OidcUserAuthority.class.isInstance(authority)) {
					OidcUserAuthority oidcUserAuthority = (OidcUserAuthority) authority;
					OidcIdToken idToken = oidcUserAuthority.getIdToken();
					// ...
				} else if (OAuth2UserAuthority.class.isInstance(authority)) {
					OAuth2UserAuthority oauth2UserAuthority = (OAuth2UserAuthority) authority;
					Map<String, Object> userAttributes = oauth2UserAuthority.getAttributes();
					// ...
				}
			});

			return grantedAuthorities;

			// Alternatively, provide a fallback set of authorities that make sense for your application
			// return AuthorityUtils.createAuthorityList("ROLE_USER", "SCOPE_read");
		};
	}

}

警告

不建議 僅從 ClientRegistration 對映許可權。

獲取 Spring 新聞郵件

透過 Spring 新聞郵件保持聯絡

訂閱

領先一步

VMware 提供培訓和認證,以加速您的進步。

瞭解更多

獲得支援

Tanzu Spring 透過一個簡單的訂閱提供 OpenJDK™、Spring 和 Apache Tomcat® 的支援和二進位制檔案。

瞭解更多

即將舉行的活動

檢視 Spring 社群中所有即將舉行的活動。

檢視全部