-
Notifications
You must be signed in to change notification settings - Fork 0
fix: HWPX 업로드 실제 파일 구조 검증 (ZIP·mimetype·section0.xml) #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
28f1d89
feat(file): HwpxSignatureValidator 추가, FileService에 .hwpx 전용 검증 분기 적용
chaeliki 96c2cdc
test(file): HwpxSignatureValidator 단위 테스트 6개 추가
chaeliki c0b2646
test(file): HWPX 실제 구조 검증 통합 테스트 3개 (정상/가짜MIME/구조없음)
chaeliki 91dc37e
Merge branch 'main' into fix/109-hwpx-signature-validation
hywznn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/main/java/com/fowoco/server/file/application/validation/HwpxSignatureValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| package com.fowoco.server.file.application.validation; | ||
|
|
||
| import java.io.ByteArrayInputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipInputStream; | ||
| import org.springframework.stereotype.Component; | ||
|
|
||
| /** | ||
| * HWPX는 ZIP(OWPML) 구조이며, 정식 MIME 타입은 있지만(application/hwp+zip) | ||
| * 클라이언트가 보내는 값을 신뢰하지 않는다. 실제 압축 내부에 | ||
| * 최상위 "mimetype" 항목의 값이 "application/hwp+zip"인지, 그리고 | ||
| * 본문 콘텐츠("Contents/section0.xml" 또는 호환 경로)가 있는지 확인한다. | ||
| */ | ||
| @Component | ||
| public class HwpxSignatureValidator { | ||
|
|
||
| private static final String MIMETYPE_ENTRY_NAME = "mimetype"; | ||
| private static final String EXPECTED_MIMETYPE = "application/hwp+zip"; | ||
| private static final String SECTION_ENTRY_PREFIX = "Contents/section"; | ||
| private static final String SECTION_ENTRY_SUFFIX = ".xml"; | ||
|
|
||
| public boolean isValidHwpx(byte[] content) { | ||
| boolean mimetypeMatched = false; | ||
| boolean sectionFound = false; | ||
|
|
||
| try (ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(content))) { | ||
| ZipEntry entry; | ||
| while ((entry = zipInputStream.getNextEntry()) != null) { | ||
| String entryName = entry.getName(); | ||
| if (MIMETYPE_ENTRY_NAME.equals(entryName)) { | ||
| mimetypeMatched = EXPECTED_MIMETYPE.equals(readEntryAsString(zipInputStream).strip()); | ||
| } else if (isSectionEntry(entryName)) { | ||
| sectionFound = true; | ||
| } | ||
| } | ||
| } catch (IOException | IllegalArgumentException exception) { | ||
| return false; | ||
| } | ||
|
|
||
| return mimetypeMatched && sectionFound; | ||
| } | ||
|
|
||
| private boolean isSectionEntry(String entryName) { | ||
| return entryName != null | ||
| && entryName.startsWith(SECTION_ENTRY_PREFIX) | ||
| && entryName.endsWith(SECTION_ENTRY_SUFFIX); | ||
| } | ||
|
|
||
| private String readEntryAsString(InputStream stream) throws IOException { | ||
| return new String(stream.readAllBytes(), StandardCharsets.UTF_8); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/test/java/com/fowoco/server/file/application/validation/HwpxSignatureValidatorTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package com.fowoco.server.file.application.validation; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.zip.ZipEntry; | ||
| import java.util.zip.ZipOutputStream; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class HwpxSignatureValidatorTest { | ||
|
|
||
| private final HwpxSignatureValidator validator = new HwpxSignatureValidator(); | ||
|
|
||
| @Test | ||
| void acceptsValidHwpxStructure() throws Exception { | ||
| byte[] content = buildZip("application/hwp+zip", "Contents/section0.xml"); | ||
|
|
||
| assertThat(validator.isValidHwpx(content)).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsWrongMimetypeEntry() throws Exception { | ||
| byte[] content = buildZip("application/zip", "Contents/section0.xml"); | ||
|
|
||
| assertThat(validator.isValidHwpx(content)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsMissingSectionXml() throws Exception { | ||
| byte[] content = buildZip("application/hwp+zip", null); | ||
|
|
||
| assertThat(validator.isValidHwpx(content)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsNonZipContent() { | ||
| byte[] content = "not a zip file at all".getBytes(StandardCharsets.UTF_8); | ||
|
|
||
| assertThat(validator.isValidHwpx(content)).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void rejectsEmptyContent() { | ||
| assertThat(validator.isValidHwpx(new byte[0])).isFalse(); | ||
| } | ||
|
|
||
| @Test | ||
| void acceptsAlternateSectionNumber() throws Exception { | ||
| byte[] content = buildZip("application/hwp+zip", "Contents/section1.xml"); | ||
|
|
||
| assertThat(validator.isValidHwpx(content)).isTrue(); | ||
| } | ||
|
|
||
| private byte[] buildZip(String mimetypeValue, String sectionEntryName) throws Exception { | ||
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | ||
| try (ZipOutputStream zip = new ZipOutputStream(out)) { | ||
| zip.putNextEntry(new ZipEntry("mimetype")); | ||
| zip.write(mimetypeValue.getBytes(StandardCharsets.UTF_8)); | ||
| zip.closeEntry(); | ||
|
|
||
| if (sectionEntryName != null) { | ||
| zip.putNextEntry(new ZipEntry(sectionEntryName)); | ||
| zip.write("<xml>placeholder</xml>".getBytes(StandardCharsets.UTF_8)); | ||
| zip.closeEntry(); | ||
| } | ||
| } | ||
| return out.toByteArray(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.