Skip to content

Commit

Permalink
Ensure eval prompt shows up on lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
MattIPv4 committed Jan 3, 2024
1 parent 9cdbd59 commit 87e32ab
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 25 deletions.
10 changes: 6 additions & 4 deletions src/spf-explainer/templates/app.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ limitations under the License.
<EvalNotif ref="EvalNotif" :ip="ipEval"></EvalNotif>
</template>
<template #buttons>
<form v-if="!SPFSandbox.empty()" autocomplete="on" @submit.prevent="">
<form v-if="showEvalForm" autocomplete="on" @submit.prevent="">
<div class="input-container">
<label for="EvaluateInput" class="hidden">Evaluate</label>
<input
Expand Down Expand Up @@ -130,6 +130,7 @@ limitations under the License.
loading: false,
records: [],
ipEval: "",
showEvalForm: false,
errorMessage: "",
spfTop,
spfBottom,
Expand Down Expand Up @@ -163,6 +164,10 @@ limitations under the License.
return
}
SPFSandbox.wipe()
SPFSandbox.listen(() => this.$data.showEvalForm = !SPFSandbox.empty())
remakeController()
if (this.$data.lastDomain === domain) this.$data.records = []
this.$data.records = await getSPFRecords(domain)
window.history.pushState({}, "", `?domain=${domain}`)
Expand All @@ -174,9 +179,6 @@ limitations under the License.
this.$refs.NoSPFRecords.close()
}
SPFSandbox.wipe()
remakeController()
this.$data.firstSearch = false
this.$data.lastDomain = domain
},
Expand Down
13 changes: 2 additions & 11 deletions src/spf-explainer/templates/spf.vue
Original file line number Diff line number Diff line change
Expand Up @@ -254,16 +254,7 @@ limitations under the License.
this.$data.links[chunkItem[0][0]] = chunkItem[0][0]
// Detect IP address record dupes and fix them.
if (chunkItem[2]) {
const i = []
for (const ip of chunkItem[2]) {
if (!i.includes(ip)) {
i.push(ip)
ips.push(ip)
}
}
chunkItem[2] = i
}
if (chunkItem[2]) chunkItem[2] = [...new Set(chunkItem[2])]
// Adds this to parts (described above).
parts.push([chunkItem[0][0].trim(), value, chunkItem[1], chunkItem[2], chunk])
Expand All @@ -273,7 +264,7 @@ limitations under the License.
// ?all will be undefined anyway, hence it not here.
const action = chunks["~all"] ? true : chunks["-all"] ? false : undefined
SPFSandbox.import(chunks, ips, action)
SPFSandbox.import(chunks, action)
// Sets the parts to the component and emits that it is done loading.
this.$data.parts = parts
Expand Down
28 changes: 18 additions & 10 deletions src/spf-explainer/utils/spf_sandbox.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 DigitalOcean
Copyright 2024 DigitalOcean
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -25,7 +25,6 @@ class SPFRule {
const regexParts: string[] = []
let regex = ""
range = range.split("/").shift()!
if (range.match(/^ip[4-6]:/)) range = range.substr(4)
if (range.includes(".")) {
// This is a IPv4 address.
const partSplit = range.split(".")
Expand Down Expand Up @@ -67,26 +66,30 @@ class SPFRule {

// Defines the SPF sandbox.
class SPFSandbox {
// Defines the rules.
// Defines the rules and listeners.
private _rules: SPFRule[]
private _listeners: (() => void)[]

// Constructs the sandbox.
public constructor() {
this._rules = []
this._listeners = []
}

// Wipes all current rules.
// Wipes all current rules and listeners.
public wipe() {
this._rules = []
this._listeners = []
}

// Imports a SPF string. THIS DOES NOT IMPORT INCLUDES FROM THE STRING! THEY ARE EXPECTED TO BE INCLUDED!
public import(spf: any, fetchedIps: string[], action: boolean | undefined) {
const ips: string[] = []
for (const p of fetchedIps) ips.push(p)
for (const v4 of spf.ip4 || []) ips.push(v4[0][1])
for (const v6 of spf.ip6 || []) ips.push(v6[0][1])
// Imports a SPF string.
// THIS DOES NOT IMPORT INCLUDES FROM THE STRING! THEY ARE EXPECTED TO BE INCLUDED!
public import(spf: Record<string, any>, action: boolean | undefined) {
const ips = new Set<string>()
for (const v4 of spf.ip4 || []) ips.add(v4[0][1])
for (const v6 of spf.ip6 || []) ips.add(v6[0][1])
for (const p of ips) this._rules.push(new SPFRule(action, p))
this._listeners.forEach(listener => listener())
}

// Evals the IP address/range given.
Expand All @@ -104,6 +107,11 @@ class SPFSandbox {
return hardfail
}

// Listen for imports.
public listen(listener: () => void) {
this._listeners.push(listener)
}

// Defines if the sandbox is empty.
public empty() {
return this._rules.length === 0
Expand Down

0 comments on commit 87e32ab

Please sign in to comment.