From 057daed8cdadd1cfdb5080e8418797542eb62978 Mon Sep 17 00:00:00 2001 From: Christian Stein Date: Tue, 20 Jan 2026 13:36:06 +0100 Subject: [PATCH] 8375433: jar should validate module names --- .../classes/sun/tools/jar/Validator.java | 24 ++++++++++++++++++- .../sun/tools/jar/resources/jar.properties | 4 ++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/jdk.jartool/share/classes/sun/tools/jar/Validator.java b/src/jdk.jartool/share/classes/sun/tools/jar/Validator.java index be694eeb1bc..9b9e6a8892b 100644 --- a/src/jdk.jartool/share/classes/sun/tools/jar/Validator.java +++ b/src/jdk.jartool/share/classes/sun/tools/jar/Validator.java @@ -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()); + } } } diff --git a/src/jdk.jartool/share/classes/sun/tools/jar/resources/jar.properties b/src/jdk.jartool/share/classes/sun/tools/jar/resources/jar.properties index c80377e20c1..62adaa3351e 100644 --- a/src/jdk.jartool/share/classes/sun/tools/jar/resources/jar.properties +++ b/src/jdk.jartool/share/classes/sun/tools/jar/resources/jar.properties @@ -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