blazingly-aasa 0.1.5

Apple Associated Domains (apple-app-site-association) semantics: parse, validate, match, explain, and diff
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! Semantic comparison of two association files.
//!
//! A textual diff of two `apple-app-site-association` files is mostly noise: key order changes,
//! `caseSensitive` moves from a component up to `defaults`, whitespace shifts. What matters is
//! whether the *decisions* changed. This module compares the effective, order-preserving rule list
//! per app, so a pure refactor reports no changes.
//!
//! # What "equivalent" means here, exactly
//!
//! This compares **normalised effective policy**, not behaviour. The distinction matters:
//!
//! * **Sound.** Two documents reported as equivalent decide every URL identically. A difference
//!   that changes a decision is never missed.
//! * **Not complete.** A reported difference does not prove a URL exists that the two documents
//!   answer differently. Reordering two rules whose patterns cannot both match — `/foo/*` and
//!   `/bar/*` — is reported as a move, because rule order decides which of two *overlapping* rules
//!   wins and this comparison does not attempt to prove they never overlap. Changing a
//!   substitution variable no pattern uses is reported too.
//!
//! In other words: no false "equivalent", but "different" means *potentially* different. That is
//! the safe direction for a check that gates a deploy, and it is worth stating rather than
//! implying more.
//!
//! Proving the stronger property — producing a witness URL that the two documents decide
//! differently, or proving none exists — is a real feature and a different algorithm. See
//! `docs/roadmap.md`.

use crate::compile::{CompiledAasa, EffectiveRule, Service, SERVICES};
use serde::Serialize;
use std::collections::BTreeSet;
use std::fmt;

/// One semantic difference between two documents.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "change", rename_all = "snake_case")]
#[non_exhaustive]
pub enum SemanticChange {
    /// A service section appeared.
    ServiceAdded {
        /// The service.
        service: Service,
    },
    /// A service section disappeared.
    ServiceRemoved {
        /// The service.
        service: Service,
    },
    /// An app gained access to a service.
    AppAdded {
        /// The service.
        service: Service,
        /// The application identifier.
        app_id: String,
    },
    /// An app lost access to a service.
    AppRemoved {
        /// The service.
        service: Service,
        /// The application identifier.
        app_id: String,
    },
    /// A rule was added for an app.
    RuleAdded {
        /// The application identifier.
        app_id: String,
        /// Position in the right-hand rule list.
        index: usize,
        /// The rule.
        rule: EffectiveRule,
    },
    /// A rule was removed for an app.
    RuleRemoved {
        /// The application identifier.
        app_id: String,
        /// Position in the left-hand rule list.
        index: usize,
        /// The rule.
        rule: EffectiveRule,
    },
    /// A rule kept its patterns but changed its settings or its `exclude` flag.
    RuleChanged {
        /// The application identifier.
        app_id: String,
        /// Position in the left-hand rule list.
        index: usize,
        /// The rule before.
        left: EffectiveRule,
        /// The rule after.
        right: EffectiveRule,
    },
    /// A rule kept its meaning but moved, which changes which rule wins first.
    RuleMoved {
        /// The application identifier.
        app_id: String,
        /// Position in the left-hand rule list.
        from: usize,
        /// Position in the right-hand rule list.
        to: usize,
        /// The rule.
        rule: EffectiveRule,
    },
    /// A substitution variable appeared.
    SubstitutionAdded {
        /// The variable name.
        name: String,
        /// Its values.
        values: Vec<String>,
    },
    /// A substitution variable disappeared.
    SubstitutionRemoved {
        /// The variable name.
        name: String,
        /// Its values.
        values: Vec<String>,
    },
    /// A substitution variable's values changed.
    SubstitutionChanged {
        /// The variable name.
        name: String,
        /// Values before.
        left: Vec<String>,
        /// Values after.
        right: Vec<String>,
    },
}

impl fmt::Display for SemanticChange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::ServiceAdded { service } => write!(f, "SERVICE_ADDED   {service}"),
            Self::ServiceRemoved { service } => write!(f, "SERVICE_REMOVED {service}"),
            Self::AppAdded { service, app_id } => write!(f, "APP_ADDED       {service}: {app_id}"),
            Self::AppRemoved { service, app_id } => {
                write!(f, "APP_REMOVED     {service}: {app_id}")
            }
            Self::RuleAdded {
                app_id,
                index,
                rule,
            } => {
                write!(f, "RULE_ADDED      {app_id} #{index}\n  {rule}")
            }
            Self::RuleRemoved {
                app_id,
                index,
                rule,
            } => {
                write!(f, "RULE_REMOVED    {app_id} #{index}\n  {rule}")
            }
            Self::RuleChanged {
                app_id,
                index,
                left,
                right,
            } => write!(
                f,
                "RULE_CHANGED    {app_id} #{index}\n  before: {left}\n  after:  {right}"
            ),
            Self::RuleMoved {
                app_id,
                from,
                to,
                rule,
            } => write!(f, "RULE_MOVED      {app_id} #{from} -> #{to}\n  {rule}"),
            Self::SubstitutionAdded { name, values } => {
                write!(f, "SUBST_ADDED     ${name} = {values:?}")
            }
            Self::SubstitutionRemoved { name, values } => {
                write!(f, "SUBST_REMOVED   ${name} = {values:?}")
            }
            Self::SubstitutionChanged { name, left, right } => write!(
                f,
                "SUBST_CHANGED   ${name}\n  before: {left:?}\n  after:  {right:?}"
            ),
        }
    }
}

