Skip to content

Commit 43f255e

Browse files
committed
Perform basic URL/output validation and consistify encode/decode behavior.
Rather than look at the helper program's full output, we now only grab the first word, and then use urlparse to check for a scheme to verify basic URL-ness. Further, we make sure that none of the characters are a control code. Calls to decode() and encode() are now passed getpreferredencoding() to match the rest of the codebase.
1 parent e78ddd0 commit 43f255e

File tree

1 file changed

+9
-2
lines changed

1 file changed

+9
-2
lines changed

bpython/repl.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,14 @@
3434
import sys
3535
import textwrap
3636
import traceback
37+
import unicodedata
3738
from glob import glob
3839
from itertools import takewhile
3940
from locale import getpreferredencoding
4041
from socket import error as SocketError
4142
from string import Template
4243
from urllib import quote as urlquote
44+
from urlparse import urlparse
4345
from xmlrpclib import ServerProxy, Error as XMLRPCError
4446

4547
from pygments.lexers import PythonLexer
@@ -820,8 +822,8 @@ def do_pastebin_helper(self, s):
820822
try:
821823
helper = subprocess.Popen('', executable=self.config.pastebin_helper,
822824
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
823-
helper.stdin.write(s.encode())
824-
paste_url = helper.communicate()[0].decode().strip()
825+
helper.stdin.write(s.encode(getpreferredencoding()))
826+
paste_url = helper.communicate()[0].decode(getpreferredencoding()).split()[0]
825827
except OSError, e:
826828
if e.errno == errno.ENOENT:
827829
self.interact.notify('Upload failed: Helper program not found.')
@@ -837,6 +839,11 @@ def do_pastebin_helper(self, s):
837839
if not paste_url:
838840
self.interact.notify('Upload failed: No output from helper program.')
839841
return
842+
else:
843+
parsed_url = urlparse(paste_url)
844+
if not parsed_url.scheme or any(unicodedata.category(char) == 'Cc' for char in paste_url):
845+
self.interact.notify("Upload failed: Failed to recognize the helper program's output as an URL.")
846+
return
840847

841848
self.prev_pastebin_content = s
842849
self.interact.notify('Pastebin URL: %s' % (paste_url, ), 10)

0 commit comments

Comments
 (0)