Oh yeah, and if you want to make sure the image always fits, also when the mask is wider in aspect ratio than your image, so you’ll want to do it in 2 dimension:
Transform -> scale:
maskSize = thisComp.layer("Shape Layer 1").content("Rectangle 1").content("Rectangle Path 1").size;
if (maskSize[0]/maskSize[1] < source.width/source.height) {
[100,100]*maskSize[1]/source.height;
} else {
[100,100]*maskSize[0]/source.width;
}
Effect -> Transform -> Position ->
mask_w = thisComp.layer(index-1).sourceRectAtTime().width;
mask_h = thisComp.layer(index-1).sourceRectAtTime().height;
wiggleRoomX = source.width - mask_w*(100/scale[0]);
wiggleRoomY = source.height - mask_h*(100/scale[1]);
x=clamp(value[0], (source.width-wiggleRoomX)/2, (source.width+wiggleRoomX)/2);
y=clamp(value[1], (source.height-wiggleRoomY)/2, (source.height+wiggleRoomY)/2);
[x,y];