Skip to content

feat: add config option to prevent import into client code - #12480

Closed
Dudek-AMS wants to merge 8 commits into
sveltejs:mainfrom
Dudek-AMS:main
Closed

feat: add config option to prevent import into client code#12480
Dudek-AMS wants to merge 8 commits into
sveltejs:mainfrom
Dudek-AMS:main

Conversation

@Dudek-AMS

@Dudek-AMS Dudek-AMS commented Jul 18, 2024

Copy link
Copy Markdown

Closes #12477

Added serverProtectedPaths serverOnlyPaths to add additional rules to prevent import into client code


Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

Edits

  • Please ensure that 'Allow edits from maintainers' is checked. PRs without this option may be closed.

@changeset-bot

changeset-bot Bot commented Jul 18, 2024

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 82f1df2

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@Dudek-AMS
Dudek-AMS marked this pull request as draft July 18, 2024 18:43
@Dudek-AMS
Dudek-AMS marked this pull request as ready for review July 19, 2024 08:04
@Dudek-AMS

Copy link
Copy Markdown
Author

Ive dropped the changeset so far to be added by maintainers.

@Dudek-AMS

Dudek-AMS commented Jul 20, 2024

Copy link
Copy Markdown
Author

After reconsidering the naming, I would suggest naming the attribute serverOnlyPaths

@Dudek-AMS Dudek-AMS changed the title Added serverProtectedPaths to add additional rules to prevent code be… Added serverOnlyPaths to add additional rules to prevent import into client code Jul 21, 2024
fix import paths
removed unused imports
Comment thread packages/kit/src/exports/vite/graph_analysis/index.js
import { app_server, env_dynamic_private, env_static_private } from '../module_ids.js';
import { load_config } from '../../../core/config/index.js';

const svelte_config = await load_config();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

loading the config again could be a breaking change in case a config contains one time setup code that is then executed twice.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Yeah mentioned it above, didnt saw another way to get the config on this part of code

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

that would have to be found then. using top-level-await here is also not great.

if (configRule.test(id)) return true;
} else if (typeof configRule === 'function') {
const check = configRule(id);
if (typeof check === 'boolean') return check; //always return the boolean to allow exceptions, if its undefined continue to allow multiple rules

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

early return should only happen on true, otherwise a return false could give access to an id that would be denied by a later check

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

that would be a design decision. If I want to make a exception and allow a file import, no matter what other rules say, the early return is needed. but depends on execution order then. If a rule dont care, it returns undefined/nothing

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this would allow for conflicting rules and a "first one wins" scenario, more complex, even harder to understand and not currently documented in the jsdoc in this PR.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

if really reducing to only one rule and not allowing exceptions i'd drop anything but regex, should be flexible enough IMO

@dominikg

Copy link
Copy Markdown
Contributor

3 different ways to define user-supplied rules seems a bit much, esp. if there is ()=>boolean already, users can just use regex or string compare themselves. But before continuing on this PR please see my comment in #12477

@Dudek-AMS

Copy link
Copy Markdown
Author

3 different ways to define user-supplied rules seems a bit much, esp. if there is ()=>boolean already, users can just use regex or string compare themselves. But before continuing on this PR please see my comment in #12477

the idea was to make the function method for more complicated rules, while string is prefered. ofc function only does work out

@teemingc teemingc added needs-decision Not sure if we want to do this yet, also design work needed feature / enhancement New feature or request labels Oct 9, 2024
@teemingc teemingc changed the title Added serverOnlyPaths to add additional rules to prevent import into client code feat: add config option to prevent import into client code Oct 10, 2024
@Rich-Harris

Copy link
Copy Markdown
Member

closing per #12477 (comment)

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

Labels

feature / enhancement New feature or request needs-decision Not sure if we want to do this yet, also design work needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Define customizable protected path to prevent import to client (lib/server/ like)

5 participants