/// The result of comparing two documents.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct AasaDiff {
    changes: Vec<SemanticChange>,
}

impl AasaDiff {
    /// Whether the two documents have identical normalised effective policy.
    ///
    /// **True means they decide every URL identically.** False means they *might* differ: this
    /// compares policy, not behaviour, so a reordering of two rules that can never both match is
    /// reported as a difference even though no URL can tell them apart. See the module
    /// documentation.
    #[must_use]
    pub fn is_equivalent(&self) -> bool {
        self.changes.is_empty()
    }

    /// Every difference found.
    #[must_use]
    pub fn changes(&self) -> &[SemanticChange] {
        &self.changes
    }
}

impl fmt::Display for AasaDiff {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.changes.is_empty() {
            return f.write_str("semantically equivalent");
        }
        for (index, change) in self.changes.iter().enumerate() {
            if index > 0 {
                f.write_str("\n")?;
            }
            write!(f, "{change}")?;
        }
        Ok(())
    }
}

impl CompiledAasa {
    /// Compares this document with `other` by normalised effective policy.
    ///
    /// Sound but not complete: anything reported as equivalent decides every URL the same way,
    /// while a reported difference may have no URL that witnesses it. See the module
    /// documentation for what that rules in and out.
    #[must_use]
    pub fn semantic_diff(&self, other: &Self) -> AasaDiff {
        let mut changes = Vec::new();

        for service in SERVICES {
            let left_present = self.declares(service);
            let right_present = other.declares(service);
            match (left_present, right_present) {
                (false, true) => changes.push(SemanticChange::ServiceAdded { service }),
                (true, false) => changes.push(SemanticChange::ServiceRemoved { service }),
                _ => {}
            }

            let left: BTreeSet<&str> = self.apps_for_service(service).into_iter().collect();
            let right: BTreeSet<&str> = other.apps_for_service(service).into_iter().collect();
            for app_id in right.difference(&left) {
                changes.push(SemanticChange::AppAdded {
                    service,
                    app_id: (*app_id).to_owned(),
                });
            }
            for app_id in left.difference(&right) {
                changes.push(SemanticChange::AppRemoved {
                    service,
                    app_id: (*app_id).to_owned(),
                });
            }
        }

        let apps: BTreeSet<&str> = self
            .applink_apps()
            .into_iter()
            .chain(other.applink_apps())
            .collect();
        for app_id in apps {
            diff_rules(
                app_id,
                &self.effective_rules_for(app_id),
                &other.effective_rules_for(app_id),
                &mut changes,
            );
        }

        diff_substitutions(self, other, &mut changes);
        AasaDiff { changes }
    }

    /// Whether the two documents have identical normalised effective policy.
    ///
    /// True guarantees identical decisions for every URL. False does not guarantee a difference in
    /// decisions; see [`AasaDiff::is_equivalent`].
    #[must_use]
    pub fn semantic_equal(&self, other: &Self) -> bool {
        self.semantic_diff(other).is_equivalent()
    }

    /// Whether the two wire models are identical, key for key.
    #[must_use]
    pub fn structural_equal(&self, other: &Self) -> bool {
        self.document == other.document
    }

    fn declares(&self, service: Service) -> bool {
        match service {
            Service::AppLinks => self.has_applinks,
            Service::WebCredentials => self.document.webcredentials.is_some(),
            Service::AppClips => self.document.appclips.is_some(),
            Service::ActivityContinuation => self.document.activitycontinuation.is_some(),
        }
    }
}

fn diff_substitutions(
    left: &CompiledAasa,
    right: &CompiledAasa,
    changes: &mut Vec<SemanticChange>,
) {
    let names: BTreeSet<&String> = left
        .substitution_variables()
        .keys()
        .chain(right.substitution_variables().keys())
        .collect();
    for name in names {
        match (
            left.substitution_variables().get(name),
            right.substitution_variables().get(name),
        ) {
            (None, Some(values)) => changes.push(SemanticChange::SubstitutionAdded {
                name: name.clone(),
                values: values.clone(),
            }),
            (Some(values), None) => changes.push(SemanticChange::SubstitutionRemoved {
                name: name.clone(),
                values: values.clone(),
            }),
            (Some(before), Some(after)) if before != after => {
                changes.push(SemanticChange::SubstitutionChanged {
                    name: name.clone(),
                    left: before.clone(),
                    right: after.clone(),
                });
            }
            _ => {}
        }
    }
}

