• Post Reply Bookmark Topic Watch Topic
  • New Topic

How put security check on constructor if it calls super?  RSS feed

 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to put a security check into a constructor, but it makes a call to super(), which has to be the first thing in a constructor. So if I put the security check after that, it's not quite a correct implementation, because it means they were able to fully invoke the super's constructor, possibly calling other methods, etc. Is there a solution?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That sounds like a job for a "Factory" design pattern - all constructors in your derived class are private and can only be called from a static Factory method which does the appropriate security checks first.
Bill
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But what if it's a class that someone else wrote and I want to use byte-code engineering to add the functionality? Is there anyway to add security calls/checks before the call to "super()"?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it possible to use BCEL to modify the superclass definition as well? If you need to protect access to the superclass constructor from within the subcless constructor, don't you also need to protect against somone invoking that superclass constructor directly? Protecting both classes separately seems like the preferred solution if you're able to manipulate both files. If not, hmmm... the only other thing I can think of is to use a security check in a class initializer (static block), which can certainly run before the superclass constructor. If you block the unauthorized user from being able to load your class at all, that should prevent instantiation too. But if you need a finer grained approach, I don't have any better ideas, sorry.
 
Robert Paris
Ranch Hand
Posts: 585
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, I was thinking of doing it like that, but didn't know if that would "essentially" do the same thing. And yep! I am using BCEL.

Is this what you meant? Question: will it be called only once? If code from one jar file tried to use this class and doesn't have perm. does this mean another jar (in the same JVM) that does have permission, also be precluded from using it because the static was called only once? Or does it mean the perm. check will happen only once and the next person gets no security check?
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is this what you meant?
Yes.
Question: will it be called only once?
Normally yes, it just gets called once when the class is first loaded. However if an exception occurs during the class loading, I think it gets converted to an ExceptionInInitializerError indicating the class wasn't loaded successfully. You can catch this* and then try again later. I belive it's also possible to make a custom classloader which unloads & reloads a class, but I don't remeber how this works offhand.
* Yeah, it's a Error, but what the hell, try catching it anyway. You only live once.
If code from one jar file tried to use this class and doesn't have perm. does this mean another jar (in the same JVM) that does have permission, also be precluded from using it because the static was called only once?
I think the next person can try again as suggested above.
Or does it mean the perm. check will happen only once and the next person gets no security check?
Mmm... I guess there's only one successful check. So if you're trying to serve different users simultaneously with the same JVM this solution probably won't work, since once user A loads the class successfully, user B has access to the class too, with no security check. Well there's a check, but it's later than you need apparently.
Are you sure you can't add security checks to the superclass too?
But what if it's a class that someone else wrote and I want to use byte-code engineering to add the functionality? Is there anyway to add security calls/checks before the call to "super()"?
Could you write your own custom wrapper class with the same API, such that the user never sees this other class, accessig it only through your wrapper?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!