Route Safety Question
Answered
Holland Lop posted this in #help-forum
Holland LopOP
Hello! I was wondering if this code is "safe"? The code is a route handler intended to extract some params from the request. I'm wondering if there are any opportunities to exploit this though, and how I can make it better. Thanks!
import generateClient from '@/lib/shopify';
import { Product } from 'shopify-buy';
export async function GET(request: Request) {
const url = request.url;
const split = url.split('?');
const params = split[1].split('&');
let query = '';
for (const param of params) {
const split = param.split('=');
if (split[0] === 'tag') {
query = `tag:${split[1]}`;
}
}
const client = generateClient();
client.graphQLClient.query((root: any) => {/* ... */});
const data = await client.graphQLClient.send(productsQuery);
const res = data.model.products;
const products = /* ... */;
return new Response(JSON.stringify(products));
}Answered by Ray
you could get the query like this
import { NextRequest } from "next/server";
export async function GET(req: NextRequest) {
const params = req.nextUrl.searchParams;
const tag = params.get("tag");
let query = "";
if (tag) {
query = `tag:${tag}`;
}
...
const products = /* ... */
return Response.json(products);
}3 Replies
@Holland Lop Hello! I was wondering if this code is "safe"? The code is a route handler intended to extract some params from the request. I'm wondering if there are any opportunities to exploit this though, and how I can make it better. Thanks!
ts
import generateClient from '@/lib/shopify';
import { Product } from 'shopify-buy';
export async function GET(request: Request) {
const url = request.url;
const split = url.split('?');
const params = split[1].split('&');
let query = '';
for (const param of params) {
const split = param.split('=');
if (split[0] === 'tag') {
query = `tag:${split[1]}`;
}
}
const client = generateClient();
client.graphQLClient.query((root: any) => {/* ... */});
const data = await client.graphQLClient.send(productsQuery);
const res = data.model.products;
const products = /* ... */;
return new Response(JSON.stringify(products));
}
you could get the query like this
import { NextRequest } from "next/server";
export async function GET(req: NextRequest) {
const params = req.nextUrl.searchParams;
const tag = params.get("tag");
let query = "";
if (tag) {
query = `tag:${tag}`;
}
...
const products = /* ... */
return Response.json(products);
}Answer
@Holland Lop thank you!
and this
params.getAll("tag") if there are multiple