Rare beauty and charm decays with the CMS experiment