fix: improve user agent validation and error messages

This commit is contained in:
mr. M 2024-12-27 16:23:57 +01:00
parent 50773b2f0e
commit 8e280426f7
No known key found for this signature in database
GPG key ID: CBD57A2AEDBDA1FB
3 changed files with 8 additions and 8 deletions

View file

@ -10,6 +10,8 @@ import { generateSlug } from './utils/slug.util';
import { GitHubService } from '../github/github.service'; import { GitHubService } from '../github/github.service';
import { SupabaseService } from '../supabase/supabase.service'; import { SupabaseService } from '../supabase/supabase.service';
const userAgentRegex = /ZenBrowser\/(\d+\.\d\w\.\d) \((.+)\)/;
@Injectable() @Injectable()
export class RicesService { export class RicesService {
constructor( constructor(
@ -25,9 +27,7 @@ export class RicesService {
const userAgent = headers['user-agent']; const userAgent = headers['user-agent'];
if (!name || !author || !userAgent) { if (!name || !author || !userAgent) {
throw new BadRequestException( throw new BadRequestException('Rice name and author are required!');
'Missing required headers: X-Zen-Rice-Name, X-Zen-Rice-Author, and User-Agent are mandatory.',
);
} }
// Validate content // Validate content
@ -56,7 +56,6 @@ export class RicesService {
} }
// Parse version and OS from User-Agent // Parse version and OS from User-Agent
const userAgentRegex = /ZenBrowser\/(\d+\.\d+\.\d.\d+) \((.+)\)/;
const match = userAgent.match(userAgentRegex); const match = userAgent.match(userAgentRegex);
if (!match) { if (!match) {
@ -157,7 +156,7 @@ export class RicesService {
) { ) {
try { try {
// Extract fields from headers // Extract fields from headers
const userAgent = headers['user-agent']; const userAgent = headers['User-Agent'];
if (!userAgent) { if (!userAgent) {
throw new BadRequestException( throw new BadRequestException(
@ -166,7 +165,8 @@ export class RicesService {
} }
// Parse version and OS from User-Agent // Parse version and OS from User-Agent
const userAgentRegex = /ZenBrowser\/(\d+\.\d+\.\d.\d+) \((.+)\)/; // It must have the following format:
// example version: 1.0.2-b.1
const match = userAgent.match(userAgentRegex); const match = userAgent.match(userAgentRegex);
if (!match) { if (!match) {

View file

@ -4,7 +4,7 @@ POST {{baseUrl}}/rices
Content-Type: application/json Content-Type: application/json
X-Zen-Rice-Name: cool-zenrice-aurora2 X-Zen-Rice-Name: cool-zenrice-aurora2
X-Zen-Rice-Author: jhon@doe.com X-Zen-Rice-Author: jhon@doe.com
User-Agent: ZenBrowser/1.0.0.0 (EndeavourOS x86_64) User-Agent: ZenBrowser/1.2b.0 (EndeavourOS x86_64)
{ {
"userChrome": "", "userChrome": "",

View file

@ -7,7 +7,7 @@ Content-Type: application/json
x-zen-rices-token: {{previous_token}} x-zen-rices-token: {{previous_token}}
X-Zen-Rice-Name: cool-zenrice-aurora2 X-Zen-Rice-Name: cool-zenrice-aurora2
X-Zen-Rice-Author: jhon@doe.com X-Zen-Rice-Author: jhon@doe.com
User-Agent: ZenBrowser/1.0.0.0 (EndeavourOS x86_64) User-Agent: ZenBrowser/1.2b.0 (EndeavourOS x86_64)
{ {
"userChrome": "", "userChrome": "",