修改 bug
这个提交包含在:
+1
-1
@@ -24,7 +24,7 @@ final class ReportingArchiveSafety {
|
||||
if(e.isDirectory()){if(!"unStruct/".equals(n))throw invalid();continue;}
|
||||
byte[] data=zip.readNBytes(10*1024*1024+1);expanded+=data.length;
|
||||
if(data.length>10*1024*1024||expanded>50*1024*1024||expanded>Math.max(1024*1024,bytes.length*100L))throw invalid();
|
||||
if(n.matches("[A-Za-z_][A-Za-z0-9_.-]{0,127}\\.xml")){xml(data);xmls++;}
|
||||
if(n.matches("[A-Za-z0-9_][A-Za-z0-9_.-]{0,127}\\.xml")){xml(data);xmls++;}
|
||||
else {
|
||||
if(!n.startsWith("unStruct/"))throw invalid();
|
||||
String name=n.substring(9);ReportingAttachmentPolicy.verify(name,data);
|
||||
|
||||
+24
-3
@@ -18,15 +18,33 @@ class ReportingPrivateFileLifecycleTest {
|
||||
@TempDir Path directory;
|
||||
String sha(byte[] bytes)throws Exception{return HexFormat.of().formatHex(MessageDigest.getInstance("SHA-256").digest(bytes));}
|
||||
@Test void stageAppendFinishStatusAndDownloadAcrossTwoReadersOfSameStore()throws Exception{
|
||||
lifecycle("{\"records\":[{\"name\":\"中文快照\"}]}".getBytes(StandardCharsets.UTF_8),"snapshot.json","application/json",FileUse.EVIDENCE);
|
||||
}
|
||||
@Test void threeResourcePackageFinishesSameOperationAndDownloadsIntact()throws Exception{
|
||||
// Synthetic records matching the reported names/counts; not the unavailable production ZIP.
|
||||
var output=new java.io.ByteArrayOutputStream();
|
||||
String suffix="_1000_20261202102546_cd49cb9e37de4af9b72866c4e63d9ef8";
|
||||
String[] resources={"APATTACHEDCY","APATTACHEDKCJX","APATTACHEDRJ"};int[] counts={3,13,10};
|
||||
try(var zip=new java.util.zip.ZipOutputStream(output)){
|
||||
for(int i=0;i<resources.length;i++){
|
||||
zip.putNextEntry(new java.util.zip.ZipEntry("91361200MADLEERM66_"+resources[i]+suffix+".xml"));
|
||||
var xml=new StringBuilder("<Package><Records>");
|
||||
for(int row=0;row<counts[i];row++)xml.append("<Record><EXT_GUID>").append(9000000000000000000L+row).append("</EXT_GUID></Record>");
|
||||
zip.write(xml.append("</Records></Package>").toString().getBytes(StandardCharsets.UTF_8));zip.closeEntry();
|
||||
}
|
||||
}
|
||||
lifecycle(output.toByteArray(),"91361200MADLEERM66_API"+suffix+".zip","application/zip",FileUse.DELIVERY_PACKAGE);
|
||||
}
|
||||
void lifecycle(byte[] bytes,String filename,String mime,FileUse use)throws Exception{
|
||||
var repository=mock(PrivateBusinessFileJdbcRepository.class);
|
||||
var auth=mock(PrivateBusinessFileAuthorizationGateway.class);
|
||||
String root=directory.toRealPath().toString();
|
||||
var storage=new DataGovLocalFileStorage(true,root,10485760);
|
||||
var service=new PrivateBusinessFileRpcService(new PrivateBusinessFileService(repository,storage,auth));
|
||||
var other=new PrivateBusinessFileRpcService(new PrivateBusinessFileService(repository,new DataGovLocalFileStorage(true,root,10485760),auth));
|
||||
byte[] bytes="{\"records\":[{\"name\":\"中文快照\"}]}".getBytes(StandardCharsets.UTF_8);String hash=sha(bytes);
|
||||
String hash=sha(bytes);
|
||||
var context=new Context(1,7,9,9,"integration","reporting:test","request-1");
|
||||
var target=new Target("integration","REGULATORY_REPORTING_ARTIFACT","artifact",1,FileUse.EVIDENCE,"REGULATORY_REPORTING","batch","snapshot.json",hash,bytes.length,"application/json",null,null);
|
||||
var target=new Target("integration","REGULATORY_REPORTING_ARTIFACT","artifact",1,use,"REGULATORY_REPORTING","batch",filename,hash,bytes.length,mime,null,null);
|
||||
var request=new StageRequest(context,target,"a".repeat(64),hash);var query=new WriteQuery(context,request.operationKey());
|
||||
var current=new AtomicReference<PrivateBusinessFileJdbcRepository.Stored>();
|
||||
when(repository.transaction(any())).thenAnswer(call->((Supplier<?>)call.getArgument(0)).get());
|
||||
@@ -39,11 +57,14 @@ class ReportingPrivateFileLifecycleTest {
|
||||
when(auth.require(any())).thenAnswer(call->{AuthorizationQuery q=call.getArgument(0);var now=java.time.Instant.now();return new AuthorizationDecision(q,State.AVAILABLE,"decision",1,"policy",now,now.plusSeconds(5),true,null);});
|
||||
var staged=service.stage(request);assertEquals(WriteState.PREPARING,staged.state());assertEquals(staged.binding(),service.stage(request).binding());
|
||||
var append=new AppendRequest(query,0,bytes,hash);assertEquals(bytes.length,service.append(append).nextOffset());assertEquals(bytes.length,service.append(append).nextOffset());
|
||||
assertEquals(WriteState.PREPARING,other.status(query).state());
|
||||
assertEquals(WriteState.STAGED,other.finish(query).state());assertEquals(WriteState.STAGED,service.status(query).state());
|
||||
assertEquals(staged.binding(),service.finish(query).binding());
|
||||
assertArrayEquals(bytes,other.readStaged(new StagedRead(query,0,65536)).bytes());
|
||||
assertEquals(WriteState.BOUND,service.complete(query).state());assertEquals(WriteState.BOUND,other.complete(query).state());
|
||||
var file=new FileQuery(context,staged.binding(),target.purpose(),target.recipientRef());
|
||||
assertArrayEquals(bytes,other.readChunk(new ChunkQuery(file,0,65536)).bytes());assertEquals(WriteState.BOUND,other.status(query).state());
|
||||
byte[] downloaded=other.readChunk(new ChunkQuery(file,0,65536)).bytes();
|
||||
assertArrayEquals(bytes,downloaded);assertEquals(hash,sha(downloaded));assertEquals(WriteState.BOUND,other.status(query).state());
|
||||
assertThrows(IllegalArgumentException.class,()->storage.read(current.get().storageKey()));
|
||||
// A node pointing to another directory must fail instead of treating a DB binding as readable bytes.
|
||||
var wrong=new PrivateBusinessFileRpcService(new PrivateBusinessFileService(repository,new DataGovLocalFileStorage(true,directory.resolve("not-shared").toString(),10485760),auth));
|
||||
|
||||
+20
@@ -1,6 +1,8 @@
|
||||
package cn.aixunkeji.resource.storage;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.ValueSource;
|
||||
import java.io.*;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.zip.*;
|
||||
@@ -11,6 +13,24 @@ class ReportingArchiveSafetyTest {
|
||||
@Test void attachmentMustMatchDigestAndPassContentValidation()throws Exception{byte[] body="evidence".getBytes(StandardCharsets.UTF_8);String name=cn.aixunkeji.resource.api.assetupgrade.ReportingAttachmentPolicy.md5(body)+"_proof.txt";var calls=new java.util.ArrayList<String>();ReportingArchiveSafety.check(packageWith("unStruct/"+name,body),false,(n,b,m)->{calls.add(n);assertEquals("text/plain",m);assertArrayEquals(body,b);});assertEquals(java.util.List.of(name),calls);assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(packageWith("unStruct/"+name,new byte[]{1}),false));assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(packageWith("unStruct/"+name,body),false,(n,b,m)->{throw new IllegalArgumentException("CONTENT_INVALID");}));}
|
||||
byte[] zip(String name,String body)throws Exception{var output=new ByteArrayOutputStream();try(var zip=new ZipOutputStream(output)){zip.putNextEntry(new ZipEntry(name));zip.write(body.getBytes(StandardCharsets.UTF_8));zip.closeEntry();}return output.toByteArray();}
|
||||
@Test void acceptsFlatXmlPackage()throws Exception{assertDoesNotThrow(()->ReportingArchiveSafety.check(zip("R.xml","<Package><Records/></Package>"),false));}
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"APATTACHEDCY","APATTACHEDKCJX","APATTACHEDRJ"})
|
||||
void acceptsCreditCodePrefixedXml(String resource)throws Exception{
|
||||
String name="91361200MADLEERM66_"+resource+"_1000_20261202102546_cd49cb9e37de4af9b72866c4e63d9ef8.xml";
|
||||
assertDoesNotThrow(()->ReportingArchiveSafety.check(zip(name,"<Package><Records/></Package>"),false));
|
||||
}
|
||||
@ParameterizedTest
|
||||
@ValueSource(strings={"/9.xml","../9.xml","nested/9.xml","C:9.xml","nested\\9.xml","9.txt",".9.xml","unStruct/9.xml"})
|
||||
void rejectsInvalidPathsAndPayloadNames(String name)throws Exception{
|
||||
assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip(name,"<Package/>"),false));
|
||||
}
|
||||
@Test void rejectsDuplicateNamesIgnoringCase()throws Exception{
|
||||
assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(packageWith("r.xml","<Package/>".getBytes(StandardCharsets.UTF_8)),false));
|
||||
}
|
||||
@Test void numericPrefixDoesNotBypassXmlSafety()throws Exception{
|
||||
assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip("91361200MADLEERM66.xml","<!DOCTYPE x [<!ENTITY a SYSTEM 'file:///etc/passwd'>]><x>&a;</x>"),false));
|
||||
assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip("91361200MADLEERM66.xml","<Package>"),false));
|
||||
}
|
||||
@Test void rejectsExternalEntities()throws Exception{assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip("R.xml","<!DOCTYPE x [<!ENTITY a SYSTEM 'file:///etc/passwd'>]><x>&a;</x>"),false));}
|
||||
@Test void rejectsTraversalAndNonXmlPayload()throws Exception{assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip("../R.xml","<x/>"),false));assertThrows(IllegalArgumentException.class,()->ReportingArchiveSafety.check(zip("run.sh","echo test"),false));}
|
||||
}
|
||||
|
||||
在新工单中引用
屏蔽一个用户