Luv{Flag}
반응형

이해를 돕기 위해 아래에 극단적인 코드 예시를 가져왔다.

app.post('/login',function(req,resp){
    var id = req.body.id
	if (id.toLowerCase()!=='hiroo' && id.toUpperCase() === "HIROO") {
    	res.render('alert',{contents:'hello admin!',red:'/admin'})
    }
 }

 

언뜻 보기엔 말이 되지 않는 코드, 통과할 수 없는 if 문 이지만, 이는 얼마든지 우회, 통과가 가능하다.

toLowerCase(), toUpperCase() 함수를 사용하여 비교구문을 작성할때, 예상치 못한 결과가 도출될 수 있음에 주의하자.

Javascript(혹은 여타 언어) 의 toLowerCase(), toUpperCase() 와 같은 함수를 사용할때는 매우 유의 하자.

 

 

toLocaleLowerCase()

그 이유는 로케일을 고려하는 toLocaleLowerCase()  메서드에서 힌트를 얻을 수 있다.

대부분의 경우 lowerCase()와 동일한 결과가 나오지만, 터키어와 같이 케이스 매핑이 유니코드의 기본 케이스 매핑을 따르지 않는 일부 로케일의 경우 다른 결과가 나타날 수 있다.

"\u0130".toLocaleLowerCase("tr") === "i"; // true
"\u0130".toLocaleLowerCase("en-US") === "i"; // false

 

 

 

유니코드

위 문제는 모두 입력값으로 유니코드를 고려하지 않았기 때문에 발생한다.

toLowerCase(), toUpperCase() 함수는 유니코드 역시 처리해준다.

 

System.out.println(Character.toUpperCase('i') == Character.toUpperCase('İ')); //false
System.out.println(Character.toLowerCase('i') == Character.toLowerCase('İ')); //true

 

 

다시, 해당 코드에서 조건문을 통과하기 위해 어떻게 해야할지 생각해 보자.

app.post('/login',function(req,resp){
    var id = req.body.id
	if (id.toLowerCase()!=='hiroo' && id.toUpperCase() === "HIROO") {
    	res.render('alert',{contents:'hello admin!',red:'/admin'})
    }
 }

 

 

튀르키에어 중 i 를 사용하여 우회가 가능하다.

id값으로 Hı(튀르키에어 i)ROO 를 입력했을때, id.toLowerCase() 를 거친 값은 'hiroo' 와 같지 않았고, 

id.toUpperCase() 를 거친 값은 'HIROO' 와 같아 조건문을 통과 할 수 있었다.

 

 

 

 

https://ko.wikipedia.org/wiki/%C4%B0%EC%99%80_%C4%B1

 

İ와 ı - 위키백과, 우리 모두의 백과사전

위키백과, 우리 모두의 백과사전. 점 있는 I와 점 없는 I의 대소문자 대응. 일부 언어의 로마자에는 점(tittle)이 있는 I(İ i)와 점이 없는 I(I ı)가 따로 있다. 이는 튀르키예어의 로마자에서 시작되

ko.wikipedia.org

 

 

 

 

이것은 마찬가지로 Java 의 String.compareIgnoreCase() 가 toLowerCase(), toUpperCase() 모두 사용하는 이유이다.

https://stackoverflow.com/questions/39084032/java-why-string-compareignorecase-uses-both-character-touppercase-and-chara

 

Java: Why String.compareIgnoreCase() uses both Character.toUpperCase() and Character.toLowerCase()?

The compareToIgnoreCase method of String Class is implemented using the method in the snippet below(jdk1.8.0_45). i. Why are both Character.toUpperCase(char) and Character.toLowerCase(char) used ...

stackoverflow.com

 

 

 

 

 

반응형

검색 태그

loading