This repository has been archived by the owner on Aug 8, 2023. It is now read-only.
forked from RadoslavGatev/Ghost-Azure
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
126 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 20 additions & 20 deletions
40
...r.min-995634bcc05458654bf7d7764b948dca.js → ...r.min-9a614fcf6610a3db1a11c9b341a727e7.js
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
const got = require('got'); | ||
const {URL} = require('url'); | ||
const dns = require('dns'); | ||
const common = require('./common'); | ||
const ghostVersion = require('./ghost-version'); | ||
const config = require('../config'); | ||
const validator = require('../data/validation').validator; | ||
|
||
function isPrivateIp(addr) { | ||
return /^(::f{4}:)?10\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || | ||
/^(::f{4}:)?192\.168\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || | ||
/^(::f{4}:)?172\.(1[6-9]|2\d|30|31)\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || | ||
/^(::f{4}:)?127\.([0-9]{1,3})\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || | ||
/^(::f{4}:)?169\.254\.([0-9]{1,3})\.([0-9]{1,3})$/i.test(addr) || | ||
/^f[cd][0-9a-f]{2}:/i.test(addr) || | ||
/^fe80:/i.test(addr) || | ||
/^::1$/.test(addr) || | ||
/^::$/.test(addr); | ||
} | ||
|
||
function errorIfHostnameResolvesToPrivateIp(options) { | ||
// allow requests through to local Ghost instance | ||
const siteUrl = new URL(config.get('url')); | ||
const requestUrl = new URL(options.href); | ||
if (requestUrl.host === siteUrl.host) { | ||
return Promise.resolve(); | ||
} | ||
|
||
dns.lookup(options.hostname, {}, (err, address) => { | ||
if (err) { | ||
throw err; | ||
} | ||
|
||
if (isPrivateIp(address)) { | ||
throw new common.errors.InternalServerError({ | ||
message: 'URL resolves to a non-permitted private IP block', | ||
code: 'URL_PRIVATE_INVALID', | ||
context: options.href | ||
}); | ||
} | ||
|
||
return Promise.resolve(); | ||
}); | ||
} | ||
|
||
// same as our normal request lib but if any request in a redirect chain resolves | ||
// to a private IP address it will be blocked before the request is made. | ||
const externalRequest = got.extend({ | ||
headers: { | ||
'user-agent': 'Ghost/' + ghostVersion.original + ' (https://github.com/TryGhost/Ghost)' | ||
}, | ||
hooks: { | ||
init: [(options) => { | ||
if (!options.hostname || !validator.isURL(options.hostname)) { | ||
throw new common.errors.InternalServerError({ | ||
message: 'URL empty or invalid.', | ||
code: 'URL_MISSING_INVALID', | ||
context: options.href | ||
}); | ||
} | ||
}], | ||
beforeRequest: [errorIfHostnameResolvesToPrivateIp], | ||
beforeRedirect: [errorIfHostnameResolvesToPrivateIp] | ||
} | ||
}); | ||
|
||
module.exports = externalRequest; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.