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

Confusion about wildcards in file extensions #892

Open
Huliiiiii opened this issue Oct 23, 2024 · 2 comments
Open

Confusion about wildcards in file extensions #892

Huliiiiii opened this issue Oct 23, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@Huliiiiii
Copy link

What version of Elysia is running?

No response

What platform is your computer?

No response

What steps can reproduce the bug?

This is how we check for file extensions:

elysia/src/type-system.ts

Lines 207 to 227 in 9007186

const validateFile = (options: ElysiaTypeOptions.File, value: any) => {
if (!(value instanceof Blob)) return false
if (options.minSize && value.size < parseFileUnit(options.minSize))
return false
if (options.maxSize && value.size > parseFileUnit(options.maxSize))
return false
if (options.extension)
if (typeof options.extension === 'string') {
if (!value.type.startsWith(options.extension)) return false
} else {
for (let i = 0; i < options.extension.length; i++)
if (value.type.startsWith(options.extension[i])) return true
return false
}
return true
}

But we have wildcard file types:

elysia/src/type-system.ts

Lines 103 to 157 in 9007186

export type StrictFileType =
| 'image'
| 'image/*'
| 'image/jpeg'
| 'image/png'
| 'image/gif'
| 'image/tiff'
| 'image/x-icon'
| 'image/svg'
| 'image/webp'
| 'image/avif'
| 'audio'
| 'audio/*'
| 'audio/aac'
| 'audio/mpeg'
| 'audio/x-ms-wma'
| 'audio/vnd.rn-realaudio'
| 'audio/x-wav'
| 'video'
| 'video/*'
| 'video/mpeg'
| 'video/mp4'
| 'video/quicktime'
| 'video/x-ms-wmv'
| 'video/x-msvideo'
| 'video/x-flv'
| 'video/webm'
| 'text'
| 'text/*'
| 'text/css'
| 'text/csv'
| 'text/html'
| 'text/javascript'
| 'text/plain'
| 'text/xml'
| 'application'
| 'application/*'
| 'application/graphql'
| 'application/graphql-response+json'
| 'application/ogg'
| 'application/pdf'
| 'application/xhtml'
| 'application/xhtml+html'
| 'application/xml-dtd'
| 'application/html'
| 'application/json'
| 'application/ld+json'
| 'application/xml'
| 'application/zip'
| 'font'
| 'font/*'
| 'font/woff2'
| 'font/woff'
| 'font/ttf'
| 'font/otf'

If we use wildcards in the extension, type checking will always fail:

function check(options, type) {
  if (options.extension)
    if (typeof options.extension === "string") {
      if (!type.startsWith(options.extension)) return false
    } else {
      for (let i = 0; i < options.extension.length; i++)
        if (type.startsWith(options.extension[i])) return true

      return false
    }
}

check({ extension: ["image/*"] }, "image/jpeg") // false
check({ extension: ["image/*"] }, "image/png") // false

Is this intentional?

What is the expected behavior?

No response

What do you see instead?

No response

Additional information

No response

Have you try removing the node_modules and bun.lockb and try again yet?

No response

@Huliiiiii Huliiiiii added the bug Something isn't working label Oct 23, 2024
@K4leri
Copy link

K4leri commented Oct 24, 2024

@Huliiiiii Already done #877

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants
@K4leri @Huliiiiii and others