Skip to content

Commit

Permalink
removed "sanitize" flag
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewplummer committed Dec 10, 2024
1 parent a110966 commit debb274
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 58 deletions.
52 changes: 2 additions & 50 deletions services/api/scripts/database/prepare-export.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const logger = require('@bedrockio/logger');
const { User } = require('../../src/models');
const { initialize } = require('../../src/utils/database');

const MONGO_URI = config.get('MONGO_URI');

program
.description(
`
Expand All @@ -34,8 +36,6 @@ program
program.parse(process.argv);
const options = program.opts();

const MONGO_URI = config.get('MONGO_URI');

async function run() {
const db = await initialize();

Expand Down Expand Up @@ -120,12 +120,7 @@ async function getSanitizations(options) {
if (options.raw) {
return [];
}
const manual = await getManualSanitizations();
const auto = await getAutoSanitizations();
return [...manual, ...auto];
}

async function getManualSanitizations() {
const gl = path.resolve(__dirname, 'sanitizations/*.{json,js}');
const files = await glob(gl);
const result = [];
Expand All @@ -152,49 +147,6 @@ async function getManualSanitizations() {
return result;
}

async function getAutoSanitizations() {
const result = [];

for (let model of Object.values(mongoose.models)) {
const collection = model.collection.name;

if (isPluginCollection(collection)) {
continue;
}

const fields = {};

for (let [name, path] of Object.entries(model.schema.paths)) {
const sanitize = path.options?.sanitize;
const remove = sanitize === true;
if (sanitize) {
fields[name] = {
$cond: {
if: {
$ne: [`$${name}`, null],
},
then: remove ? '$$REMOVE' : sanitize,
// Effectively doesn't set the field if it does not exist.
else: '$$REMOVE',
},
};
}
}
if (Object.keys(fields).length > 0) {
result.push({
name: getSanitizedName(collection),
collection,
pipeline: [
{
$set: fields,
},
],
});
}
}
return result;
}

async function runSanitizations(db, sanitizations) {
for (let sanitization of sanitizations) {
const { collection, pipeline } = sanitization;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"collection": "applicationcredentials",
"pipeline": [
{
"$unset": "apiSecret"
}
]
}
7 changes: 0 additions & 7 deletions services/api/scripts/database/sanitizations/users.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
// Note that this is an example of a complex sanitization pipeline.
// For a simple case the "sanitize" key can be set on individual fields
// in a model definition:

// - When `true` the value will be stripped from all documents.
// - When a string the value will be set to a literal for all documents.

const bcrypt = require('bcrypt');

// Development password. Note this is hard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"type": "String",
"trim": true,
"required": true,
"sanitize": true,
"unique": true,
"writeAccess": "none"
},
Expand Down

0 comments on commit debb274

Please sign in to comment.