Giter VIP home page Giter VIP logo

burnex's Introduction

Burnex

Build Status Coverage Status Module Version Hex Docs Total Download License Last Updated

Compare an email address against 3900+ burner email domains (temporary email providers) based on this list from https://github.com/wesbos/burner-email-providers.

Installation

Add :burnex to your list of dependencies in mix.exs.

def deps do
  [
    {:burnex, "~> 3.1.0"}
  ]
end

Usage

Be aware that Burnex will not check if the email is RFC compliant, it will only check the domain (everything that comes after @).

iex> Burnex.is_burner?("[email protected]")
false
iex> Burnex.is_burner?("[email protected]")
true
iex> Burnex.is_burner? "invalid.format.yopmail.fr"
false
iex> Burnex.is_burner? "\"this is a valid address! crazy right ?\"@yopmail.fr"
true

iex> Burnex.providers |> MapSet.member?("yopmail.fr")
true

With an Ecto changeset

Following code ensures email has a valid format then check if it belongs to a burner provider:

def changeset(model, params) do
  model
  |> cast(params, @required_fields ++ @optional_fields)
  |> validate_required([:email])
  |> validate_email()
end

@email_regex ~r/\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i

defp validate_email(%{changes: %{email: email}} = changeset) do
  case Regex.match?(@email_regex, email) do
    true ->
      case Burnex.is_burner?(email) do
        true -> add_error(changeset, :email, "forbidden_provider")
        false -> changeset
      end
    false -> add_error(changeset, :email, "invalid_format")
  end
end
defp validate_email(changeset), do: changeset

MX record DNS resolution

As an extra precaution against newly-created burner domains, you can use Burnex to do MX record DNS resolution. This is done like this:

iex> Burnex.check_domain_mx_record("gmail.com")
:ok
iex> Burnex.check_domain_mx_record("gmail.dklfsd")
{:error, "Cannot find MX records"}

Here is an example function to check if an email is valid:

  # Use a regex capture to get the "domain" part of an email
  @email_regex ~r/^\S+@(\S+\.\S+)$/

  # hard-code some trusted domains to avoid checking their MX record every time
  @good_email_domains [
    "gmail.com",
    "fastmail.com"
  ]

  defp email_domain(email), do: Regex.run(@email_regex, String.downcase(email))

  defp is_not_burner?(email, domain) do
    with {:is_burner, false} <- {:is_burner, Burnex.is_burner?(email)},
         {:check_mx_record, :ok} <- {:check_mx_record, Burnex.check_domain_mx_record(domain)} do
      true
    else
      {:is_burner, true} ->
        {false, "forbidden email"}

      {:check_mx_record, {:error, error_message}} when is_binary(error_message) ->
        {false, error_message}

      {:check_mx_record, :error} ->
        {false, "forbidden provider"}
    end
  end

  @spec is_valid?(String.t()) :: true | {false, String.t()}
  def is_valid?(email) do
    case email_domain(email) do
      [_ | [domain]] when domain in @good_email_domains ->
        true

      [_ | [domain]] ->
        is_not_burner?(email, domain)

      _ ->
        {false, "Email in bad format"}
    end
  end

License

This software is licensed under MIT license. Copyright (c) 2018- Benjamin Piouffle.

burnex's People

Contributors

alexbenic avatar betree avatar dependabot-preview[bot] avatar dependabot[bot] avatar dolfinus avatar drselump14 avatar kianmeng avatar martinsvalin avatar niccolox avatar peaceful-james avatar robsonpeixoto avatar tomciopp avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

burnex's Issues

[feature] allow library users to specify providers

Burnex users have to use providers that come with the library, we can make this library more flexible by accepting optional providers argument for. This would allow library users to edit provider list or keep their own while reusing logic.

This would be a backwards compatible change. Suggested API change:

  iex> providers = ["badomain.org"]
  iex> Burnex.is_burner?("[email protected], providers: providers)

or more realistic:

  @good_domains ["com.ar", ...]
  @providers Burnex.providers() |> MapSet.delete(@good_domains)

  ...

  def is_burner?(email) do
     Burnex.is_burner?(email, providers: @providers)
  end

It's possible to do all of this in "userspace", but we pretty much have to re-implement lib.

[feature] MX record DNS lookup (RFC)

I think it would be nice to use dns lib (https://github.com/tungd/elixir-dns) or similar to do a :mx lookup and validate the mail server domains returned against the same list we check the actual email domain against.

Maybe it's kind of paranoid but it seems like it would make burnex even safer.

What do people think of this idea?

GMail Trick Detector

Consider these two emails:

It might not be very widespread knowledge, but these two emails are the same - they both get delivered to [email protected]. It would be nice to have burnex not only return a boolean, but instead also return whether the target final email is different than the one provided - so that someone providing [email protected] couldn't register several times, for example.

I'd be up for making the PR on this project, but I'd rather first know if it would be interesting for the library?

Is VERSION now required?

After the latest bump, we're getting the following error after from mix compile:

** (File.Error) could not read file "VERSION": no such file or directory

I think the culprit line can be found here:

version: String.trim(File.read!("VERSION")),

Our project does following semantic versioning but we do not use the VERSION file as you do with this project. Would it be possible to exclude this file from being required in order to use this dependency?

Dialyzer error

After the latest version bump to 2.0.0, for some reason including Burnex.is_burner?/1 in my application results in the following dialyzer error:

The guard test:

/=(_ :: false, false)

can never succeed.

I'm fairly confident that it's the is_burner/1 function as the second I remove the error goes away. Any thoughts on what's causing this?

Invalid Documentation

The README seems to refer to a Phoenix changeset. I don't think there is such thing - maybe we could change it to an Ecto changeset instead?

Phoenix changeset?

trying to work out how to pull this into the typical Phoenix user changeset pipeline

stub

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.