Ghost/core/frontend/helpers/url.js
Thibaut Patel 77c0364efd 🐛 Fixed malformed URLs crashing the url helper
refs https://github.com/TryGhost/Team/issues/960

- Character like "%%" or "%80" would crash our current url escaping behavior. We consider they aren't valid URLs as the percentages haven't been properly escaped.
2021-12-02 10:34:24 +01:00

37 lines
1.2 KiB
JavaScript

// # URL helper
// Usage: `{{url}}`, `{{url absolute="true"}}`
//
// Returns the URL for the current object scope i.e. If inside a post scope will return post permalink
// `absolute` flag outputs absolute URL, else URL is relative
const {metaData} = require('../services/proxy');
const {SafeString} = require('../services/rendering');
const logging = require('@tryghost/logging');
const sentry = require('../../shared/sentry');
const errors = require('@tryghost/errors');
const {getMetaDataUrl} = metaData;
module.exports = function url(options) {
const absolute = options && options.hash.absolute && options.hash.absolute !== 'false';
let outputUrl = getMetaDataUrl(this, absolute);
try {
outputUrl = encodeURI(decodeURI(outputUrl));
} catch (err) {
// Happens when the outputURL contains an invalid URI character like "%%" or "%80"
// Send the error not to be blind to these
const error = new errors.IncorrectUsageError({
message: `The url "${outputUrl}" couldn't be escaped correctly`,
err: err
});
sentry.captureException(error);
logging.error(error);
return new SafeString('');
}
return new SafeString(outputUrl);
};