Skip to content

Commit

Permalink
add unauntheticated endpoint returning sensitive data alert
Browse files Browse the repository at this point in the history
  • Loading branch information
NikhilShahi committed Oct 3, 2022
1 parent 39af5fc commit 72455e4
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
25 changes: 25 additions & 0 deletions backend/src/services/alert/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { Alert, ApiEndpoint, ApiTrace, DataField, OpenApiSpec } from "models"
import {
AlertType,
DataSection,
RestMethod,
SpecExtension,
Status,
UpdateAlertType,
Expand Down Expand Up @@ -523,4 +524,28 @@ export class AlertService {
return []
}
}

static async createUnauthEndpointSenDataAlerts(
endpoints: Array<{ uuid: string, path: string, host: string, method: RestMethod }>
) {
try {
if (!endpoints || endpoints?.length === 0) {
return []
}
let alerts: Alert[] = []
for (const item of endpoints) {
const description = `${item.method} ${item.path} in ${item.host} is returning sensitive data.`
const newAlert = new Alert()
newAlert.type = AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA
newAlert.riskScore = ALERT_TYPE_TO_RISK_SCORE[AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA]
newAlert.apiEndpointUuid = item.uuid
newAlert.description = description
alerts.push(newAlert)
}
return alerts
} catch (err) {
console.error(`Error creating alert for unauthenticated endpoints returning sensitive data: ${err}`)
return []
}
}
}
6 changes: 5 additions & 1 deletion backend/src/services/jobs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
} from "utils"
import { ApiEndpoint, ApiTrace, OpenApiSpec, Alert, DataField } from "models"
import { AppDataSource } from "data-source"
import { AlertType, DataType, RestMethod, SpecExtension } from "@common/enums"
import { AlertType, DataSection, DataTag, DataType, RestMethod, SpecExtension, Status } from "@common/enums"
import { getPathTokens } from "@common/utils"
import { AlertService } from "services/alert"
import { DataFieldService } from "services/data-field"
Expand All @@ -21,6 +21,7 @@ import { SpecService } from "services/spec"
import {
aggregateTracesDataHourlyQuery,
aggregateTracesDataMinutelyQuery,
getUnauthenticatedEndpointsSensitiveData,
updateUnauthenticatedEndpoints,
} from "./queries"

Expand Down Expand Up @@ -135,6 +136,9 @@ export class JobsService {
try {
await queryRunner.connect()
await queryRunner.query(updateUnauthenticatedEndpoints)
const endpointsToAlert = await queryRunner.query(getUnauthenticatedEndpointsSensitiveData, [DataSection.RESPONSE_BODY, DataTag.PII, AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA, Status.RESOLVED])
const alerts = await AlertService.createUnauthEndpointSenDataAlerts(endpointsToAlert)
await queryRunner.manager.createQueryBuilder().insert().into(Alert).values(alerts).execute()
} catch (err) {
console.error(
`Encountered error when checking for unauthenticated endpoints: ${err}`,
Expand Down
37 changes: 37 additions & 0 deletions backend/src/services/jobs/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,40 @@ export const updateUnauthenticatedEndpoints = `
AND "sessionMeta" ->> 'authenticationSuccessful' = 'true'
)
`

export const getUnauthenticatedEndpointsSensitiveData = `
With endpoints AS (
SELECT
endpoint.uuid,
endpoint.path,
endpoint.method,
endpoint.host
FROM
"api_endpoint" "endpoint"
LEFT JOIN "data_field" "field" ON "field" ."apiEndpointUuid" = "endpoint" ."uuid"
WHERE
(
endpoint."isAuthenticatedDetected" = FALSE
OR endpoint."isAuthenticatedUserSet" = FALSE
)
AND field."dataSection" = $1
AND field."dataTag" = $2
GROUP BY
1
)
SELECT
*
FROM
endpoints
WHERE
endpoints.uuid NOT IN (
SELECT
"apiEndpointUuid"
FROM
alert
WHERE
alert."apiEndpointUuid" = endpoints.uuid
AND alert.type = $3
AND alert.status != $4
)
`
2 changes: 2 additions & 0 deletions common/src/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export enum AlertType {
PATH_SENSITIVE_DATA = "Sensitive Data in Path Params",
BASIC_AUTHENTICATION_DETECTED = "Basic Authentication Detected",
UNSECURED_ENDPOINT_DETECTED = "Endpoint not secured by SSL",
UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA = "Unauthenticated Endpoint returning Sensitive Data",
}

export const VULNERABILITY_ALERT_TYPES = [
Expand All @@ -63,6 +64,7 @@ export const VULNERABILITY_ALERT_TYPES = [
AlertType.PATH_SENSITIVE_DATA,
AlertType.BASIC_AUTHENTICATION_DETECTED,
AlertType.UNSECURED_ENDPOINT_DETECTED,
AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA,
]

export enum AttackType {
Expand Down
1 change: 1 addition & 0 deletions common/src/maps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ export const ALERT_TYPE_TO_RISK_SCORE: Record<AlertType, RiskScore> = {
[AlertType.PATH_SENSITIVE_DATA]: RiskScore.HIGH,
[AlertType.BASIC_AUTHENTICATION_DETECTED]: RiskScore.MEDIUM,
[AlertType.UNSECURED_ENDPOINT_DETECTED]: RiskScore.HIGH,
[AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA]: RiskScore.HIGH,
}

export const ATTACK_TYPE_TO_RISK_SCORE: Record<AttackType, RiskScore> = {
Expand Down
12 changes: 12 additions & 0 deletions frontend/src/components/Alert/AlertDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { getDateTimeString } from "utils"
import { METHOD_TO_COLOR, STATUS_TO_COLOR } from "~/constants"
import TraceDetail from "components/Endpoint/TraceDetail"
import { getSpec } from "api/apiSpecs"
import Link from "next/link"

export interface SpecDiffContext {
pathPointer: string[]
Expand Down Expand Up @@ -245,6 +246,17 @@ export const AlertDetail: React.FC<AlertDetailProps> = ({
setRightPanel(res.rightPanel)
setLoadingSpec(false)
break
case AlertType.UNAUTHENTICATED_ENDPOINT_SENSITIVE_DATA:
setLeftPanel(
<Box alignSelf="flex-start">
<Link href={`/endpoint/${alert.apiEndpointUuid}?tab=fields`}>
<Text as="button" fontWeight="semibold">
View Endpoint →
</Text>
</Link>
</Box>,
)
break
case AlertType.PII_DATA_DETECTED:
case AlertType.QUERY_SENSITIVE_DATA:
case AlertType.BASIC_AUTHENTICATION_DETECTED:
Expand Down

0 comments on commit 72455e4

Please sign in to comment.