yarri / LinkFinder

Converts non-clickable URLs and email addresses in text (plain or HTML) into clickable HTML links
MIT License
18 stars 3 forks source link

Undefined array key "uri" #5

Closed driesvints closed 3 years ago

driesvints commented 3 years ago

I'm trying to parse a certain Markdown content string provided from user input for a new thread of the Laravel.io forums: https://laravel.io/forum

I don't have control over this content. This is what they provided:

# Code parameter is in callback url from google but not passed back\r\n\r\n## The error\r\nI get the following error, this happens only on the digitalcocean(LEMP template) server I use. On my Mac with localhost it works fine.\r\n\r\nClientException in RequestException.php line 107:\r\nClient error: `POST https://accounts.google.com/o/oauth2/token` resulted in a `400 Bad Request` response:\r\n{\r\n\"error\" : \"invalid_request\",\r\n\"error_description\" : \"Missing required parameter: code\"\r\n}  \r\n....  \r\nat Client->request('post', 'https://accounts.google.com/o/oauth2/token', array('headers' => array('Accept' => 'application/json'), 'form_params' => array('client_id' => '501018655941-lu5e4mhrmo1opkef4d8b7i5tpgjj84ac.apps.googleusercontent.com', 'client_secret' => 'xtqpHi4rF8MS2p_WOyJQOhmv', code' => null,\r\n'redirect_uri' => 'http://pep-test.trouwborst.net/auth/google/callback', 'grant_type' => 'authorization_code'))) in Client.php line 87  \r\n\r\nWhen examining the google callback url the code parameter is actually there  \r\nGET /auth/google/callback?state=dJBafzowawsXuGMqBQIo66kgssL9X13PHMBSlxAw&code=4/xm5JH99vYNdcXxbUvDvKTTvcOXisrlnAc2fPEko4DtA&authuser=1&session_state=91c04a6df341600b444033cc9bf8a8b2a1485479..94ac&prompt=none HTTP/1.1 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 Accept-Encoding: gzip, deflate Accept-Language: nl-nl Connection: keep-alive Content-Length: Content-Type: Host: pep-test.trouwborst.net User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/601.7.7 (KHTML, like Gecko) Version/9.1.2 Safari/601.7.7\r\n\r\n## AuthController\r\n~~~\r\n    public function handleProviderCallback(request $Request)\r\n    {\r\n        try {\r\n            $user = Socialite::driver('google')->stateless()->user();   //watch out with changes due to stateless user\r\n        } catch (Exception $e) {\r\n            return Redirect::to('auth/google');\r\n        }\r\n\r\n        $authUser = $this->checkUserValidation($user);\r\n\r\n\r\n~~~\r\n\r\nCan anyone shed some light on this issue?\r\n\r\n\r\n

We use the link finder like so:

(new LinkFinder([
    'attrs' => ['target' => '_blank', 'rel' => 'nofollow'],
]))->processHtml($markdown);

Which with the above content produces the following ErrorException: Undefined array key "uri". Which can be lead back to this line here: https://github.com/yarri/LinkFinder/blob/9d6434dae4d527fbcfff1e177905780babeaf2e5/src/link_finder.php#L440

You can reproduce it with the following test:

function testFailure() {
    $src = '501018655941-lu5e4mhrmo1opkef4d8b7i5tpgjj84ac.apps.googleusercontent.com';

    $lfinder = new LinkFinder();
    $this->assertEquals('',$lfinder->process($src));
}

The reason why I haven't filled out the assertEquals is because I'm not 100% sure how this should be handled since technically they're posting a PHP snippet. I'll leave it up to you to decide how you want to handle it.

yarri commented 3 years ago

Hi Dries! Thank you for the issue reporting. I fixed it and new version 2.7.1 was released.

In version 2.7, there is a new feature for automatic long URLs shortening. It can be disabled using the option: $lfinder = new LinkFinder(["shorten_long_urls" => false]);

driesvints commented 3 years ago

@yarri amazing! Thank you so much for the quick reply & fix 🙂