2022-03-28 11:46:47 -04:00
|
|
|
import xss from "xss";
|
|
|
|
|
|
|
|
const HTML_TYPES = ["html_block", "html_inline"];
|
|
|
|
|
2019-05-28 21:00:25 -04:00
|
|
|
// add image to array if src has an upload
|
|
|
|
function addImage(uploads, token) {
|
|
|
|
if (token.attrs) {
|
|
|
|
for (let i = 0; i < token.attrs.length; i++) {
|
2022-06-28 15:27:15 -04:00
|
|
|
const value = token.attrs[i][1];
|
|
|
|
if (value?.startsWith("upload://")) {
|
|
|
|
uploads.push({ token, srcIndex: i, origSrc: value });
|
2019-05-28 21:00:25 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
function attr(name, value) {
|
|
|
|
if (value) {
|
|
|
|
return `${name}="${xss.escapeAttrValue(value)}"`;
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
return name;
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
function uploadLocatorString(url) {
|
|
|
|
return `___REPLACE_UPLOAD_SRC_${url}___`;
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
function findUploadsInHtml(uploads, blockToken) {
|
|
|
|
// Slightly misusing our HTML sanitizer to look for upload://
|
|
|
|
// image src attributes, and replace them with a placeholder.
|
|
|
|
// Note that we can't use browser DOM APIs because this needs
|
|
|
|
// to run in mini-racer.
|
|
|
|
let foundImage = false;
|
2022-06-23 22:28:05 -04:00
|
|
|
let allowList;
|
|
|
|
|
|
|
|
const filter = new xss.FilterXSS({
|
|
|
|
allowList: [],
|
2022-03-28 11:46:47 -04:00
|
|
|
allowCommentTag: true,
|
2022-06-23 22:28:05 -04:00
|
|
|
onTag(tag, html, info) {
|
2022-03-28 11:46:47 -04:00
|
|
|
// We're not using this for sanitizing, so allow all tags through
|
2022-06-23 22:28:05 -04:00
|
|
|
info.isWhite = true;
|
|
|
|
allowList[tag] = [];
|
2022-03-28 11:46:47 -04:00
|
|
|
},
|
|
|
|
onTagAttr(tag, name, value) {
|
|
|
|
if (tag === "img" && name === "src" && value.startsWith("upload://")) {
|
|
|
|
uploads.push({ token: blockToken, srcIndex: null, origSrc: value });
|
|
|
|
foundImage = true;
|
|
|
|
return uploadLocatorString(value);
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2022-03-28 11:46:47 -04:00
|
|
|
return attr(name, value);
|
|
|
|
},
|
|
|
|
});
|
2022-06-23 22:28:05 -04:00
|
|
|
|
|
|
|
allowList = filter.options.whiteList;
|
|
|
|
const newContent = filter.process(blockToken.content);
|
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
if (foundImage) {
|
|
|
|
blockToken.content = newContent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function processToken(uploads, token) {
|
|
|
|
if (token.tag === "img" || token.tag === "a") {
|
|
|
|
addImage(uploads, token);
|
|
|
|
} else if (HTML_TYPES.includes(token.type)) {
|
|
|
|
findUploadsInHtml(uploads, token);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (token.children) {
|
|
|
|
for (let j = 0; j < token.children.length; j++) {
|
|
|
|
const childToken = token.children[j];
|
|
|
|
processToken(uploads, childToken);
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 11:46:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function rule(state) {
|
|
|
|
let uploads = [];
|
|
|
|
|
|
|
|
for (let i = 0; i < state.tokens.length; i++) {
|
|
|
|
let blockToken = state.tokens[i];
|
|
|
|
|
|
|
|
processToken(uploads, blockToken);
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
if (uploads.length > 0) {
|
2022-03-28 11:46:47 -04:00
|
|
|
let srcList = uploads.map((u) => u.origSrc);
|
|
|
|
|
|
|
|
// In client-side cooking, this lookup returns nothing
|
|
|
|
// This means we set data-orig-src, and let decorateCooked
|
|
|
|
// lookup the image URLs asynchronously
|
2019-05-28 21:00:25 -04:00
|
|
|
let lookup = state.md.options.discourse.lookupUploadUrls;
|
|
|
|
let longUrls = (lookup && lookup(srcList)) || {};
|
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
uploads.forEach(({ token, srcIndex, origSrc }) => {
|
2019-05-28 21:00:25 -04:00
|
|
|
let mapped = longUrls[origSrc];
|
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
if (HTML_TYPES.includes(token.type)) {
|
|
|
|
const locator = uploadLocatorString(origSrc);
|
|
|
|
let attrs = [];
|
|
|
|
|
|
|
|
if (mapped) {
|
|
|
|
attrs.push(
|
|
|
|
attr("src", mapped.url),
|
|
|
|
attr("data-base62-sha1", mapped.base62_sha1)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
attrs.push(
|
|
|
|
attr(
|
|
|
|
"src",
|
|
|
|
state.md.options.discourse.getURL("/images/transparent.png")
|
|
|
|
),
|
|
|
|
attr("data-orig-src", origSrc)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
token.content = token.content.replace(locator, attrs.join(" "));
|
|
|
|
} else if (token.tag === "img") {
|
|
|
|
if (mapped) {
|
|
|
|
token.attrs[srcIndex][1] = mapped.url;
|
|
|
|
token.attrs.push(["data-base62-sha1", mapped.base62_sha1]);
|
|
|
|
} else {
|
|
|
|
// no point putting a transparent .png for audio/video
|
|
|
|
if (token.content.match(/\|video|\|audio/)) {
|
2022-07-06 04:37:54 -04:00
|
|
|
token.attrs[srcIndex][1] =
|
|
|
|
state.md.options.discourse.getURL("/404");
|
2022-03-28 11:46:47 -04:00
|
|
|
} else {
|
|
|
|
token.attrs[srcIndex][1] = state.md.options.discourse.getURL(
|
|
|
|
"/images/transparent.png"
|
|
|
|
);
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
token.attrs.push(["data-orig-src", origSrc]);
|
|
|
|
}
|
|
|
|
} else if (token.tag === "a") {
|
|
|
|
if (mapped) {
|
2022-09-28 19:24:33 -04:00
|
|
|
// when secure uploads is enabled we want the full /secure-media-uploads or /secure-uploads
|
2022-03-28 11:46:47 -04:00
|
|
|
// url to take advantage of access control security
|
|
|
|
if (
|
2022-09-28 19:24:33 -04:00
|
|
|
state.md.options.discourse.limitedSiteSettings.secureUploads &&
|
|
|
|
(mapped.url.includes("secure-media-uploads") ||
|
|
|
|
mapped.url.includes("secure-uploads"))
|
2022-03-28 11:46:47 -04:00
|
|
|
) {
|
|
|
|
token.attrs[srcIndex][1] = mapped.url;
|
|
|
|
} else {
|
|
|
|
token.attrs[srcIndex][1] = mapped.short_path;
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
2022-03-28 11:46:47 -04:00
|
|
|
} else {
|
|
|
|
token.attrs[srcIndex][1] = state.md.options.discourse.getURL("/404");
|
2019-05-28 21:00:25 -04:00
|
|
|
|
2022-03-28 11:46:47 -04:00
|
|
|
token.attrs.push(["data-orig-href", origSrc]);
|
|
|
|
}
|
2019-05-28 21:00:25 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function setup(helper) {
|
|
|
|
const opts = helper.getOptions();
|
2020-09-22 10:28:28 -04:00
|
|
|
if (opts.previewing) {
|
2020-10-27 22:22:06 -04:00
|
|
|
helper.allowList(["img.resizable"]);
|
2020-09-22 10:28:28 -04:00
|
|
|
}
|
2019-06-10 21:15:45 -04:00
|
|
|
|
2020-10-27 22:22:06 -04:00
|
|
|
helper.allowList([
|
2019-06-10 21:15:45 -04:00
|
|
|
"img[data-orig-src]",
|
|
|
|
"img[data-base62-sha1]",
|
|
|
|
"a[data-orig-href]",
|
|
|
|
]);
|
2019-05-28 21:00:25 -04:00
|
|
|
|
|
|
|
helper.registerPlugin((md) => {
|
|
|
|
md.core.ruler.push("upload-protocol", rule);
|
|
|
|
});
|
|
|
|
}
|