IT TIP

urlencoded 슬래시가 URL을 깨고 있습니다.

itqueen 2020. 10. 31. 10:21
반응형

urlencoded 슬래시가 URL을 깨고 있습니다.


시스템 정보

내 프로젝트에이 형식의 URL이 있습니다.

http://project_name/browse_by_exam/type/tutor_search/keyword/class/new_search/1/search_exam/0/search_subject/0

여기서 키워드 / 클래스 쌍은 "클래스"키워드로 검색을 의미합니다.

프로젝트의 모든 모듈에 대해 실행되는 공통 index.php 파일이 있습니다. URL에서 index.php를 제거하는 재 작성 규칙 만 있습니다.

RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L,QSA]

검색 URL을 읽는 동안 검색 URL과 urldecode ()를 준비하는 동안 urlencode ()를 사용하고 있습니다.

문제

슬래시 문자 만 URL을 깨고 404 페이지를 찾을 수 없음 오류가 발생합니다. 예를 들어, 내가 검색 one/two하면 URL은

http://project_name/browse_by_exam/type/tutor_search/keyword/one%2Ftwo/new_search/1/search_exam/0/search_subject/0/page_sort/

이 문제를 어떻게 해결합니까? URL에서 index.php를 숨겨야합니다. 그렇지 않으면 필요하지 않은 경우 슬래시에 문제가 없을 것이며이 URL을 사용할 수 있습니다.

http://project_name/index.php?browse_by_exam/type/tutor_search/keyword/one
%2Ftwo/new_search/1/search_exam/0/search_subject/0

아파치는 모든 URL을 거부 %2F보안상의 이유로, 경로 부분 : 사이의 차이를 말할. (다시 작성하지 않고도 예) 스크립트 수없는 정상적으로 %2F/받는 인해 PATH_INFO바보 인 (자동 URL-디코딩되는 환경 변수를하지만, 오랜 CGI 사양의 일부이므로 이에 대해 수행 할 수있는 작업이 없습니다.)

AllowEncodedSlashes지시문을 사용하여이 기능을 끌 수 있지만 다른 웹 서버는 여전히이를 허용하지 않으며 (해제 옵션이 없음) 다른 문자도 금기 일 수 있으며 (예 :) %5C, %00특히 항상 Apache와 IIS 모두에 의해 차단됩니다. 따라서 응용 프로그램 %2F이 경로 부분에 또는 다른 문자 를 가질 수있는 것에 의존한다면 호환성 / 배포 옵션을 제한하게됩니다.

검색 URL을 준비하는 동안 urlencode ()를 사용하고 있습니다.

경로 부분 이스케이프가 rawurlencode()아닌 urlencode()사용해야합니다 . URL의 다른 부분이 아니라 urlencode()실제로 application/x-www-form-urlencoded쿼리 문자열 또는 POST 요청 본문과 같은 데이터에 대한 이름 입니다.

차이점은 +경로 부분의 공간을 의미하지 않는다는 것입니다. 대신 rawurlencode()올바르게 생성 %20되며 양식 인코딩 된 데이터와 URL의 다른 부분 모두에서 작동합니다.


URL 인코딩 후 % 2F를 % 252F로 바꿉니다.

PHP

function custom_http_build_query($query=array()){

    return str_replace('%2F','%252F', http_build_query($query));
}

htaccess를 통해 요청 처리

.htaccess

RewriteCond %{REQUEST_URI} ^(.*?)(%252F)(.*?)$ [NC]
RewriteRule . %1/%3 [R=301,L,NE]

자원

http://www.leakon.com/archives/865


Apache에서 AllowEncodedSlashes On은 요청이 404로 즉시 거부되는 것을 방지합니다.

이 문제를 해결하는 방법에 대한 또 다른 아이디어입니다.


$encoded_url = str_replace('%2F', '/', urlencode($url));

url get param의 슬래시와 동일한 문제가 발생했습니다. 제 경우에는 다음과 같은 PHP 코드가 작동합니다.

