github-advanced-security[bot] commented on code in PR #3301:
URL: https://github.com/apache/jackrabbit-oak/pull/3301#discussion_r4598731230


##########
oak-segment-aws/src/main/java/org/apache/jackrabbit/oak/segment/aws/tool/AwsSegmentStoreMigrator.java:
##########
@@ -198,18 +209,42 @@
             }, 16, 5)));
         }
 
+        Map<UUID, ByteBuffer> segments = new LinkedHashMap<>();
         for (Future<Segment> future : futures) {
             Segment segment = future.get();
             segment.write(writer);
+            segments.put(segment.id(), segment.data);
         }
+        return segments;
     }
 
-    private void migrateBinaryRef(SegmentArchiveReader reader, 
SegmentArchiveWriter writer) throws IOException {
+    private void migrateBinaryRef(String archiveName, Map<UUID, ByteBuffer> 
segments, SegmentArchiveReader reader,
+                                  SegmentArchiveWriter writer) throws 
IOException {
         ByteBuffer binaryReferences = reader.getBinaryReferences();
+        byte[] array;
         if (binaryReferences != null) {
-            byte[] array = fetchByteArray(binaryReferences);
-            writer.writeBinaryReferences(array);
+            array = fetchByteArray(binaryReferences);
+        } else {
+            log.warn("{}/{} has no binary references, recovering them from its 
segments", sourceName, archiveName);
+            array = sourceStore().recoverBinaryReferences(segments);
         }
+        writer.writeBinaryReferences(array);
+    }
+
+    /**
+     * Opened only when an archive without binary references is migrated. It 
must be read-only:
+     * a writable store would recover the source archives and modify the 
source.
+     */
+    private ReadOnlyFileStore sourceStore() throws IOException {
+        if (sourceStore == null) {
+            sourceStoreDirectory = 
Files.createTempDirectory("segment-store-migrator").toFile();

Review Comment:
   ## SonarCloud / Temporary files should not be created in publicly writable 
directories
   
   <!--SONAR_ISSUE_KEY:AaHpMSaohVYzpZp0V5db-->Make sure publicly writable 
directories are used safely here. <p>See more on <a 
href="https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&issues=AaHpMSaohVYzpZp0V5db&open=AaHpMSaohVYzpZp0V5db&pullRequest=3301";>SonarQube
 Cloud</a></p>
   
   [Show more 
details](https://github.com/apache/jackrabbit-oak/security/code-scanning/198)



##########
oak-segment-azure/src/main/java/org/apache/jackrabbit/oak/segment/azure/tool/SegmentStoreMigrator.java:
##########
@@ -205,19 +216,43 @@
             })));
         }
 
+        Map<UUID, ByteBuffer> segments = new LinkedHashMap<>();
         for (Future<Segment> future : futures) {
             Segment segment = future.get();
             RETRIER.execute(() -> segment.write(writer));
+            segments.put(segment.id(), segment.data);
         }
+        return segments;
     }
 
-    private void migrateBinaryRef(SegmentArchiveReader reader, 
SegmentArchiveWriter writer) throws IOException, ExecutionException, 
InterruptedException {
+    private void migrateBinaryRef(String archiveName, Map<UUID, ByteBuffer> 
segments, SegmentArchiveReader reader,
+                                  SegmentArchiveWriter writer) throws 
IOException, ExecutionException, InterruptedException {
         Future<ByteBuffer> future = executor.submit(() -> 
RETRIER.execute(reader::getBinaryReferences));
         ByteBuffer binaryReferences = future.get();
+        byte[] array;
         if (binaryReferences != null) {
-            byte[] array = fetchByteArray(binaryReferences);
-            RETRIER.execute(() -> writer.writeBinaryReferences(array));
+            array = fetchByteArray(binaryReferences);
+        } else {
+            log.warn("{}/{} has no binary references, recovering them from its 
segments", sourceName, archiveName);
+            array = sourceStore().recoverBinaryReferences(segments);
         }
+        RETRIER.execute(() -> writer.writeBinaryReferences(array));
+    }
+
+    /**
+     * Opened only when an archive without binary references is migrated. It 
must be read-only:
+     * a writable store would recover the source archives and modify the 
source.
+     */
+    private ReadOnlyFileStore sourceStore() throws IOException {
+        if (sourceStore == null) {
+            sourceStoreDirectory = 
Files.createTempDirectory("segment-store-migrator").toFile();

Review Comment:
   ## SonarCloud / Temporary files should not be created in publicly writable 
directories
   
   <!--SONAR_ISSUE_KEY:AaHpMSaGhVYzpZp0V5da-->Make sure publicly writable 
directories are used safely here. <p>See more on <a 
href="https://sonarcloud.io/project/issues?id=org.apache.jackrabbit%3Ajackrabbit-oak&issues=AaHpMSaGhVYzpZp0V5da&open=AaHpMSaGhVYzpZp0V5da&pullRequest=3301";>SonarQube
 Cloud</a></p>
   
   [Show more 
details](https://github.com/apache/jackrabbit-oak/security/code-scanning/199)



-- 
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