github-advanced-security[bot] commented on code in PR #9138:
URL: https://github.com/apache/nifi/pull/9138#discussion_r1700931409


##########
nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/asset/StandardAssetManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.asset;
+
+import org.apache.nifi.nar.FileDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.HexFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardAssetManager implements AssetManager {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardAssetManager.class);
+
+    public static final String ASSET_STORAGE_LOCATION_PROPERTY = "directory";
+    public static final String DEFAULT_ASSET_STORAGE_LOCATION = "./assets";
+
+    private volatile File assetStorageLocation;
+    private final Map<String, Asset> assets = new ConcurrentHashMap<>();
+
+    @Override
+    public void initialize(final AssetManagerInitializationContext context) {
+        final String storageLocation = getStorageLocation(context);
+
+        assetStorageLocation = new File(storageLocation);
+        if (!assetStorageLocation.exists()) {
+            try {
+                Files.createDirectories(assetStorageLocation.toPath());
+            } catch (IOException e) {
+                throw new RuntimeException("The Asset Manager's [%s] property 
is set to [%s] but the directory does not exist and cannot be created"
+                        .formatted(ASSET_STORAGE_LOCATION_PROPERTY, 
storageLocation), e);
+            }
+        }
+
+        try {
+            recoverLocalAssets();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to access assets", e);
+        }
+    }
+
+    @Override
+    public Asset createAsset(final String parameterContextId, final String 
assetName, final InputStream contents) throws IOException {
+        final String assetId = createAssetId(parameterContextId, assetName);
+
+        final File file = getFile(parameterContextId, assetName);
+        final File dir = file.getParentFile();
+        if (!dir.exists()) {
+            try {
+                Files.createDirectories(dir.toPath());
+            } catch (final IOException ioe) {
+                throw new IOException("Could not create directory in order to 
store asset", ioe);
+            }
+        }
+
+        // Write contents to a temporary file, then move it to the final 
location.
+        // This allows us to avoid a situation where we upload a file, then we 
attempt to overwrite it but fail, leaving a corrupt asset.
+        final File tempFile = new File(dir, file.getName() + ".tmp");
+        logger.debug("Writing temp asset file [{}]", 
tempFile.getAbsolutePath());
+
+        try {
+            Files.copy(contents, tempFile.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+        } catch (final Exception e) {
+            throw new IOException("Failed to write asset to file " + 
tempFile.getAbsolutePath(), e);
+        }
+
+        Files.move(tempFile.toPath(), file.toPath(), 
StandardCopyOption.REPLACE_EXISTING);

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/77)



##########
nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/asset/StandardAssetManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.asset;
+
+import org.apache.nifi.nar.FileDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.HexFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardAssetManager implements AssetManager {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardAssetManager.class);
+
+    public static final String ASSET_STORAGE_LOCATION_PROPERTY = "directory";
+    public static final String DEFAULT_ASSET_STORAGE_LOCATION = "./assets";
+
+    private volatile File assetStorageLocation;
+    private final Map<String, Asset> assets = new ConcurrentHashMap<>();
+
+    @Override
+    public void initialize(final AssetManagerInitializationContext context) {
+        final String storageLocation = getStorageLocation(context);
+
+        assetStorageLocation = new File(storageLocation);
+        if (!assetStorageLocation.exists()) {
+            try {
+                Files.createDirectories(assetStorageLocation.toPath());
+            } catch (IOException e) {
+                throw new RuntimeException("The Asset Manager's [%s] property 
is set to [%s] but the directory does not exist and cannot be created"
+                        .formatted(ASSET_STORAGE_LOCATION_PROPERTY, 
storageLocation), e);
+            }
+        }
+
+        try {
+            recoverLocalAssets();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to access assets", e);
+        }
+    }
+
+    @Override
+    public Asset createAsset(final String parameterContextId, final String 
assetName, final InputStream contents) throws IOException {
+        final String assetId = createAssetId(parameterContextId, assetName);
+
+        final File file = getFile(parameterContextId, assetName);
+        final File dir = file.getParentFile();
+        if (!dir.exists()) {
+            try {
+                Files.createDirectories(dir.toPath());
+            } catch (final IOException ioe) {
+                throw new IOException("Could not create directory in order to 
store asset", ioe);
+            }
+        }
+
+        // Write contents to a temporary file, then move it to the final 
location.
+        // This allows us to avoid a situation where we upload a file, then we 
attempt to overwrite it but fail, leaving a corrupt asset.
+        final File tempFile = new File(dir, file.getName() + ".tmp");
+        logger.debug("Writing temp asset file [{}]", 
tempFile.getAbsolutePath());
+
+        try {
+            Files.copy(contents, tempFile.toPath(), 
StandardCopyOption.REPLACE_EXISTING);
+        } catch (final Exception e) {
+            throw new IOException("Failed to write asset to file " + 
tempFile.getAbsolutePath(), e);
+        }
+
+        Files.move(tempFile.toPath(), file.toPath(), 
StandardCopyOption.REPLACE_EXISTING);

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/76)



##########
nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/asset/StandardAssetManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.asset;
+
+import org.apache.nifi.nar.FileDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.HexFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardAssetManager implements AssetManager {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardAssetManager.class);
+
+    public static final String ASSET_STORAGE_LOCATION_PROPERTY = "directory";
+    public static final String DEFAULT_ASSET_STORAGE_LOCATION = "./assets";
+
+    private volatile File assetStorageLocation;
+    private final Map<String, Asset> assets = new ConcurrentHashMap<>();
+
+    @Override
+    public void initialize(final AssetManagerInitializationContext context) {
+        final String storageLocation = getStorageLocation(context);
+
+        assetStorageLocation = new File(storageLocation);
+        if (!assetStorageLocation.exists()) {
+            try {
+                Files.createDirectories(assetStorageLocation.toPath());
+            } catch (IOException e) {
+                throw new RuntimeException("The Asset Manager's [%s] property 
is set to [%s] but the directory does not exist and cannot be created"
+                        .formatted(ASSET_STORAGE_LOCATION_PROPERTY, 
storageLocation), e);
+            }
+        }
+
+        try {
+            recoverLocalAssets();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to access assets", e);
+        }
+    }
+
+    @Override
+    public Asset createAsset(final String parameterContextId, final String 
assetName, final InputStream contents) throws IOException {
+        final String assetId = createAssetId(parameterContextId, assetName);
+
+        final File file = getFile(parameterContextId, assetName);
+        final File dir = file.getParentFile();
+        if (!dir.exists()) {

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/73)



##########
nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/asset/StandardAssetManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.asset;
+
+import org.apache.nifi.nar.FileDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.HexFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardAssetManager implements AssetManager {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardAssetManager.class);
+
+    public static final String ASSET_STORAGE_LOCATION_PROPERTY = "directory";
+    public static final String DEFAULT_ASSET_STORAGE_LOCATION = "./assets";
+
+    private volatile File assetStorageLocation;
+    private final Map<String, Asset> assets = new ConcurrentHashMap<>();
+
+    @Override
+    public void initialize(final AssetManagerInitializationContext context) {
+        final String storageLocation = getStorageLocation(context);
+
+        assetStorageLocation = new File(storageLocation);
+        if (!assetStorageLocation.exists()) {
+            try {
+                Files.createDirectories(assetStorageLocation.toPath());
+            } catch (IOException e) {
+                throw new RuntimeException("The Asset Manager's [%s] property 
is set to [%s] but the directory does not exist and cannot be created"
+                        .formatted(ASSET_STORAGE_LOCATION_PROPERTY, 
storageLocation), e);
+            }
+        }
+
+        try {
+            recoverLocalAssets();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to access assets", e);
+        }
+    }
+
+    @Override
+    public Asset createAsset(final String parameterContextId, final String 
assetName, final InputStream contents) throws IOException {
+        final String assetId = createAssetId(parameterContextId, assetName);
+
+        final File file = getFile(parameterContextId, assetName);
+        final File dir = file.getParentFile();
+        if (!dir.exists()) {
+            try {
+                Files.createDirectories(dir.toPath());

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/74)



##########
nifi-framework-bundle/nifi-framework/nifi-web/nifi-web-api/src/main/java/org/apache/nifi/web/api/dto/DtoFactory.java:
##########
@@ -1509,6 +1512,26 @@
        return config;
    }
 
+   public AssetEntity createAssetEntity(final Asset asset) {
+         final AssetEntity entity = new AssetEntity();
+         entity.setAsset(createAssetDto(asset));
+         return entity;
+   }
+
+   public AssetDTO createAssetDto(final Asset asset) {
+       final File assetFile = asset.getFile();
+       final AssetDTO dto = new AssetDTO();
+       dto.setId(asset.getIdentifier());
+       dto.setName(asset.getName());
+       dto.setDigest(asset.getDigest().orElse(null));
+       dto.setMissingContent(!assetFile.exists());

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/79)



##########
nifi-framework-bundle/nifi-framework/nifi-framework-components/src/main/java/org/apache/nifi/asset/StandardAssetManager.java:
##########
@@ -0,0 +1,200 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.nifi.asset;
+
+import org.apache.nifi.nar.FileDigestUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.StandardCopyOption;
+import java.util.ArrayList;
+import java.util.HexFormat;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.UUID;
+import java.util.concurrent.ConcurrentHashMap;
+
+public class StandardAssetManager implements AssetManager {
+    private static final Logger logger = 
LoggerFactory.getLogger(StandardAssetManager.class);
+
+    public static final String ASSET_STORAGE_LOCATION_PROPERTY = "directory";
+    public static final String DEFAULT_ASSET_STORAGE_LOCATION = "./assets";
+
+    private volatile File assetStorageLocation;
+    private final Map<String, Asset> assets = new ConcurrentHashMap<>();
+
+    @Override
+    public void initialize(final AssetManagerInitializationContext context) {
+        final String storageLocation = getStorageLocation(context);
+
+        assetStorageLocation = new File(storageLocation);
+        if (!assetStorageLocation.exists()) {
+            try {
+                Files.createDirectories(assetStorageLocation.toPath());
+            } catch (IOException e) {
+                throw new RuntimeException("The Asset Manager's [%s] property 
is set to [%s] but the directory does not exist and cannot be created"
+                        .formatted(ASSET_STORAGE_LOCATION_PROPERTY, 
storageLocation), e);
+            }
+        }
+
+        try {
+            recoverLocalAssets();
+        } catch (final IOException e) {
+            throw new RuntimeException("Unable to access assets", e);
+        }
+    }
+
+    @Override
+    public Asset createAsset(final String parameterContextId, final String 
assetName, final InputStream contents) throws IOException {
+        final String assetId = createAssetId(parameterContextId, assetName);
+
+        final File file = getFile(parameterContextId, assetName);
+        final File dir = file.getParentFile();
+        if (!dir.exists()) {
+            try {
+                Files.createDirectories(dir.toPath());
+            } catch (final IOException ioe) {
+                throw new IOException("Could not create directory in order to 
store asset", ioe);
+            }
+        }
+
+        // Write contents to a temporary file, then move it to the final 
location.
+        // This allows us to avoid a situation where we upload a file, then we 
attempt to overwrite it but fail, leaving a corrupt asset.
+        final File tempFile = new File(dir, file.getName() + ".tmp");
+        logger.debug("Writing temp asset file [{}]", 
tempFile.getAbsolutePath());
+
+        try {
+            Files.copy(contents, tempFile.toPath(), 
StandardCopyOption.REPLACE_EXISTING);

Review Comment:
   ## Uncontrolled data used in path expression
   
   This path depends on a [user-provided value](1).
   This path depends on a [user-provided value](2).
   
   [Show more details](https://github.com/apache/nifi/security/code-scanning/75)



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to