Skip to content

Force user to explicitly select token scope, show warning if user scopes to account #6274

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions tests/unit/manage/test_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ def test_validate_description_missing(self):
)

assert not form.validate()
assert form.description.errors.pop() == "Specify a description"
assert form.description.errors.pop() == "Specify a token name"

def test_validate_description_in_use(self):
form = forms.CreateMacaroonForm(
Expand All @@ -371,7 +371,18 @@ def test_validate_token_scope_missing(self):
)

assert not form.validate()
assert form.token_scope.errors.pop() == "Specify a token scope"
assert form.token_scope.errors.pop() == "Specify the token scope"

def test_validate_token_scope_unspecified(self):
form = forms.CreateMacaroonForm(
data={"description": "dummy", "token_scope": "scope:unspecified"},
user_id=pretend.stub(),
macaroon_service=pretend.stub(get_macaroon_by_description=lambda *a: None),
project_names=pretend.stub(),
)

assert not form.validate()
assert form.token_scope.errors.pop() == "Specify the token scope"

@pytest.mark.parametrize(
("scope"), ["not a real scope", "scope:project", "scope:foo:bar"]
Expand Down
7 changes: 5 additions & 2 deletions warehouse/manage/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ def __init__(self, *args, user_id, macaroon_service, project_names, **kwargs):

description = wtforms.StringField(
validators=[
wtforms.validators.DataRequired(message="Specify a description"),
wtforms.validators.DataRequired(message="Specify a token name"),
wtforms.validators.Length(
max=100, message="Description must be 100 characters or less"
),
]
)

token_scope = wtforms.StringField(
validators=[wtforms.validators.DataRequired(message="Specify a token scope")]
validators=[wtforms.validators.DataRequired(message="Specify the token scope")]
)

def validate_description(self, field):
Expand All @@ -224,6 +224,9 @@ def validate_token_scope(self, field):
except ValueError:
raise wtforms.ValidationError(f"Unknown token scope: {scope}")

if scope_kind == "unspecified":
raise wtforms.ValidationError(f"Specify the token scope")

if scope_kind == "user":
self.validated_scope = scope_kind
return
Expand Down
18 changes: 18 additions & 0 deletions warehouse/static/js/warehouse/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,24 @@ docReady(ProvisionWebAuthn);
// Get WebAuthn authentication ready
docReady(AuthenticateWebAuthn);

docReady(() => {
const tokenSelect = document.getElementById("token_scope");

if (tokenSelect === null) {
return;
}

tokenSelect.addEventListener("change", () => {
const tokenScopeWarning = document.getElementById("api-token-scope-warning");
if (tokenScopeWarning === null) {
return;
}

const tokenScope = tokenSelect.options[tokenSelect.selectedIndex].value;
tokenScopeWarning.hidden = (tokenScope !== "scope:user");
});
});

// Bind again when client-side includes have been loaded (for the logged-in
// user dropdown)
document.addEventListener("CSILoaded", bindDropdowns);
Expand Down
9 changes: 9 additions & 0 deletions warehouse/static/sass/blocks/_callout-block.scss
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

Modifiers:
- danger: Makes border red
- warning: Makes border brown
- success: Makes border green
- bottom-margin: Adds extra margin below the callout
*/
Expand Down Expand Up @@ -68,6 +69,14 @@
}
}

&--warning {
border-color: $warn-text;

&:before {
background-color: $warn-text;
}
}

&--success {
border-color: $success-color;

Expand Down
9 changes: 7 additions & 2 deletions warehouse/templates/manage/token.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,20 @@ <h2>Token for "{{ macaroon.description }}"</h2>
<p id="description-help-text" class="form-group__help-text">What is this token for?</p>
</div>
<div class="form-group">
<label for="scope" class="form-group__label">Scope</label>
<select name="token_scope" id="scope" class="form-group__input">
<label for="token_scope" class="form-group__label">Scope</label>
<select name="token_scope" id="token_scope" class="form-group__input" aria-describedby="token_scope-errors">
<option disabled selected value="scope:unspecified">Select scope...</option>
<option value="scope:user">Entire account (all projects)</option>
{% for project in project_names %}
<option value="scope:project:{{ project }}">Project: {{ project }}</option>
{% endfor %}
</select>
{{ field_errors(create_macaroon_form.token_scope) }}
</div>
<div id="api-token-scope-warning" class="callout-block callout-block--warning" hidden>
<h3 class="callout-block__heading">Proceed with caution!</h3>
<p>An API token scoped to your entire account will have upload permissions for all of your projects.</p>
</div>
<div>
<input value="Add token" class="button button--primary" type="submit">
</div>
Expand Down