Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Validate url #710

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/entities/Event/routes/createEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ import {
EventAttributes,
MAX_EVENT_DURATION,
} from "../types"
import { calculateRecurrentProperties, eventTargetUrl } from "../utils"
import {
calculateRecurrentProperties,
eventTargetUrl,
validateImageUrl,
} from "../utils"

const validateNewEvent = createValidator<EventAttributes>(
newEventSchema as AjvObjectSchema
Expand Down Expand Up @@ -61,6 +65,10 @@ export async function createEvent(req: WithAuthProfile<WithAuth>) {

validateNewEvent(data)

if (data.image) {
await validateImageUrl(data.image)
}

const x = data.x
const y = data.y
if (!isInsideWorldLimits(x, y)) {
Expand Down
10 changes: 9 additions & 1 deletion src/entities/Event/routes/updateEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ import {
editEventAttributes,
editOwnEventAttributes,
} from "../types"
import { calculateRecurrentProperties, eventTargetUrl } from "../utils"
import {
calculateRecurrentProperties,
eventTargetUrl,
validateImageUrl,
} from "../utils"

import { DECENTRALAND_URL } from "./index"

Expand Down Expand Up @@ -111,6 +115,10 @@ export async function updateEvent(req: WithAuthProfile<WithAuth>) {
recurrent_until: Time.date(updatedAttributes.recurrent_until)?.toJSON(),
})

if (updatedAttributes.image) {
await validateImageUrl(updatedAttributes.image)
}

// make schedules unique
if (updatedAttributes.schedules) {
updatedAttributes.schedules = Array.from(
Expand Down
21 changes: 21 additions & 0 deletions src/entities/Event/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import logger from "decentraland-gatsby/dist/entities/Development/logger"
import RequestError from "decentraland-gatsby/dist/entities/Route/error"
import { CatalystAbout } from "decentraland-gatsby/dist/utils/api/Catalyst.types"
import Time from "decentraland-gatsby/dist/utils/date/Time"
import env from "decentraland-gatsby/dist/utils/env"
Expand All @@ -16,6 +18,7 @@ import {
WeekdayMask,
Weekdays,
} from "./types"
import { POSTER_FILE_SIZE, POSTER_FILE_TYPES } from "../Poster/types"
import { ScheduleAttributes } from "../Schedule/types"

const DECENTRALAND_URL = env(
Expand All @@ -31,6 +34,8 @@ const PROFILE_SITE_URL = env(
"https://profile.decentraland.org"
)

const BUCKET_URL = env("AWS_BUCKET_URL")

export function profileSiteUrl(address: string) {
const target = new URL(PROFILE_SITE_URL)
target.pathname = `/accounts/${address}`
Expand Down Expand Up @@ -449,3 +454,19 @@ export function isPastEvent(event: EventAttributes) {
const finish_at = Time.date(event.finish_at)
return finish_at.getTime() < now
}

export async function validateImageUrl(imageUrl: string) {
const url = new URL(imageUrl)
const whitelistedDomains = [BUCKET_URL].filter(
(domain): domain is string => !!domain
)

if (!whitelistedDomains.some((domain) => domain.endsWith(url.host))) {
throw new RequestError(
`Invalid image url ${imageUrl}, please upload the image through the upload poster endpoint (POST /poster)`,
RequestError.BadRequest
)
}

return imageUrl
}