The stability of a detonation wave passing through opposed supersonic flow in a duct of varying cross section and with friction is studied numerically by a transient quasi-one-dimensional model, taking into account the unsteady motion of the duct and using a single-step Arrhenius reaction scheme. The analysis indicates that in a converging duct, that is, a nozzle raising the rate of area change drives a detonation wave towards instability, and adding roughness behind the shock front increases the detonation velocity and stabilizes the detonation wave. The stable detonation-wave configuration can be analysed by replacing the classical Chapman-Jouguet criterion with a generalized sonic choking condition based on the competition between area change, friction and heat release.