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

[stable2.2] fix(autoconfig): Refactor DNS query for testing #9968

Merged
merged 1 commit into from
Aug 7, 2024
Merged
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
45 changes: 45 additions & 0 deletions .github/workflows/update-public-suffix-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
# SPDX-License-Identifier: MIT
name: Update public suffix list

on:
workflow_dispatch:
schedule:
- cron: "5 2 * * *"

jobs:
update-public-suffix-list:
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
branches: ['main', 'stable3.7']

name: update-public-suffix-list-${{ matrix.branches }}

steps:
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332
with:
ref: ${{ matrix.branches }}
submodules: true

- name: Download public suffix list
run: curl --output resources/resources/public_suffix_list.dat https://publicsuffix.org/list/public_suffix_list.dat

- name: Create Pull Request
uses: peter-evans/create-pull-request@c5a7806660adbe173f04e3e038b0ccdcd758773c
with:
token: ${{ secrets.COMMAND_BOT_PAT }}
commit-message: 'fix(dns): Update public suffix list'
committer: GitHub <noreply@github.com>
author: nextcloud-command <nextcloud-command@users.noreply.github.com>
signoff: true
branch: 'fix/dns/update-public-suffix-list-${{ matrix.branches }}'
title: '[${{ matrix.branches }}] fix(dns): Update public suffix list'
body: |
Auto-generated update of https://publicsuffix.org/
labels: |
dependencies
3. to review
reviewers: ChristophWurst
373 changes: 373 additions & 0 deletions LICENSES/MPL-2.0.txt

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"ezyang/htmlpurifier": "4.16.0",
"gravatarphp/gravatar": "^2.0",
"html2text/html2text": "^4.3",
"jeremykendall/php-domain-parser": "^6.0",
"nextcloud/horde-managesieve": "^1.0",
"nextcloud/horde-smtp": "^1.0",
"psr/log": "^1",
Expand Down
95 changes: 94 additions & 1 deletion composer.lock

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

32 changes: 32 additions & 0 deletions lib/Dns/Resolver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Mail\Dns;

use Pdp\Domain;
use Pdp\Rules;
use function dns_get_record;

class Resolver {
/**
* @return array|false
*/
public function resolve(string $hostname, int $type) {
return dns_get_record($hostname, $type);
}

public function isSuffix(string $hostname): bool {
$publicSuffixList = Rules::fromPath(__DIR__ . '/../../resources/public_suffix_list.dat');
$domain = Domain::fromIDNA2008($hostname);

$result = $publicSuffixList->resolve($domain);

return $result->secondLevelDomain()->toString() === '';
}
}
15 changes: 12 additions & 3 deletions lib/Service/AutoConfig/IspDb.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@

use Exception;
use Horde_Mail_Rfc822_Address;
use OCA\Mail\Dns\Resolver;
use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use Psr\Log\LoggerInterface;
use SimpleXMLElement;
use function explode;
use function str_replace;
use function strtolower;

Expand All @@ -40,6 +42,7 @@ class IspDb {

/** @var LoggerInterface */
private $logger;
private Resolver $dnsResolver;

/** @returns string[] */
public function getUrls(): array {
Expand All @@ -52,8 +55,11 @@ public function getUrls(): array {
];
}

public function __construct(IClientService $clientService, LoggerInterface $logger) {
public function __construct(IClientService $clientService,
Resolver $dnsResolver,
LoggerInterface $logger) {
$this->client = $clientService->newClient();
$this->dnsResolver = $dnsResolver;
$this->logger = $logger;
}

Expand Down Expand Up @@ -160,11 +166,14 @@ public function query(string $domain, Horde_Mail_Rfc822_Address $email, bool $tr
}
}

if ($tryMx && ($dns = dns_get_record($domain, DNS_MX))) {
if ($tryMx && ($dns = $this->dnsResolver->resolve($domain, DNS_MX))) {
$domain = $dns[0]['target'];
if (!($config = $this->query($domain, $email, false))) {
[, $domain] = explode('.', $domain, 2);
$config = $this->query($domain, $email, false);
// Only try second-level domains and deeper
if (!$this->dnsResolver->isSuffix($domain)) {
$config = $this->query($domain, $email, false);
}
}
}
return $config;
Expand Down
Loading
Loading