<commit_before>var env = require('./environment');
var multer = require("multer");
var upload = multer({
  dest: require("os").tmpdir(),
  limits: {
    fileSize: env.get("MAX_FILE_SIZE_BYTES")
  }
});

module.exports = function middlewareConstructor() {
  var metrics = require('./metrics')(),
      utils = require("./utils"),
      hood = require("hood"),
      emulate_s3 = env.get('S3_EMULATION') || !env.get('S3_KEY'),
      knox = emulate_s3 ? require("noxmox").mox : require("knox"),
      Cryptr = require("cryptr");

  var cryptr = new Cryptr(env.get("SESSION_SECRET"));

  return {
    /**
     * Multipart File Upload of a single `brambleFile` form field
     */
    fileUpload: upload.single("brambleFile"),

    /**
     * Content Security Policy HTTP response header
     * helps you reduce XSS risks on modern browsers
     * by declaring what dynamic resources are allowed
     * to load via a HTTP Header.
     */
    addCSP: function ( options ) {
      return hood.csp({
        headers: [
          "Content-Security-Policy-Report-Only"
        ],
        policy: {
          'connect-src': [
            "'self'"
          ],
          'default-src': [
            "'self'"
          ],
          'frame-src': [
            "'self'",
            "https://docs.google.com",
            options.brambleHost,
            options.personaHost
          ],
          'font-src': [
            "'self'",
            "https://fonts.gstatic.com",
            "https://netdna.bootstrapcdn.com"
          ],
          'img-src': [
            "*"
          ],
          'media-src': [
            "*"
          ],
          'script-src': [
            "'self'",
            "http://mozorg.cdn.mozilla.net",
            "http://*.newrelic.com",
            "https://*.newrelic.com",
            "https://ajax.googleapis.com",
            "https://mozorg.cdn.mozilla.net",
            "https://www.google-analytics.com",
            options.personaHost
          ],
          'style-src': [
            "'self'",
            "http://mozorg.cdn.mozilla.net",
            "https://ajax.googleapis.com",
            "https://fonts.googleapis.com",
            "https://mozorg.cdn.mozilla.net",
            "https://netdna.bootstrapcdn.com"
          ]
        }
      });
    },
    /**
     * The operation for the / route is special,
     * and never an edit or remix.
     */
    setNewPageOperation: function(req, res, next) {
      req.body.pageOperation = "create";
      next();
    },

    /**
     * By default, the publish operation is to create a new
     * page. Later functions can override this behaviour.
     */
    setDefaultPublishOperation: function(req, res, next) {
      req.body.pageOperation = "remix";
      next();
    },

    /**
     * Override the default publication operation to act
     * as an update, rather than a create. This will lead
     * to old data being overwritten upon publication.
     */
    setPublishAsUpdate: function(req, res, next) {
      req.body.pageOperation = "edit";
      next();
    },

    /**
     * Check whether the requesting user is authenticated through Persona.
     */
    checkForAuth: function(req, res, next) {
      if (!req.session.user) {
        return next(utils.error(500, req.gettext("please log in first")));
      }
      next();
    },

    /**
     * Check to see whether a page request is actually for some page.
     */
    requestForId: function(req, res, next) {
      if(!req.params.id) {
        return next(utils.error(500, req.gettext("request did not point to a project")));
      }
      next();
    },

    /**
     * Check to see if a publish attempt actually has data for publishing.
     */
    checkForPublishData: function(req, res, next) {
      if(!req.body.html || req.body.html.trim() === "") {
        return next(utils.error(500, req.gettext("no data to publish")));
      }
      next();
    },

    /**
     * Ensure a publish has metadata. If not default it to an empty object.
     */
    ensureMetaData: function(req, res, next) {<commit_msg>Fixed #734 - Added auto redirects to the home page when a protected route is accessed by an unauthorized user<commit_after>var env = require('./environment');
var multer = require("multer");
var upload = multer({
  dest: require("os").tmpdir(),
  limits: {
    fileSize: env.get("MAX_FILE_SIZE_BYTES")
  }
});

module.exports = function middlewareConstructor() {
  var metrics = require('./metrics')(),
      utils = require("./utils"),
      hood = require("hood"),
      emulate_s3 = env.get('S3_EMULATION') || !env.get('S3_KEY'),
      knox = emulate_s3 ? require("noxmox").mox : require("knox"),
      Cryptr = require("cryptr");

  var cryptr = new Cryptr(env.get("SESSION_SECRET"));

  return {
    /**
     * Multipart File Upload of a single `brambleFile` form field
     */
    fileUpload: upload.single("brambleFile"),

    /**
     * Content Security Policy HTTP response header
     * helps you reduce XSS risks on modern browsers
     * by declaring what dynamic resources are allowed
     * to load via a HTTP Header.
     */
    addCSP: function ( options ) {
      return hood.csp({
        headers: [
          "Content-Security-Policy-Report-Only"
        ],
        policy: {
          'connect-src': [
            "'self'"
          ],
          'default-src': [
            "'self'"
          ],
          'frame-src': [
            "'self'",
            "https://docs.google.com",
            options.brambleHost,
            options.personaHost
          ],
          'font-src': [
            "'self'",
            "https://fonts.gstatic.com",
            "https://netdna.bootstrapcdn.com"
          ],
          'img-src': [
            "*"
          ],
          'media-src': [
            "*"
          ],
          'script-src': [
            "'self'",
            "http://mozorg.cdn.mozilla.net",
            "http://*.newrelic.com",
            "https://*.newrelic.com",
            "https://ajax.googleapis.com",
            "https://mozorg.cdn.mozilla.net",
            "https://www.google-analytics.com",
            options.personaHost
          ],
          'style-src': [
            "'self'",
            "http://mozorg.cdn.mozilla.net",
            "https://ajax.googleapis.com",
            "https://fonts.googleapis.com",
            "https://mozorg.cdn.mozilla.net",
            "https://netdna.bootstrapcdn.com"
          ]
        }
      });
    },
    /**
     * The operation for the / route is special,
     * and never an edit or remix.
     */
    setNewPageOperation: function(req, res, next) {
      req.body.pageOperation = "create";
      next();
    },

    /**
     * By default, the publish operation is to create a new
     * page. Later functions can override this behaviour.
     */
    setDefaultPublishOperation: function(req, res, next) {
      req.body.pageOperation = "remix";
      next();
    },

    /**
     * Override the default publication operation to act
     * as an update, rather than a create. This will lead
     * to old data being overwritten upon publication.
     */
    setPublishAsUpdate: function(req, res, next) {
      req.body.pageOperation = "edit";
      next();
    },

    /**
     * Check whether the requesting user has been authenticated.
     */
    checkForAuth: function(req, res, next) {
      if (!req.session.user) {
        return res.redirect(301, "/");
      }
      next();
    },

    /**
     * Check to see whether a page request is actually for some page.
     */
    requestForId: function(req, res, next) {
      if(!req.params.id) {
        return next(utils.error(500, req.gettext("request did not point to a project")));
      }
      next();
    },

    /**
     * Check to see if a publish attempt actually has data for publishing.
     */
    checkForPublishData: function(req, res, next) {
      if(!req.body.html || req.body.html.trim() === "") {
        return next(utils.error(500, req.gettext("no data to publish")));
      }
      next();
    },

    /**
     * Ensure a publish has metadata. If not default it to an empty object.
     */
    ensureMetaData: function(req, res, next) {
{'fraction_non_alphanumeric': 0.144991427871663, 'fraction_numerical': 0.0035513103110457996, 'mean_word_length': 1.824974057419578, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 22, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6492889', 'n_tokens_mistral': 2423, 'n_tokens_neox': 2363, 'n_words': 575}