$value = "hello/world"
$value = str_replace('/', '/', $value;?>
$value = urlencode($value);?>
# $value is now hello%26%2347%3Bworld

먼저 슬래시를 html 엔티티로 바꾼 다음 URL 인코딩을 수행합니다.


내 호스팅 계정에서이 문제는 모든 계정에 대해 자동으로 설정된 ModSecurity 규칙으로 인해 발생했습니다. 이 문제를보고하자 관리자가 내 계정에 대한이 규칙을 빠르게 제거했습니다.


다른 문자를 사용하고 서버 측 슬래시를 바꿉니다.

예를 들어 Drupal.org는 % 21 (Exalamation Mark 문자!)을 사용하여 url 매개 변수의 슬래시를 나타냅니다.

아래 링크는 모두 작동합니다.

https://api.drupal.org/api/drupal/includes%21common.inc/7

https://api.drupal.org/api/drupal/includes!common.inc/7

캐릭터가 매개 변수의 캐릭터와 충돌 할 수 있다고 걱정된다면 캐릭터 조합을 사용하십시오.

따라서 URL은 http : // project_name / browse_by_exam / type / tutor_search / keyword / one_-! two / new_search / 1 / search_exam / 0 / search_subject / 0이됩니다.

js로 변경하고 다시 슬래시 서버 측으로 변환하십시오.


이 문제에 대한 표준 솔루션은 슬래시를 포함 할 수있는 매개 변수를 URL의 마지막 매개 변수로 만들어 슬래시를 허용하는 것입니다.

제품 코드 URL의 경우 ...

mysite.com/product/details/PR12345/22

검색어에 대해

http://project/search_exam/0/search_subject/0/keyword/Psychology/Management

(여기 키워드는 심리학 / 경영)

첫 번째 "이름이 지정된"매개 변수를 처리 한 다음 나머지 매개 변수를 제품 코드 또는 키워드로 연결하는 것은 엄청난 작업이 아닙니다.

일부 프레임 워크에는 라우팅 정의에이 기능이 내장되어 있습니다.

슬래시가 포함 된 두 개의 매개 변수가 포함 된 사용 사례에는 적용되지 않습니다.


나를 위해 간단합니다 base64_encode

$term = base64_encode($term) 
$url = $youurl.'?term='.$term

용어를 해독 한 후

$term = base64_decode($['GET']['term'])

이 방법으로 "/"및 "\"를 인코딩합니다.


Here's my humble opinion. !!!! Don't !!!! change settings on the server to make your parameters work correctly. This is a time bomb waiting to happen someday when you change servers.

The best way I have found is to just convert the parameter to base 64 encoding. So in my case, I'm calling a php service from Angular and passing a parameter that could contain any value.

So my typescript code in the client looks like this:

    private encodeParameter(parm:string){
    if (!parm){
        return null;
    }
    return btoa(parm);
}

And to retrieve the parameter in php:

    $item_name = $request->getAttribute('item_name');
    $item_name = base64_decode($item_name); 

I use javascript encodeURI() function for the URL part that has forward slashes that should be seen as characters instead of http address. Eg:

"/api/activites/" + encodeURI("?categorie=assemblage&nom=Manipulation/Finition")

see http://www.w3schools.com/tags/ref_urlencode.asp


I solved this by using 2 custom functions like so:

function slash_replace($query){

    return str_replace('/','_', $query);
}

function slash_unreplace($query){

    return str_replace('_','/', $query);
}

So to encode I could call:

rawurlencode(slash_replace($param))

and to decode I could call

slash_unreplace(rawurldecode($param);

Cheers!


You can use %2F if using it this way:
?param1=value1&param2=value%2Fvalue

but if you use /param1=value1/param2=value%2Fvalue it will throw an error.

참고URL : https://stackoverflow.com/questions/3235219/urlencoded-forward-slash-is-breaking-url

반응형