Professor Nicholas J. Wheeler, Professor of International Relations at the University of Birmingham, writes in the context of US response to 9/11:
A non-state terrorist group attacked the USA, but the counter-attack was directed against the territory of Afghanistan that had provided a safe haven for Al-Qaeda. The UN Security Council in Resolutions 1368, 1373 and 1378 recognised the right of self-defence to respond to attacks of this kind. Here, the Security Council recognised that large-scale terrorist attacks could constitute an “armed attack” that gives rise to a right of self-defence. The US claimed that it was acting in self-defence in taking action against the Taliban and Al-Qaeda, with military action being undertaken to defend the United States against potential future attacks of the kind experienced in New York and Washington DC. The threat of future attacks, in the light of past attacks, justifying the claim that the USA acted pursuant to a right of self-defence.Critics of the legality of the war in Afghanistan argue that the terrorist attacks on 11 September 2001 fail to meet the requirement of an “armed attack” because this is restricted to the use of force by states, and requires, in the words of the 1974 General Assembly’s Definition of Aggression , activity analogous to large-scale cross-border attacks.Set against this, the drafters of the UN Charter did not envisage non-state violence on the scale of the events of 11 September 2001, and it is necessary for interpretations of Article 51 to evolve to meet the challenges posed by groups like Al-Qaeda. The resolutions adopted by the Council in the immediate aftermath of the 11 September attacks provide strong support for a new custom that supports a right of self-defence against states that are believed to have harboured groups who have committed attacks – and crucially, are preparing further attacks - against the territory of the state claiming the right of self-defence. What is left unclear here is whether this right to self-defence extends to anticipatory action against terrorist groups – and their state sponsors - before they have launched an attack. For example, could the US, believing there to be an imminent threat from Al-Qaeda, have reasonably claimed a right of self-defence in attacking terrorist bases in Afghanistan on 10 September, 2001?
If the US could attack Afghanistan because it was harboring Al Qaeda, then certainly Israel could attack the head of Hamas being hosted by Iran. Arguably, Israel could attack Iran itself for hosting terrorists like Haniyeh and Islamic Jihad leader Ziyad al-Nakhalah.
(This is not even considering the likelihood that Iran was itself involved in the planning of 10/7, amd was aware of the attack beforehand.)