Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement validation for autoconfig files #123

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions .github/workflows/rust.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
name: Rust

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
paths:
- tools/validation/**
schedule:
- cron: '30 2 * * *'

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest
name: "Build & Test: ./tools/validation"
defaults:
run:
working-directory: ./tools/validation

steps:
- name: Checkout source
uses: actions/checkout@v4

- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy

- name: Check Formatting
run: cargo fmt --all -- --check

- name: Cargo Cache
uses: actions/cache@v3
with:
path: |
~/.cargo/registry/index/
~/.cargo/registry/cache/
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Build project
run: cargo build

- name: Test project
run: cargo test --all

- name: Run clippy
uses: giraffate/clippy-action@v1
with:
reporter: 'github-pr-check'
clippy_flags: --no-deps
filter_mode: nofilter
github_token: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target
.idea
166 changes: 166 additions & 0 deletions tools/validation/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions tools/validation/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[workspace]
resolver = "2"
members = ["autoconfig_validate"]
6 changes: 6 additions & 0 deletions tools/validation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Autoconfig validation

Tools to use for reading and validating autoconfig configuration files.

For more information on autoconfig files and the autoconfig format,
see https://github.com/thunderbird/autoconfig
12 changes: 12 additions & 0 deletions tools/validation/autoconfig_validate/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "autoconfig_validate"
version = "0.1.0"
edition = "2021"

[dependencies]
anyhow = "1.0.86"
quick-xml = { version = "0.34.0", features = ["serde", "serialize"] }
regex = "1.10.5"
serde = { version = "1.0.203", features = ["derive"] }
serde_path_to_error = "0.1.16"
thiserror = "1.0.61"
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8"?>
<clientConfig version="1.1">
<emailProvider id="office365.com">
<!-- Office365 customer domains -->
<domain>office365.com</domain>
<domain>onmicrosoft.com</domain>
<!-- MX e.g. example.mail.protection.outlook.com -->
<domain>mail.protection.outlook.com</domain>
<displayName>Microsoft 365</displayName>
<displayShortName>Microsoft 365</displayShortName>
<incomingServer type="imap">
<hostname>outlook.office365.com</hostname>
<port>993</port>
<socketType>SSL</socketType>
<authentication>OAuth2</authentication>
<username>%EMAILADDRESS%</username>
</incomingServer>
<incomingServer type="pop3">
<hostname>outlook.office365.com</hostname>
<port>995</port>
<socketType>SSL</socketType>
<authentication>OAuth2</authentication>
<username>%EMAILADDRESS%</username>
<pop3>
<leaveMessagesOnServer>true</leaveMessagesOnServer>
</pop3>
</incomingServer>
<incomingServer type="exchange">
<hostname>outlook.office365.com</hostname>
<port>443</port>
<username>%EMAILADDRESS%</username>
<socketType>SSL</socketType>
<authentication>OAuth2</authentication>
<owaURL>https://outlook.office365.com/owa/</owaURL>
<ewsURL>https://outlook.office365.com/ews/exchange.asmx</ewsURL>
<useGlobalPreferredServer>true</useGlobalPreferredServer>
</incomingServer>
<outgoingServer type="smtp">
<hostname>smtp.office365.com</hostname>
<port>587</port>
<socketType>STARTTLS</socketType>
<authentication>OAuth2</authentication>
<username>%EMAILADDRESS%</username>
</outgoingServer>
</emailProvider>

<oAuth2>
<issuer>login.microsoftonline.com</issuer>
<scope>https://outlook.office365.com/IMAP.AccessAsUser.All https://outlook.office365.com/POP.AccessAsUser.All
https://outlook.office365.com/SMTP.Send offline_access
</scope>
<!-- https://docs.microsoft.com/en-us/azure/active-directory/develop/active-directory-v2-protocols#endpoints -->
<authURL>https://login.microsoftonline.com/common/oauth2/v2.0/authorize</authURL>
<tokenURL>https://login.microsoftonline.com/common/oauth2/v2.0/token</tokenURL>
</oAuth2>

</clientConfig>
45 changes: 45 additions & 0 deletions tools/validation/autoconfig_validate/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::{env, io};
use thiserror::Error;

use crate::types::IncomingServerType;

/// An error that arose while deserializing or validating the autoconfig file.
#[derive(Debug, Error)]
pub enum ValidationError {
#[error("failed to deserialize structure from XML: {0}")]
Deserialize(#[from] serde_path_to_error::Error<quick_xml::DeError>),

#[error("unsupported clientConfig version: {0}")]
UnsupportedVersion(String),

#[error(
"found protocol-specific config for {found:?} on incoming server of type {server_type:?}"
)]
InvalidIncomingConfig {
server_type: IncomingServerType,
found: IncomingServerType,
},

#[error("invalid username template: {0}")]
InvalidUsernameTemplate(String),

#[error("encountered an unexpected error: {0}")]
Unexpected(#[source] anyhow::Error),
}

/// An error that arose in a test.
#[derive(Debug, Error)]
pub(crate) enum TestError {
#[error("failed to read variable from environment: {0}")]
EnvRead(#[from] env::VarError),

#[error("failed to read file: {0}")]
Io(#[from] io::Error),

#[error("validation error: {0}")]
Validation(#[from] ValidationError),
}
Loading