Skip to content
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

Defensive guards against invalid code points in preferences. #6094

Merged
merged 1 commit into from
Jul 4, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -674,22 +674,30 @@ public static synchronized IntrospectedInfo getKnownInfo() {
null | String[] enumTags: .enumTags=whenempty,always,never
*/
Pattern p = Pattern.compile("(.+)\\.(supportsText|attrs\\.(.+)|subs\\.(.+)|enumTags)");
@Override
public IntrospectedInfo load(Preferences node) {
IntrospectedInfo ii = new IntrospectedInfo();
try {
for (String k : node.keys()) {
String v = node.get(k, null);
String v;
try {
v = node.get(k, null);
} catch (IllegalArgumentException ex) { // e.g invalid code point JDK-8075156
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}, msg: {2}",
new Object[] {k, node.absolutePath(), ex.getMessage()});
continue;
}
assert v != null : k;
String[] ss = k.split("\\.", 2);
if (ss.length != 2) {
LOG.log(Level.WARNING, "malformed key: {0}", k);
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
continue;
}
if (ss[0].equals("class")) {
Matcher m = p.matcher(ss[1]);
boolean match = m.matches();
if (!match) {
LOG.log(Level.WARNING, "malformed key: {0}", k);
LOG.log(Level.WARNING, "malformed key: {0}, pref path: {1}", new Object[] {k, node.absolutePath()});
continue;
}
String c = m.group(1);
Expand Down Expand Up @@ -742,6 +750,7 @@ private IntrospectedClass assureDefined(IntrospectedInfo ii, String clazz) {
}
return ic;
}
@Override
public void store(Preferences node, IntrospectedInfo info) {
try {
node.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.util.Exceptions;
import org.openide.util.Lookup;
import org.openide.util.NbPreferences;
import org.openide.util.Utilities;
Expand All @@ -75,9 +74,9 @@ public class MavenFileOwnerQueryImpl implements FileOwnerQueryImplementation {

private final PropertyChangeListener projectListener;
private final ProjectGroupChangeListener groupListener;
private final List<ChangeListener> listeners = new CopyOnWriteArrayList<ChangeListener>();
private final List<ChangeListener> listeners = new CopyOnWriteArrayList<>();

private static final AtomicReference<Preferences> prefs = new AtomicReference<Preferences>(NbPreferences.forModule(MavenFileOwnerQueryImpl.class).node(EXTERNAL_OWNERS));
private static final AtomicReference<Preferences> prefs = new AtomicReference<>(NbPreferences.forModule(MavenFileOwnerQueryImpl.class).node(EXTERNAL_OWNERS));

private static final Logger LOG = Logger.getLogger(MavenFileOwnerQueryImpl.class.getName());

Expand Down Expand Up @@ -150,7 +149,15 @@ public void registerCoordinates(String groupId, String artifactId, String versio
String ownerString = owner.toString();
try {
for (String k : prefs().keys()) {
if (ownerString.equals(prefs().get(k, null))) {
String value;
try {
value = prefs().get(k, null);
} catch (IllegalArgumentException ex) {
// e.g invalid code point JDK-8075156
LOG.log(Level.WARNING, "Invalid prefrences key at {0}, msg: {1}", new Object[] { prefs().absolutePath(), ex.getMessage() });
continue;
}
if (ownerString.equals(value)) {
prefs().remove(k);
break;
}
Expand Down
11 changes: 11 additions & 0 deletions nb/o.n.upgrader/src/org/netbeans/upgrade/CopyFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map.Entry;
Expand All @@ -39,6 +40,8 @@
import org.openide.filesystems.FileUtil;
import org.openide.util.EditableProperties;

import static java.nio.charset.StandardCharsets.UTF_8;

/** Does copy of files according to include/exclude patterns.
*
* @author Jiri Skrivanek
Expand Down Expand Up @@ -136,6 +139,14 @@ private static void copyFile(File sourceFile, File targetFile) throws IOExceptio
* @throws java.io.IOException if copying fails
*/
private void copyFile(File sourceFile) throws IOException {

// invalid code point check JDK-8075156
if (sourceFile.getName().endsWith(".properties")
&& new String(Files.readAllBytes(sourceFile.toPath()), UTF_8).indexOf('\u0000') != -1) {
LOGGER.log(Level.WARNING, "{0} contains invalid code points -> skipping", sourceFile); //NOI18N
return;
}

String relativePath = getRelativePath(sourceRoot, sourceFile);
currentProperties = null;
boolean includeFile = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,13 @@ void load() {
Preferences prefs = getPreferences();
try {
for( String key : prefs.keys() ) {
boolean view = prefs.getBoolean( key, false );
boolean view;
try {
view = prefs.getBoolean( key, false );
} catch (IllegalArgumentException ex) {
Logger.getLogger(TopComponentTracker.class.getName()).log(Level.INFO, "invalid preferences key", ex);
continue; // e.g invalid code point JDK-8075156
}
if( view )
viewIds.add( key );
else
Expand Down
Loading