Skip to content

fix(view): handle view names ending with "." to avoid require("")#7351

Open
webdevelopersrinu wants to merge 1 commit into
expressjs:masterfrom
webdevelopersrinu:fix/view-trailing-dot-render
Open

fix(view): handle view names ending with "." to avoid require("")#7351
webdevelopersrinu wants to merge 1 commit into
expressjs:masterfrom
webdevelopersrinu:fix/view-trailing-dot-render

Conversation

@webdevelopersrinu

Copy link
Copy Markdown

Problem

A view name ending in "." (e.g. res.render('index.') or app.render('index.', cb)) makes the View constructor call require(""), throwing TypeError [ERR_INVALID_ARG_VALUE]: The argument 'id' must be a non-empty string. With app.render() this throws past the callback; with res.render() it surfaces as an opaque 500.

Cause

path.extname('index.') returns '.' (truthy), so the default-engine fallback in lib/view.js is skipped, this.ext stays '.', and this.ext.slice(1) is '' — leading to require('').

Fix

Treat a bare '.' extension as no extension, so the normal default-engine path runs and an unresolved view yields the usual Failed to lookup view error via the callback.

Tests

Added a regression test in test/app.render.js. It fails without the fix (with the ERR_INVALID_ARG_VALUE crash) and passes with it. Full suite (1250 tests) and lint pass.

Fixes #7350

A view name ending in "." (e.g. res.render('index.')) produced a bare "."
from path.extname(), which skipped the default-engine fallback and led to
require("") — throwing an opaque TypeError [ERR_INVALID_ARG_VALUE] that
bypassed the render callback. Treat a bare-dot extension as no extension so
a normal "Failed to lookup view" error is delivered instead.

@Ziiyodullayevv Ziiyodullayevv left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good fix for view names ending with dot. This prevents potential security issues with require() receiving empty strings. The validation is simple and effective.

@webdevelopersrinu

Copy link
Copy Markdown
Author

Thanks for taking a look and for the feedback, @Ziiyodullayevv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

res.render()/app.render() throws opaque TypeError for a view name ending in "."

2 participants