denoland / deno

A modern runtime for JavaScript and TypeScript.
https://deno.com
MIT License
98.16k stars 5.4k forks source link

`InvalidContentType` when trying to write to connection over `connectTls` on port 587 #20951

Open Unkn0wn0x opened 1 year ago

Unkn0wn0x commented 1 year ago

If I try to write into a open connection on port 587 via connectTls, I'm getting the error:

InvalidData: received corrupt message of type InvalidContentType
    at async write (ext:deno_net/01_net.js:32:10)
    at async sendEmail (<anonymous>:11:5)
    at async <anonymous>:17:1 {
  name: "InvalidData"
}

This is the code example I used / how the issue can be reproduced:

async function sendEmail() {

    const encoder = new TextEncoder();

    try {

        const conn = await Deno.connectTls({
            hostname: "smtp.mydomain.com",
            port: 587
        });

        await conn.write(encoder.encode("EHLO mydomain.com\r\n"));
        conn.close();

    } catch (error) {
        console.error(error);
    }
}

await sendEmail();

If I try to start the connection via connect and then switch the connection over with startTls, I'm getting the same error:

InvalidData: received corrupt message of type InvalidContentType
    at async write (ext:deno_net/01_net.js:32:10)
    at async sendEmail (<anonymous>:13:5)
    at async <anonymous>:19:1 {
  name: "InvalidData"
}

Code example:

async function sendEmail() {

    const encoder = new TextEncoder();

    let conn;
    try {

        conn = await Deno.connect({
            hostname: "smtp.mydomain.com",
            port: 587
        });
        const tlsConn = await Deno.startTls(conn, { hostname: "smtp.mydomain.com" });

        await tlsConn.write(encoder.encode("EHLO mydomain.com\r\n"));

        tlsConn.close();

    } catch (error) {
        console.error(error);
    }
}

await sendEmail();

This affects sending emails over a secure connection via Deno. On the other hand, when port 465 on the SMTP server is available, it works without any problems.

If I run openssl s_client -debug -starttls smtp -crlf smtp.mydomain.com:587 everything seems to work fine.

Any ideas how to fix this bug? It affects the Deno mailing packages like:

Could also be related to:

Especially:

Thanks in advance.

hongkongkiwi commented 2 days ago

Just hit this, port 587 on nodemailer using deno fails, but port 465 works fine.