Limiting rotations to box edge

I have a top down 2D game with 3D elements. I have a laser pointer offset from the ground, with the laser aiming at the ground. My question is how to limit the rotation of the laser gun to lie within a box? See attached image

I tried limiting the X and Y axis with rotation limits, but the further from the gun the laser point is, the larger the rotation range needs to be.

Any help wold be appreciated.

Thanks is advance

I assume the laser has a fixed length? The easy way would be to put colliders on the edges of the rectangle. Then you could test rotate a vector to the new, potential rotation and do a Raycast using the oval position and that vector. If the Raycast fails to hit the colliders, then go ahead and rotate the object to the new rotation.

It can be done with math. It is a fussy problem to work for an arbitrary placement of the oval within the rectangle, but it is just some trig calculations for a few different tests.