Skip to content

Commit 604efb6

Browse files
committed
Respect TLS override in WebID profile fetch
1 parent 2cfa32c commit 604efb6

2 files changed

Lines changed: 92 additions & 11 deletions

File tree

lib/webid/lib/get.mjs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
1-
import { URL } from 'url'
2-
3-
export default function get (webid, callback) {
4-
let uri
1+
import { URL } from 'url'
2+
import { Agent } from 'undici'
3+
4+
const insecureDispatcher = new Agent({
5+
connect: {
6+
rejectUnauthorized: false
7+
}
8+
})
9+
10+
export default function get (webid, callback) {
11+
let uri
512
try {
613
uri = new URL(webid)
714
} catch (err) {
815
return callback(new Error('Invalid WebID URI: ' + webid + ': ' + err.message))
916
}
10-
const headers = {
11-
Accept: 'text/turtle, application/ld+json'
12-
}
13-
fetch(uri.href, { method: 'GET', headers })
14-
.then(async res => {
15-
if (!res.ok) {
16-
return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.status))
17+
const headers = {
18+
Accept: 'text/turtle, application/ld+json'
19+
}
20+
const options = { method: 'GET', headers }
21+
if (uri.protocol === 'https:' && process.env.NODE_TLS_REJECT_UNAUTHORIZED === '0') {
22+
options.dispatcher = insecureDispatcher
23+
}
24+
fetch(uri.href, options)
25+
.then(async res => {
26+
if (!res.ok) {
27+
return callback(new Error('Failed to retrieve WebID from ' + uri.href + ': HTTP ' + res.status))
1728
}
1829
const contentType = res.headers.get('content-type')
1930
let body

test/unit/webid-get-test.mjs

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import { expect } from 'chai'
2+
import get from '../../lib/webid/lib/get.mjs'
3+
4+
describe('webid get()', () => {
5+
const originalFetch = global.fetch
6+
const originalTlsSetting = process.env.NODE_TLS_REJECT_UNAUTHORIZED
7+
8+
function callGet (webid) {
9+
return new Promise((resolve, reject) => {
10+
get(webid, (err, body, contentType) => {
11+
if (err) {
12+
reject(err)
13+
return
14+
}
15+
resolve({ body, contentType })
16+
})
17+
})
18+
}
19+
20+
afterEach(() => {
21+
global.fetch = originalFetch
22+
if (originalTlsSetting === undefined) {
23+
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED
24+
} else {
25+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = originalTlsSetting
26+
}
27+
})
28+
29+
it('uses an insecure dispatcher for https fetches when TLS verification is disabled', async () => {
30+
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'
31+
32+
global.fetch = async (url, options) => {
33+
expect(url).to.equal('https://example.com/profile/card#me')
34+
expect(options.method).to.equal('GET')
35+
expect(options.headers.Accept).to.equal('text/turtle, application/ld+json')
36+
expect(options.dispatcher).to.exist
37+
38+
return {
39+
ok: true,
40+
headers: {
41+
get: () => 'text/turtle'
42+
},
43+
text: async () => '@prefix ex: <http://example.com/> .'
44+
}
45+
}
46+
47+
const { body, contentType } = await callGet('https://example.com/profile/card#me')
48+
expect(contentType).to.equal('text/turtle')
49+
expect(body).to.include('@prefix ex:')
50+
})
51+
52+
it('does not use an insecure dispatcher when TLS verification is enabled', async () => {
53+
delete process.env.NODE_TLS_REJECT_UNAUTHORIZED
54+
55+
global.fetch = async (url, options) => {
56+
expect(options.dispatcher).to.equal(undefined)
57+
58+
return {
59+
ok: true,
60+
headers: {
61+
get: () => 'text/turtle'
62+
},
63+
text: async () => 'ok'
64+
}
65+
}
66+
67+
const { body } = await callGet('https://example.com/profile/card#me')
68+
expect(body).to.equal('ok')
69+
})
70+
})

0 commit comments

Comments
 (0)