I have used silicone sealant but not that expanding foam
Reason being it can hold moisture in the air pockets in the foam and rust/rott the metal making the leak worse.
I think parts stores carry the black sealant for this.
Might be pricey though.
And either way its a PITA (a contortionists nightmare)to get to from under the hood.
I try and seal it from inside the cabin then try and get to the other side, under the hood.
You can use a paint stir stick(or putty knife) to spead the goop into the crack(s).
Good Luck !