-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
entry_checker: add sender validation lambda
- Loading branch information
Showing
20 changed files
with
384 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
name: Run tests | ||
name: Run tests (Go) | ||
|
||
on: | ||
push: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
--- | ||
name: Run tests (Python) | ||
on: | ||
push: | ||
workflow_dispatch: | ||
pull_request: | ||
jobs: | ||
pytest: | ||
strategy: | ||
fail-fast: false | ||
matrix: | ||
cfg: | ||
- component: "lambda/entry_checker" | ||
runs-on: ubuntu-latest | ||
env: | ||
python_version: "1.12" | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Install poetry | ||
run: pipx install poetry | ||
- uses: actions/setup-python@v5 | ||
with: | ||
python-version: ${{ env.python_version }} | ||
cache: "poetry" | ||
cache-dependency-path: | | ||
${{ matrix.cfg.component }}/pyproject.toml | ||
${{ matrix.cfg.component }}/poetry.lock | ||
- name: Run pytest | ||
run: | | ||
cd ${{ matrix.cfg.component }} | ||
poetry run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ This repository consists of two parts: | |
|
||
Terraform module is provided in `terraform` directory. | ||
It can be used to create necessary components on AWS's end. | ||
It's required to set up [SES email receiving](https://docs.aws.amazon.com/ses/latest/dg/receiving-email-setting-up.html) first and | ||
It's required to set up [SES email receiving](https://docs.aws.amazon.com/ses/latest/dg/receiving-email-setting-up.html) first, | ||
configuring this part is outside the scope of this project. | ||
|
||
The minimum to get started is: | ||
|
@@ -55,6 +55,14 @@ The minimum to get started is: | |
recipients = ["[email protected]", "[email protected]"] | ||
``` | ||
|
||
It's also recommended (but optional) to set a regex describing allowed senders. | ||
Emails sent from different addresses will simply be ignored. | ||
It can be set as below: | ||
|
||
``` | ||
senders = ".*@example.com" | ||
``` | ||
|
||
2. run `terraform init` | ||
3. run `terraform apply` | ||
4. plug in the values obtained via `terraform output` as env variables in the following section | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
venv | ||
.venv | ||
_pycache_ | ||
|
||
payload |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import logging | ||
import os | ||
from entry_checker.validate import validate_sender | ||
|
||
logger = logging.getLogger(__name__) | ||
logger.setLevel(logging.DEBUG) | ||
|
||
|
||
def main(event, context): | ||
allowed_senders_regex = os.environ["ALLOWED_SENDERS_REGEX"] | ||
|
||
ses_notification = event["Records"].pop()["ses"] | ||
receipt = ses_notification["receipt"] | ||
|
||
logger.debug(receipt) | ||
|
||
stop_email = False | ||
|
||
if receipt["spfVerdict"]["status"] == "FAIL": | ||
logger.debug("SPF FAIL detected, cutting off") | ||
stop_email = True | ||
|
||
elif receipt["dkimVerdict"]["status"] == "FAIL": | ||
logger.debug("DKIM FAIL detected, cutting off") | ||
stop_email = True | ||
|
||
elif receipt["spamVerdict"]["status"] == "FAIL": | ||
logger.debug("SPAM FAIL detected, cutting off") | ||
stop_email = True | ||
|
||
elif receipt["virusVerdict"]["status"] == "FAIL": | ||
logger.debug("VIRUS FAIL detected, cutting off") | ||
stop_email = True | ||
|
||
else: | ||
# Such validation is not perfect, but paired with the above conditions | ||
# that rely on Amazon checks, it's definitely better than nothing. | ||
sender = ses_notification["mail"]["commonHeaders"]["from"].pop() | ||
|
||
if not validate_sender(allowed_senders_regex, sender): | ||
logger.debug("sender validation FAIL detected, cutting off") | ||
stop_email = True | ||
|
||
if stop_email: | ||
return {"disposition": "STOP_RULE_SET"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import re | ||
|
||
|
||
def validate_sender(allowed_senders_regex: str, sender: str): | ||
# Get rid of the "quotation marks" | ||
sender = sender.replace("<", "").replace(">", "") | ||
|
||
if re.search(allowed_senders_regex, sender): | ||
return True | ||
|
||
return False |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
[tool.poetry] | ||
name = "entry-checker" | ||
version = "0.1.0" | ||
description = "" | ||
authors = ["dezeroku <[email protected]>"] | ||
readme = "README.md" | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.12" | ||
|
||
[tool.poetry.group.dev.dependencies] | ||
pytest = "^8.3.4" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
from entry_checker.validate import validate_sender | ||
|
||
import pytest | ||
|
||
allowed_senders_regex = ".*@example.com" | ||
|
||
|
||
@pytest.mark.parametrize("sender", ["[email protected]", "<[email protected]>"]) | ||
def test_validate_sender_success(sender): | ||
assert validate_sender(allowed_senders_regex, sender) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"sender", ["[email protected]", "<[email protected]>"] | ||
) | ||
def test_validate_sender_fail(sender): | ||
assert not validate_sender(allowed_senders_regex, sender) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
.terraform* | ||
terraform* | ||
!.terraform.lock.hcl | ||
!terraform.tfvars.development |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.