#[derive(Debug, Clone, Copy)]
enum Edit {
    Keep,
    Remove(usize),
    Add(usize),
}

fn diff_rules(
    app_id: &str,
    left: &[EffectiveRule],
    right: &[EffectiveRule],
    changes: &mut Vec<SemanticChange>,
) {
    if left == right {
        return;
    }
    let edits = edit_script(left, right);

    let mut removed: Vec<usize> = Vec::new();
    let mut added: Vec<usize> = Vec::new();
    for edit in edits {
        match edit {
            Edit::Keep => {}
            Edit::Remove(index) => removed.push(index),
            Edit::Add(index) => added.push(index),
        }
    }

    let mut added_taken = vec![false; added.len()];
    let mut removed_taken = vec![false; removed.len()];

    // A removal and an insertion of the very same rule is a reorder, not a rewrite.
    for (removed_slot, left_index) in removed.iter().enumerate() {
        for (added_slot, right_index) in added.iter().enumerate() {
            if added_taken[added_slot] {
                continue;
            }
            if left[*left_index] == right[*right_index] {
                removed_taken[removed_slot] = true;
                added_taken[added_slot] = true;
                changes.push(SemanticChange::RuleMoved {
                    app_id: app_id.to_owned(),
                    from: *left_index,
                    to: *right_index,
                    rule: left[*left_index].clone(),
                });
                break;
            }
        }
    }

    // Same patterns, different settings: report it as one change rather than a delete plus an add.
    for (removed_slot, left_index) in removed.iter().enumerate() {
        if removed_taken[removed_slot] {
            continue;
        }
        for (added_slot, right_index) in added.iter().enumerate() {
            if added_taken[added_slot] {
                continue;
            }
            if same_shape(&left[*left_index], &right[*right_index]) {
                removed_taken[removed_slot] = true;
                added_taken[added_slot] = true;
                changes.push(SemanticChange::RuleChanged {
                    app_id: app_id.to_owned(),
                    index: *left_index,
                    left: left[*left_index].clone(),
                    right: right[*right_index].clone(),
                });
                break;
            }
        }
    }

    for (slot, left_index) in removed.iter().enumerate() {
        if !removed_taken[slot] {
            changes.push(SemanticChange::RuleRemoved {
                app_id: app_id.to_owned(),
                index: *left_index,
                rule: left[*left_index].clone(),
            });
        }
    }
    for (slot, right_index) in added.iter().enumerate() {
        if !added_taken[slot] {
            changes.push(SemanticChange::RuleAdded {
                app_id: app_id.to_owned(),
                index: *right_index,
                rule: right[*right_index].clone(),
            });
        }
    }
}

fn same_shape(left: &EffectiveRule, right: &EffectiveRule) -> bool {
    left.path == right.path && left.query == right.query && left.fragment == right.fragment
}

/// A longest-common-subsequence edit script, so an insertion does not shift every later rule.
fn edit_script(left: &[EffectiveRule], right: &[EffectiveRule]) -> Vec<Edit> {
    const MAX_CELLS: usize = 1 << 20;
    if left.len().saturating_mul(right.len()) > MAX_CELLS {
        // Degenerate input: fall back to a positional comparison rather than allocating a huge
        // table. Real association files never reach this.
        let mut edits = Vec::new();
        for index in 0..left.len().max(right.len()) {
            match (left.get(index), right.get(index)) {
                (Some(a), Some(b)) if a == b => edits.push(Edit::Keep),
                (Some(_), Some(_)) => {
                    edits.push(Edit::Remove(index));
                    edits.push(Edit::Add(index));
                }
                (Some(_), None) => edits.push(Edit::Remove(index)),
                (None, Some(_)) => edits.push(Edit::Add(index)),
                (None, None) => {}
            }
        }
        return edits;
    }

    let rows = left.len() + 1;
    let columns = right.len() + 1;
    let mut table = vec![0usize; rows * columns];
    for row in (0..left.len()).rev() {
        for column in (0..right.len()).rev() {
            table[row * columns + column] = if left[row] == right[column] {
                table[(row + 1) * columns + column + 1] + 1
            } else {
                table[(row + 1) * columns + column].max(table[row * columns + column + 1])
            };
        }
    }

    let mut edits = Vec::new();
    let (mut row, mut column) = (0usize, 0usize);
    while row < left.len() && column < right.len() {
        if left[row] == right[column] {
            edits.push(Edit::Keep);
            row += 1;
            column += 1;
        } else if table[(row + 1) * columns + column] >= table[row * columns + column + 1] {
            edits.push(Edit::Remove(row));
            row += 1;
        } else {
            edits.push(Edit::Add(column));
            column += 1;
        }
    }
    while row < left.len() {
        edits.push(Edit::Remove(row));
        row += 1;
    }
    while column < right.len() {
        edits.push(Edit::Add(column));
        column += 1;
    }
    edits
}