Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssl unverified context fix for HTTPS testing #44

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .test import BaseTest, ValidatorError
import ssl
try:
# python3
from urllib.request import Request, urlopen, HTTPError
Expand All @@ -19,11 +20,12 @@ def run(self, result):
url = url.replace('/info.json', '')
try:
r = Request(url)
wh = urlopen(r)
img = wh.read()
context = ssl._create_unverified_context()
wh = urlopen(r, context=context)
img = wh.read()
wh.close()
except HTTPError as e:
wh = e
wh = e

u = wh.geturl()
if u == url:
Expand All @@ -32,4 +34,4 @@ def run(self, result):
else:
# we must have redirected if our url is not what was requested
result.tests.append('redirect')
return result
return result
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .test import BaseTest
import ssl
try:
# python3
from urllib.request import Request, urlopen, HTTPError
Expand All @@ -18,8 +19,9 @@ def run(self, result):
hdrs = {'Accept': 'image/png;q=1.0'}
try:
r = Request(url, headers=hdrs)
wh = urlopen(r)
img = wh.read()
context = ssl._create_unverified_context()
wh = urlopen(r, context=context)
img = wh.read()
wh.close()
except HTTPError as e:
wh = e
Expand All @@ -32,4 +34,4 @@ def run(self, result):
result.last_status = wh.code
result.urls.append(url)
self.validationInfo.check('format', ct, 'image/png', result)
return result
return result
7 changes: 4 additions & 3 deletions implementations/validator/iiif_validator/tests/format_jp2.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .test import BaseTest, ValidatorError
import magic, urllib
import magic, urllib, ssl

class Test_Format_Jp2(BaseTest):
label = 'JPEG2000 format'
Expand All @@ -13,7 +13,8 @@ def run(self, result):
params = {'format': 'jp2'}
url = result.make_url(params)
# Need as plain string for magic
wh = urllib.urlopen(url)
context = ssl._create_unverified_context()
wh = urllib.urlopen(url, context=context)
img = wh.read()
wh.close()

Expand All @@ -24,4 +25,4 @@ def run(self, result):
raise ValidatorError('format', info, 'JPEG 2000', result)
else:
result.tests.append('format')
return result
return result
7 changes: 4 additions & 3 deletions implementations/validator/iiif_validator/tests/format_pdf.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .test import BaseTest, ValidatorError
import magic, urllib
import magic, urllib, ssl

class Test_Format_Pdf(BaseTest):
label = 'PDF format'
Expand All @@ -13,7 +13,8 @@ def run(self, result):
params = {'format': 'pdf'}
url = result.make_url(params)
# Need as plain string for magic
wh = urllib.urlopen(url)
context = ssl._create_unverified_context()
wh = urllib.urlopen(url, context=context)
img = wh.read()
wh.close()

Expand All @@ -24,4 +25,4 @@ def run(self, result):
raise ValidatorError('format', info, 'PDF', result)
else:
result.tests.append('format')
return result
return result
7 changes: 4 additions & 3 deletions implementations/validator/iiif_validator/tests/format_webp.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from .test import BaseTest, ValidatorError
import urllib
import urllib, ssl

class Test_Format_Webp(BaseTest):
label = 'WebP format'
Expand All @@ -14,11 +14,12 @@ def run(self, result):
params = {'format': 'webp'}
url = result.make_url(params)
# Need as plain string for magic
wh = urllib.urlopen(url)
context = ssl._create_unverified_context()
wh = urllib.urlopen(url, context=context)
img = wh.read()
wh.close()
if img[8:12] != "WEBP":
raise ValidatorError('format', 'unknown', 'WEBP', result)
else:
result.tests.append('format')
return result
return result
8 changes: 5 additions & 3 deletions implementations/validator/iiif_validator/tests/jsonld.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .test import BaseTest
import ssl
try:
# python3
from urllib.request import Request, urlopen, HTTPError
Expand All @@ -18,11 +19,12 @@ def run(self, result):
hdrs = {'Accept': 'application/ld+json'}
try:
r = Request(url, headers=hdrs)
wh = urlopen(r)
img = wh.read()
context = ssl._create_unverified_context()
wh = urlopen(r, context=context)
img = wh.read()
wh.close()
except HTTPError as e:
wh = e
ct = wh.headers['content-type']
self.validationInfo.check('json-ld', ct.startswith('application/ld+json'), 1, result, "Content-Type to start with application/ld+json")
return result
return result
5 changes: 3 additions & 2 deletions implementations/validator/iiif_validator/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import sys
import random
import os
import ssl
try: # python2
import BytesIO as io
# Must check for python2 first as io exists but is wrong one
Expand Down Expand Up @@ -260,9 +261,9 @@ def fetch(self, url):
"Referer": "http://iiif.io/api/image/validator",
"User-Agent": "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1b3pre) Gecko/20081130 Minefield/3.1b3pre"}
req = Request(url, headers=HEADERS)

context = ssl._create_unverified_context()
try:
wh = urlopen(req, timeout=5)
wh = urlopen(req, timeout=5, context=context)
except HTTPError as e:
wh = e
except:
Expand Down