8375433: jar should validate module names

This commit is contained in:
Christian Stein 2026-01-20 13:36:06 +01:00
parent a67979c4e6
commit 057daed8cd
2 changed files with 27 additions and 1 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -47,6 +47,8 @@ import java.util.function.Function;
import java.util.function.IntSupplier;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
@ -272,6 +274,26 @@ final class Validator {
errorAndInvalid(formatMsg("error.validator.metainf.wrong.position", firstName));
}
}
// Check for a valid and consistent automatic module name
try (InputStream jis = zf.getInputStream(zf.getEntry(entryName))) {
Manifest manifest = new Manifest(jis);
Attributes global = manifest.getMainAttributes();
String name = global.getValue("Automatic-Module-Name");
if (name != null) {
try {
ModuleDescriptor.newAutomaticModule(name);
} catch (IllegalArgumentException e) {
errorAndInvalid(formatMsg("error.validator.manifest.invalid.automatic.module.name", name));
}
if (md != null) {
if (!name.equals(md.name())) {
errorAndInvalid(formatMsg("error.validator.manifest.inconsistent.automatic.module.name", name, md.name()));
}
}
}
} catch (IOException e) {
errorAndInvalid(e.getMessage());
}
}
}

View File

@ -138,6 +138,10 @@ error.validator.metainf.wrong.position=\
expected entry META-INF/ to be at position 0, but found: {0}
error.validator.manifest.wrong.position=\
expected entry META-INF/MANIFEST.MF to be at position 0 or 1, but found it at position: {0}
error.validator.manifest.invalid.automatic.module.name=\
invalid module name of Automatic-Module-Name entry in manifest: {0}
error.validator.manifest.inconsistent.automatic.module.name=\
expected module name is: {1} - but found Automatic-Module-Name entry in manifest: {0}
warn.validator.identical.entry=\
Warning: entry {0} contains a class that\n\
is identical to an entry already in the jar