Modding lens cap on camera?
I do agree with needing a layer of something on the inside of the lens cap.
But it needs to be rather firm, not squizy or easily distorted. Most foams would just not do the job.

Sometimes you can get hold of professional packaging foams that are smooth and rather sturdy on the surface and allow compression from the top (when the sheet lays flat on the table.) Those would do the job if you can get the edge to sit wider than the outside of the filter.

In my view easier is using sticky tape on the inside rim of the filter. You can add that one layer at a time and cut to the right dimensions, so not sticking out of the cap, when you have enough layers.
Many kinds of sticky tape will do, I would go for the paper kind that people use when painting the house, which is crepe like in structure.
It gives a better hold than most of the plastic tapes and it is kinder, as in not scratching the outside of the filter.

Whatever tape you use, add one layer at a time and test between adding.
Make sure you take away any glue that may be pushed out from under or between the layers of tape.

If you find you need several layers and having added them one at a time made the thing untidy, take all out and apply the number of layers you ended up using. Specially overlap or gaps where the layer meets going round it likely to mesh up tidy fit.
You might find it easier to stick a layer or a few layers of tape on the outer ring of the filter. 

The easier option is to go to a camera shop and get a new lens cap from them, which has worked several times for me. For use of filters and for lost lenscaps.