Skip to content

Commit

Permalink
[backend] Infinite loop still possible in some use case for platform …
Browse files Browse the repository at this point in the history
…stream sync for files (#6212)
  • Loading branch information
richard-julien committed Mar 1, 2024
1 parent 45a67f9 commit 11dabc7
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
12 changes: 6 additions & 6 deletions opencti-platform/opencti-graphql/src/database/file-storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,17 @@ export const uploadJobImport = async (context, user, fileId, fileMime, entityId,
};

export const upload = async (context, user, filePath, fileUpload, opts) => {
const { entity, meta = {}, noTriggerImport = false, fileVersion, errorOnExisting = false } = opts;
const { entity, meta = {}, noTriggerImport = false, errorOnExisting = false } = opts;
const metadata = { ...meta };
if (!metadata.version) {
metadata.version = now();
}
const { createReadStream, filename, mimetype, encoding = '' } = await fileUpload;
const truncatedFileName = `${truncate(path.parse(filename).name, 200, false)}${truncate(path.parse(filename).ext, 10, false)}`;
const key = `${filePath}/${truncatedFileName}`;
const currentFile = await documentFindById(context, user, key);
if (currentFile) {
if (currentFile.metaData?.version === fileVersion) {
if (currentFile.metaData?.version === metadata.version) {
return { upload: currentFile, untouched: true };
}
if (errorOnExisting) {
Expand All @@ -337,10 +341,6 @@ export const upload = async (context, user, filePath, fileUpload, opts) => {
// Upload the data
const readStream = createReadStream();
const fileMime = guessMimeType(key) || mimetype;
const metadata = { ...meta };
if (!metadata.version) {
metadata.version = now();
}
const fullMetadata = {
...metadata,
filename: encodeURIComponent(truncatedFileName),
Expand Down
4 changes: 2 additions & 2 deletions opencti-platform/opencti-graphql/src/domain/stixCoreObject.js
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,12 @@ export const stixCoreObjectImportPush = async (context, user, id, file, args = {
const isAutoExternal = !entitySetting ? false : entitySetting.platform_entity_files_ref;
const filePath = `import/${previous.entity_type}/${internalId}`;
// 01. Upload the file
const meta = {};
const meta = { version: fileVersion };
if (isAutoExternal) {
const key = `${filePath}/${filename}`;
meta.external_reference_id = generateStandardId(ENTITY_TYPE_EXTERNAL_REFERENCE, { url: `/storage/get/${key}` });
}
const { upload: up, untouched } = await upload(context, user, filePath, file, { meta, noTriggerImport, fileVersion, entity: previous });
const { upload: up, untouched } = await upload(context, user, filePath, file, { meta, noTriggerImport, entity: previous });
if (untouched) {
// When synchronizing the version can be the same.
// If it's the case, just return without any x_opencti_files modifications
Expand Down

0 comments on commit 11dabc7

Please sign in to comment.