<commit_before>/* global Cookies */
WebApp.connectHandlers.use('/snippet/download', function(req, res) {
	var match = /^\/([^\/]+)\/(.*)/.exec(req.url);

	if (match[1]) {
		let snippet = RocketChat.models.Messages.findOneById(match[1]);
		var cookie, rawCookies, ref, token, uid;
		cookie = new Cookies();

		if ((typeof req !== 'undefined' && req !== null ? (ref = req.headers) != null ? ref.cookie : void 0 : void 0) != null) {
			rawCookies = req.headers.cookie;
		}

		if (rawCookies != null) {
			uid = cookie.get('rc_uid', rawCookies);
		}

		if (rawCookies != null) {
			token = cookie.get('rc_token', rawCookies);
		}

		if (uid == null) {
			uid = req.query.rc_uid;
			token = req.query.rc_token;
		}

		if (!(uid && token && RocketChat.models.Users.findOneByIdAndLoginToken(uid, token))) {
			res.writeHead(403);
			res.end();
			return false;
		}

		res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(snippet.snippetName)}`);
		res.setHeader('Content-Type', 'application/octet-stream');

		// Removing the ``` contained in the msg.
		let snippetContent = snippet.msg.substr(3, snippet.msg.length - 6);
		res.setHeader('Content-Length', snippetContent.length);
		res.write(snippetContent);
		res.end();
		return;
	}

	res.writeHead(404);
	res.end();
	return;
});<commit_msg>Snippet: Fix permissions in the download section.<commit_after>/* global Cookies */
WebApp.connectHandlers.use('/snippet/download', function(req, res) {
	var cookie, rawCookies, ref, token, uid;
	cookie = new Cookies();

	if ((typeof req !== 'undefined' && req !== null ? (ref = req.headers) != null ? ref.cookie : void 0 : void 0) != null) {
		rawCookies = req.headers.cookie;
	}

	if (rawCookies != null) {
		uid = cookie.get('rc_uid', rawCookies);
	}

	if (rawCookies != null) {
		token = cookie.get('rc_token', rawCookies);
	}

	if (uid == null) {
		uid = req.query.rc_uid;
		token = req.query.rc_token;
	}

	let user = RocketChat.models.Users.findOneByIdAndLoginToken(uid, token);

	if (!(uid && token && user)) {
		res.writeHead(403);
		res.end();
		console.log('here');
		return false;
	}
	var match = /^\/([^\/]+)\/(.*)/.exec(req.url);

	if (match[1]) {
		let snippet = RocketChat.models.Messages.findOne(
			{
				'_id': match[1],
				'snippeted': true
			}
		);
		let room = RocketChat.models.Rooms.findOne({ '_id': snippet.rid, 'usernames': { '$in': [user.username] }});
		if (room === undefined) {
			res.writeHead(403);
			res.end();
			console.log('not here');
			return false;
		}

		res.setHeader('Content-Disposition', `attachment; filename*=UTF-8''${encodeURIComponent(snippet.snippetName)}`);
		res.setHeader('Content-Type', 'application/octet-stream');

		// Removing the ``` contained in the msg.
		let snippetContent = snippet.msg.substr(3, snippet.msg.length - 6);
		res.setHeader('Content-Length', snippetContent.length);
		res.write(snippetContent);
		res.end();
		return;
	}

	res.writeHead(404);
	res.end();
	return;
});
{'fraction_non_alphanumeric': 0.19036388140161725, 'fraction_numerical': 0.00977088948787062, 'mean_word_length': 3.8355048859934855, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5008059', 'n_tokens_mistral': 1181, 'n_tokens_neox': 1047, 'n_words': 215}