Cancel and Confirm typically just happen to have same size buttons. There’s really no UX principle supporting same size primary and secondary buttons
In fact, a larger confirm button is preferred to emphasize the primary action. This is why a lot of cancel and undo actions on the web are styled as a link (e.g. UX.SE’s post answer form) to give the primary action more weight.
You can read more about visual weight from the article referenced by @DasBesto https://uxmag.com/articles/visual-balance-and-weight-allocation-for-usability
Then why are cancel and confirm buttons usually the same size?
- The word “Cancel” and “Confirm” have very similar length.
Buttons length by default are determined by the length of its content. Since Cancel and Confirm are very similar in length, you get similar size buttons.
- Buttons on touch screen needs to be large.
On a mobile device, you need large tappable buttons. With a modal, to facilitate tapping, you make the buttons span the entire width. Cancel and Confirm are made the same size as a compromise to ensure both buttons are easy to tap on. This is okay because color also provides strong visual weight. As long as your Confirm button color make it stand out from your Cancel button, having the two button be the same size isn’t that big a deal.