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

Added validation and exception handling in readAttribute #405

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Shivam7-1
Copy link

Issue link: https://issues.oss-fuzz.com/issues/375660994
Added validation for attribute name and length in readAttribute method to prevent security exceptions. This change ensures that invalid or malformed attributes are handled properly, enhancing the robustness and security of the library.

Before you push a pull request, review this list:

  • Read the contribution guidelines for this project.
  • Run a successful build using the default Maven goal with mvn; that's mvn on the command line by itself.
  • Write unit tests that match behavioral changes, where the tests fail if the changes to the runtime are not applied. This may not always be possible but is a best-practice.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Each commit in the pull request should have a meaningful subject line and body. Note that commits might be squashed by a maintainer on merge.

@garydgregory
Copy link
Member

Hello @Shivam7-1

You'll need to add a unit test that fails without the changes to 'main'. It looks like the indentation is messed up.

Ty.

@Shivam7-1
Copy link
Author

Hii @garydgregory Thanks for response done this

Copy link
Member

@garydgregory garydgregory left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Shivam7-1

You did not follow the instructions in the PR template: Run mvn by itself and fix all errors. The PR title is confusing: The PR doesn't handle any exceptions, it throws exceptions for validation failures.

See my scattered comments as well.

Overall, I'm not sure if this PR is needed: If I apply the test side of the patch, I get ClassFormatExceptions already for the new tests.

Looking at the method, you could see where an NPE could happen but that's not what the tests cause.

@@ -18,6 +18,7 @@
*/
package org.apache.bcel.classfile;

import java.security.InvalidParameterException;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change has nothing to do with JCA or JCE, so java.security.InvalidParameterException is the wrong type. We define ClassFormatException for this very purpose. Examine the reset of the code base for examples.

@@ -113,10 +114,16 @@ public static Attribute readAttribute(final DataInput dataInput, final ConstantP
// Get class name from constant pool via 'name_index' indirection
final int nameIndex = dataInput.readUnsignedShort();
final String name = constantPool.getConstantUtf8(nameIndex).getBytes();

// Validate name
if (name == null || name.isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reuse StringUtils.isEmpty().

@@ -125,8 +132,8 @@ public static Attribute readAttribute(final DataInput dataInput, final ConstantP
}
}

// Call proper constructor, depending on 'tag'
switch (tag) {
// Call proper constructor, depending on 'tag'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The indentation is still messed up, no need to change it.

import org.apache.bcel.classfile.ConstantPool;
import org.junit.jupiter.api.Test;

public class readAttributeTest {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class name does not follow Java conventions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants