Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 80 additions & 50 deletions android/src/main/java/cn/reactnative/modules/update/DownloadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -252,19 +252,41 @@ private void doFullPatch(DownloadTaskParams param) throws IOException {
}
}

private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy, HashMap<String, ArrayList<File>> resToCopy2) throws IOException {
private void copyFromResource(HashMap<String, ArrayList<File> > resToCopy) throws IOException {
SafeZipFile zipFile = new SafeZipFile(new File(context.getPackageResourcePath()));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();

String fn = ze.getName();
ArrayList<File> targets = resToCopy.get(fn);
if (targets != null) {
File lastTarget = null;
for (File target: targets) {
if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Copying from resource " + fn + " to " + target);
}
if (lastTarget != null) {
copyFile(lastTarget, target);
} else {
zipFile.unzipToFile(ze, target);
lastTarget = target;
}
}
}
}
zipFile.close();
}

private void copyFromResourceV2(HashMap<String, ArrayList<File>> resToCopy2) throws IOException {
SafeZipFile zipFile = new SafeZipFile(new File(context.getPackageResourcePath()));
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry ze = entries.nextElement();
String fn = ze.getName();
long zipCrc32 = ze.getCrc();
String crc32Decimal = getCRC32AsDecimal(zipCrc32);
ArrayList<File> targets = resToCopy2.get(crc32Decimal);
if(targets==null || targets.isEmpty()){
targets = resToCopy.get(fn);
}
if (targets != null) {
File lastTarget = null;
for (File target: targets) {
Expand All @@ -290,6 +312,7 @@ private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONEx
param.unzipDirectory.mkdirs();
HashMap<String, ArrayList<File>> copyList = new HashMap<String, ArrayList<File>>();
HashMap<String, ArrayList<File>> copiesv2List = new HashMap<String, ArrayList<File>>();
Boolean isV2 = false;
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use primitive boolean instead of wrapper Boolean for simple flag variables. This avoids unnecessary object allocation and potential null pointer issues.

Suggested change
Boolean isV2 = false;
boolean isV2 = false;

Copilot uses AI. Check for mistakes.


boolean foundDiff = false;
boolean foundBundlePatch = false;
Expand All @@ -310,53 +333,56 @@ private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONEx
JSONObject copies = obj.getJSONObject("copies");
JSONObject copiesv2 = obj.getJSONObject("copiesv2");
Iterator<?> keys = copies.keys();
Iterator<?> keys2 = copiesv2.keys();
while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
Iterator<?> keysV2 = copiesv2.keys();
if(keysV2.hasNext()){
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Missing space after if keyword. Consider using if (keysV2.hasNext()) for consistent code formatting.

Suggested change
if(keysV2.hasNext()){
if (keysV2.hasNext()) {

Copilot uses AI. Check for mistakes.

isV2 = true;
while( keysV2.hasNext() ) {
String from = (String)keysV2.next();
String to = copiesv2.getString(from);
if (from.isEmpty()) {
from = to;
}
ArrayList<File> target = null;
if (!copiesv2List.containsKey(from)) {
target = new ArrayList<File>();
copiesv2List.put(from, target);
} else {
target = copiesv2List.get((from));
}
File toFile = new File(param.unzipDirectory, to);

// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
}
ArrayList<File> target = null;
if (!copyList.containsKey(from)) {
target = new ArrayList<File>();
copyList.put(from, target);
} else {
target = copyList.get((from));
}
File toFile = new File(param.unzipDirectory, to);

// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}else{
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Missing space before opening brace. Consider using } else { for consistent code formatting.

Suggested change
}else{
} else {

Copilot uses AI. Check for mistakes.

while( keys.hasNext() ) {
String to = (String)keys.next();
String from = copies.getString(to);
if (from.isEmpty()) {
from = to;
}
ArrayList<File> target = null;
if (!copyList.containsKey(from)) {
target = new ArrayList<File>();
copyList.put(from, target);
} else {
target = copyList.get((from));
}
File toFile = new File(param.unzipDirectory, to);

// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
}
target.add(toFile);
}

while( keys2.hasNext() ) {
String from = (String)keys2.next();
String to = copiesv2.getString(from);
if (from.isEmpty()) {
from = to;
}
ArrayList<File> target = null;
if (!copiesv2List.containsKey(from)) {
target = new ArrayList<File>();
copiesv2List.put(from, target);
} else {
target = copiesv2List.get((from));
}
File toFile = new File(param.unzipDirectory, to);

// Fixing a Zip Path Traversal Vulnerability
// https://support.google.com/faqs/answer/9294009
String canonicalPath = toFile.getCanonicalPath();
if (!canonicalPath.startsWith(param.unzipDirectory.getCanonicalPath() + File.separator)) {
throw new SecurityException("Illegal name: " + to);
}
target.add(toFile);
}
continue;
}
Expand Down Expand Up @@ -385,7 +411,11 @@ private void doPatchFromApk(DownloadTaskParams param) throws IOException, JSONEx
throw new Error("bundle patch not found");
}

copyFromResource(copyList, copiesv2List);
if(isV2){
copyFromResourceV2(copiesv2List);
}else{
Comment on lines +414 to +416
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Missing spaces around if and else keywords. Consider using if (isV2) { and } else { for consistent code formatting.

Suggested change
if(isV2){
copyFromResourceV2(copiesv2List);
}else{
if (isV2) {
copyFromResourceV2(copiesv2List);
} else {

Copilot uses AI. Check for mistakes.

copyFromResource(copyList);
}

if (UpdateContext.DEBUG) {
Log.d("react-native-update", "Unzip finished");
Expand Down