Programming/Thymeleaf

[Trouble shooting] Static classes or parameters is forbidden in thymeleaf 3.0.12

주리니e 2022. 7. 13. 16:37
728x90

Static classes or parameters is forbidden in thymeleaf 3.0.12

 

SpringBoot 보안 업데이트로 인해 Thymeleaf 버전 또한 같이 업데이트 하였다. 업데이트 후 다음과 같은 오류를 맞이하였다. Thymeleaf 3.0.12 이후의 버전에서는 아래와 같은 오류가 발생한다.

Caused by: org.attoparser.ParseException: Instantiation of new objects and access to static classes or parameters is forbidden in this context (template: "home" - line 9, col 5)
	at org.attoparser.MarkupParser.parseDocument(MarkupParser.java:393) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.attoparser.MarkupParser.parse(MarkupParser.java:257) ~[attoparser-2.0.5.RELEASE.jar:2.0.5.RELEASE]
	at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:230) ~[thymeleaf-3.0.15.RELEASE.jar:3.0.15.RELEASE]
	... 48 common frames omitted
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Instantiation of new objects and access to static classes or parameters is forbidden in this context (template: "home" - line 9, col 5)

 

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Thymeleaf</title>
</head>
<body>
	<div th:text="${T(com.example.thymeleaf.enums.Fruits).APPLE.name}"></div>
	<a th:href="@{'fruits/' + ${T(com.example.thymeleaf.enums.Fruits).APPLE.name}}">thymeleaf th:href</a>
</body>
</html>

th:text는 enum에 접근하여 상수를 정상적으로 출력하지만
th:href의 경우에는 상수에 접근하는 순간 오류가 발생한다.
Thymeleaf Github에서 찾아본 결과 Thymeleaf 3.0.12 버전 이후로는 T(className)를 사용한 정적 코드 실행은 특정 시나리오에서 사용할 수 없다고 한다.

 

해결방법

  • Enum Class
package com.example.thymeleaf.enums;

public enum Fruits {
	APPLE("사과"), BANANA("바나나"), MELON("멜론");

	Fruits(String name) {
		this.name = name;
	}

	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

 

  • Controller
package com.example.thymeleaf.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import com.example.thymeleaf.enums.Fruits;

@Controller
public class HomeController {

	@GetMapping("/")
	public String home(Model model) {
		model.addAttribute("name", Fruits.APPLE.getName());  // Controller 에서 상수 값을 전달
		return "home";
	}
}

 

  • HTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>Thymeleaf</title>
</head>
<body>
	<div th:text="${T(com.example.thymeleaf.enums.Fruits).APPLE.name}"></div>
	<a th:href="@{'fruits/' + ${name}}">사과 주소 페이지 열기</a>
</body>
</html>

위와 같이 값을 Controller에서 받아 사용한다.

 

이 제한은 보안상의 이유로 그리고 취약성 보고서의 결과의 일부로 설정되었습니다. 링크 표현식은 보안 관점에서 특히 민감하며 새 객체 생성(new ...)과 정적 코드 실행(T(...)) 모두 다음 두 가지 방식으로 문제가 됩니다.

  • 첫째, 이러한 표현식을 사용하면 웹 애플리케이션 개발자가 거의 또는 전혀 제어할 수 없는 라이브러리에서 가져온 클래스를 포함하여 클래스 경로의 모든 위치에서 임의의 코드를 실행할 수 있기 때문입니다.
  • 둘째, 사용자 입력(예: 요청 매개변수 또는 데이터 저장소의 값)이 유효성 검사 없이 모델 속성으로 컨트롤러에 추가된 다음 템플릿에서 URL 링크의 일부로 사용될 수 있는 시나리오가 많이 있습니다. 이 사용자 입력은 위에서 설명한 이 임의 코드의 실행을 요청하는 조각을 포함할 수 있는 URL에 대한 링크를 생성하거나 예를 들어 T(java.lang.Runtime).getRuntime().exec(...)를 호출합니다. 이것은 일반적이지는 않지만 가능한 경로의 표현식을 자동으로 실행할 수 있는 다른 쪽의 요청 확인자와 결합되어 원격 코드 실행을 위한 창을 엽니다.

 

참고 : https://github.com/thymeleaf/thymeleaf/issues/816

 

Release 3.0.12 has broken most of our templates due to #809 · Issue #816 · thymeleaf/thymeleaf

Due to changes in #809 we have a lot of templates that are now broken. For a lot of our templates we use static members in our Spring controllers for the request names. Hrefs within our templates l...

github.com

 

728x90