Skip to content

Commit

Permalink
🐛 fix: prevent SSRF attacks
Browse files Browse the repository at this point in the history
  • Loading branch information
MakakWasTaken committed Feb 15, 2024
1 parent 249c95f commit 4ca73a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 28 deletions.
20 changes: 4 additions & 16 deletions pages/api/database/recipe/structured-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,29 +61,17 @@ const handleGET = async (
res: NextApiResponse,
session?: Session,
) => {
const url = req.query.url
const content = req.query.content

if (!url || typeof url !== 'string') {
if (!content || typeof content !== 'string') {
res.status(400).json({
ok: false,
message: "'url' is not valid",
message: "'content' is not valid",
})
return
}

const response = await axios.get(url)

// Extract the structured data from the response

if (!response.headers['content-type'].includes('text/html')) {
res.status(400).json({
ok: false,
message: 'The url does not link to a website',
})
return
}

const parsedRecipe = await parseHTML(response.data)
const parsedRecipe = await parseHTML(content)

if (typeof parsedRecipe === 'string') {
throw new Error(parsedRecipe)
Expand Down
33 changes: 21 additions & 12 deletions pages/recipe/create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
Typography,
} from '@mui/material'
import { Recipe, RecipeImage, RecipeIngredient } from '@prisma/client'
import axios from 'axios'
import { GetStaticProps } from 'next'
import { useSession } from 'next-auth/react'
import { useTranslation } from 'next-i18next'
Expand Down Expand Up @@ -142,18 +143,26 @@ const CreateRecipePage: FC = () => {

const handleImportSubmit = () => {
toast.promise(
api.get<
YKResponse<
Recipe & {
ingredients: RecipeIngredient[]
image: (RecipeImage & { file?: File })[]
}
>
>('database/recipe/structured-data', {
params: {
url: importUrl,
},
}),
async () => {
// We do this client side to prevent SSRF attacks
const contentResponse = await axios.get(importUrl, {
headers: {
Accept: 'text/html',
},
})
return api.get<
YKResponse<
Recipe & {
ingredients: RecipeIngredient[]
image: (RecipeImage & { file?: File })[]
}
>
>('database/recipe/structured-data', {
params: {
content: contentResponse.data,
},
})
},
{
loading: t('importing_recipe'),
error: (err) => err.message ?? err,
Expand Down

0 comments on commit 4ca73a9

Please sign in to comment.