TL;DR: Based on international law, if Ukraine takes aggressive military action in the territory of Russia, including, but not limited to, occupying some or all cities of Russia, it will be considered legitimate.
- Retaliations must be proportionate to the previous attacks (note that previous attacks of Russia included numerous war crimes).
- Retaliations must be aimed only at combatants and military objectives.
References:
Article 51
Nothing in the present Charter shall impair the inherent right of individual or collective self-defence if an armed attack occurs against a Member of the United Nations, until the Security Council has taken measures necessary to maintain international peace and security. Measures taken by Members in the exercise of this right of self-defence shall be immediately reported to the Security Council and shall not in any way affect the authority and responsibility of the Security Council under the present Charter to take at any time such action as it deems necessary in order to maintain or restore international peace and security.
United Nations Charter (full text): https://www.un.org/en/about-us/un-charter/full-text
In times of conflict, reprisals are considered legal under certain conditions: they must be carried out in response to a previous attack, they must be proportionate to that attack, and they must be aimed only at combatants and military objectives.
Humanitarian law hence forbids all reprisals against civilian persons and objects protected by the 1949 Geneva Conventions and their 1977 Additional Protocols. These include wounded, sick, or shipwrecked persons; medical or religious personnel, units, transports, or material; prisoners of war; civilian persons or civilian objects; cultural property or places of worship; objects indispensable to the survival of the civilian population; the natural environment; works and installations containing dangerous forces; and buildings and material used for the protection of the civilian population (GCI Art. 46; GCII Art. 47; GCIII Art. 13; GCIV Art. 33; API Arts. 20 and 51–56).
Médecins Sans Frontières (Doctors without borders) "The Practical Guide to Humanitarian Law: Reprisals": https://guide-humanitarian-law.org/content/article/3/reprisals/
A “reprisal” is a breach of international humanitarian law, which would otherwise be unlawful but in exceptional cases is considered lawful as an enforcement measure in response to a previous breach of international humanitarian law by the enemy, with the purpose of terminating the enemy’s violation.
Thus, reprisals are intended to put pressure on the enemy in order to obtain the enemy's compliance with international humanitarian law.
Reprisals are only allowed under very strict conditions and there is a trend towards outlawing reprisals in international humanitarian law.
Reprisals against wounded, sick or shipwrecked persons, medical or religious personnel, medical units, transports and material, prisoners of war, the civilian population and civilian persons, civilian objects, cultural property, objects indispensable to the survival of the civilian population, the natural environment, works and installations containing dangerous forces and the buildings and material used for the protection of the civilian population are always prohibited.
M. Sassòli, A. Bouvier, A. Quintin, J. Grignon, How does law protect in war?, ICRC, Geneva, 2014: https://casebook.icrc.org/a_to_z/glossary/reprisals