Thanks all, it seems United will rebook me for free if I miss the connection due to the immigration delay.
I just have another question, because my original flight from SFO-YYZ will arrive at YYZ at 9pm, so the following flight from SFO might arrive at 11pm or even 1am. Can I ask united to rebook me for a later over night flight which arrive YYZ at the morning? Or maybe early morning flight on next day (within 24 hours)?